From 21f61b0911797b5456d633021f9ea26020157114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Wed, 16 Nov 2022 07:18:21 +0100 Subject: [PATCH] maintanance and downloading files. --- app/Http/Controllers/ChapterController.php | 18 +++++++++++++ app/Http/Controllers/CourseController.php | 10 ++----- app/Http/Controllers/Index.php | 7 ++--- app/Http/SymfonyCastDl/HtmlParser.php | 7 ++--- .../SymfonyCastDl/SymfonyCastDlService.php | 19 ++++++++++--- app/Jobs/DownloadVideoFile.php | 27 +++++++++++++++++++ app/Jobs/GetVideoFileSize.php | 13 ++------- app/Providers/AppServiceProvider.php | 7 ++++- resources/views/index.blade.php | 1 + routes/web.php | 15 +++++++---- 10 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 app/Http/Controllers/ChapterController.php create mode 100644 app/Jobs/DownloadVideoFile.php diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php new file mode 100644 index 0000000..abae6e5 --- /dev/null +++ b/app/Http/Controllers/ChapterController.php @@ -0,0 +1,18 @@ +videoSize($chapter); + $symfonyCastDlService->downloadFile($chapter); +// $symfonyCastDlService->videoSize($chapter); +// file_put_contents('movie.avi', file_get_contents($chapter->video_link)); + dd($chapter->toArray()); + } +} diff --git a/app/Http/Controllers/CourseController.php b/app/Http/Controllers/CourseController.php index ec34920..b5126ed 100644 --- a/app/Http/Controllers/CourseController.php +++ b/app/Http/Controllers/CourseController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Http\SymfonyCastDl\HtmlParser; use App\Http\SymfonyCastDl\SymfonyCastDlService; +use App\Jobs\DownloadVideoFile; use App\Models\Chapter; use App\Models\Course; use Illuminate\View\View; @@ -15,17 +16,10 @@ class CourseController extends Controller return view('course.index', compact('course')); } - public function chapter(Chapter $chapter, HtmlParser $htmlParser) - { - $service = new SymfonyCastDlService($htmlParser); - $service->videoSize($chapter); - - dd($chapter->toArray()); - } - public function sync(Course $course) { $course->chapters->each->update(['sync_offline' => 1]); + $course->chapters->each(fn($chapter) => DownloadVideoFile::dispatch($chapter->id)); return redirect(route('course.index', ['course' => $course])); } diff --git a/app/Http/Controllers/Index.php b/app/Http/Controllers/Index.php index 2fbd336..1d35e7d 100644 --- a/app/Http/Controllers/Index.php +++ b/app/Http/Controllers/Index.php @@ -12,12 +12,13 @@ class Index extends Controller public function index() { $courses = Course::with('chapters')->withCount('chapters')->get(); + +// dd($courses->toArray()); return view('index', compact(['courses'])); } - public function download(HtmlParser $htmlParser) + public function download(SymfonyCastDlService $symfonyCastDlService) { - $service = new SymfonyCastDlService($htmlParser); - $service->getInfo(); + $symfonyCastDlService->getInfo(); } } diff --git a/app/Http/SymfonyCastDl/HtmlParser.php b/app/Http/SymfonyCastDl/HtmlParser.php index 23c6a67..b524ed0 100644 --- a/app/Http/SymfonyCastDl/HtmlParser.php +++ b/app/Http/SymfonyCastDl/HtmlParser.php @@ -26,7 +26,7 @@ class HtmlParser $course = Course::firstOrNew(['course_id' => $courseId]); $course->name = $courseItem->first('h3')->text(); $course->thumbnail = $courseItem->first('img.course-list-item-img')->attr('src'); - $course->link = $courseItem->first('a')->attr('href'); + $course->link = last(explode('/', $courseItem->first('a')->attr('href'))); $course->status = $courseItem->attr('data-status'); $course->course_id = $courseItem->attr('data-id'); $course->numberofchapters = $courseItem->attr('data-chapter-count'); @@ -49,15 +49,12 @@ class HtmlParser if ($chapterItem->first('.col')) { $chapterId++; -// if(!$chapterItem->first('.length-styling')){ -// dd($chapterItem->html()); -// } $chapter = Chapter::firstOrNew(['course_id' => $courseId, 'order' => $chapterId]); $chapter->duration = $chapterItem->first('.length-styling')?->text(); $chapter->order = $chapterId; $chapter->course_id = $courseId; if ($link = trim($chapterItem->first('a')->attr('href'), '#')) { - $chapter->link = config('symfonycast.base_url') . $link; + $chapter->link = last(explode('/', $link));; $chapter->video_link = config('symfonycast.base_url') . $link . '/download/video'; } $chapter->title = preg_replace('/\v(?:[\v\h]+)/', '', $chapterItem->first('.col')->text()); diff --git a/app/Http/SymfonyCastDl/SymfonyCastDlService.php b/app/Http/SymfonyCastDl/SymfonyCastDlService.php index c13a34a..9716cb2 100644 --- a/app/Http/SymfonyCastDl/SymfonyCastDlService.php +++ b/app/Http/SymfonyCastDl/SymfonyCastDlService.php @@ -21,10 +21,9 @@ class SymfonyCastDlService $response = $this->client->get('login'); - $token = $htmlParser->getCsrfToken($response); - $response = $this->client->post('login', [ + $this->client->post('login', [ 'form_params' => [ 'email' => config('symfonycast.login'), 'password' => config('symfonycast.password'), @@ -44,7 +43,7 @@ class SymfonyCastDlService $courses->each->save(); /** @var Course $course */ foreach ($courses as $course) { - $singleCoursePage = $this->client->get($course->link); + $singleCoursePage = $this->client->get('/screencast/' . $course->link); $chapters = $this->htmlParser->getCourseDetails($singleCoursePage, $course->id); $chapters->each->save(); $chapters->each(fn($chapter) => GetVideoFileSize::dispatch($chapter->id)); @@ -55,6 +54,7 @@ class SymfonyCastDlService { try { if (!$chapter->video_size) { + echo 1; $response = $this->client->head($chapter->video_link); if ($response->hasHeader('Content-Length')) { $chapter->video_size = $response->getHeader('Content-Length')[0]; @@ -65,4 +65,17 @@ class SymfonyCastDlService } return $chapter; } + + public function downloadFile(Chapter $chapter): bool + { + if (!is_dir($chapter->course_id . '/')) { + mkdir($chapter->course_id); + } + $this->client->request( + 'GET', + $chapter->video_link, + ['sink' => $chapter->course_id . '/' . $chapter->id . '.mp4'] + ); + return true; + } } diff --git a/app/Jobs/DownloadVideoFile.php b/app/Jobs/DownloadVideoFile.php new file mode 100644 index 0000000..19f6667 --- /dev/null +++ b/app/Jobs/DownloadVideoFile.php @@ -0,0 +1,27 @@ +videoSize(Chapter::find($this->chapterId)); + } +} diff --git a/app/Jobs/GetVideoFileSize.php b/app/Jobs/GetVideoFileSize.php index b02559a..b5a658d 100644 --- a/app/Jobs/GetVideoFileSize.php +++ b/app/Jobs/GetVideoFileSize.php @@ -20,17 +20,8 @@ class GetVideoFileSize implements ShouldQueue { } - - - public function handle(HtmlParser $htmlParser) + public function handle(SymfonyCastDlService $symfonyCastDlService) { - -// $this-> -// $this->videoSize($this->chapter); -// $this->chapter->save(); - $service = new SymfonyCastDlService($htmlParser); - $service->videoSize(Chapter::find($this->chapterId)); - -// dd($this->chapterId); + $symfonyCastDlService->videoSize(Chapter::find($this->chapterId)); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..650ea71 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Http\SymfonyCastDl\HtmlParser; +use App\Http\SymfonyCastDl\SymfonyCastDlService; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -13,7 +15,10 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + $this->app->singleton(SymfonyCastDlService::class, function ($app) { + $htmlparser = new HtmlParser(); + return new SymfonyCastDlService($htmlparser); + }); } /** diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 1026cce..67b0ace 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -1,4 +1,5 @@ +

List of Courses

diff --git a/routes/web.php b/routes/web.php index ccecd2a..2a092d6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,8 @@ name('course.index'); -Route::get('/course/{course}/sync', [\App\Http\Controllers\CourseController::class, 'sync'])->name('course.sync'); -Route::get('/chapter/{chapter}', [\App\Http\Controllers\CourseController::class, 'chapter'])->name('course.chapter'); +Route::get('/download', [Index::class, 'download']); +Route::get('/', [Index::class, 'index']); +Route::prefix('course')->name('course.')->group(function () { + Route::get('/{course}', [CourseController::class, 'index'])->name('index'); + Route::get('/{course}/sync', [CourseController::class, 'sync'])->name('sync'); +}); +Route::get('/chapter/{chapter}', [ChapterController::class, 'index'])->name('course.chapter');