diff --git a/app/Http/Controllers/CourseController.php b/app/Http/Controllers/CourseController.php index 4a8aab2..ec34920 100644 --- a/app/Http/Controllers/CourseController.php +++ b/app/Http/Controllers/CourseController.php @@ -22,4 +22,11 @@ class CourseController extends Controller dd($chapter->toArray()); } + + public function sync(Course $course) + { + $course->chapters->each->update(['sync_offline' => 1]); + return redirect(route('course.index', ['course' => $course])); + } + } diff --git a/app/Http/SymfonyCastDl/HtmlParser.php b/app/Http/SymfonyCastDl/HtmlParser.php index 02b09f4..23c6a67 100644 --- a/app/Http/SymfonyCastDl/HtmlParser.php +++ b/app/Http/SymfonyCastDl/HtmlParser.php @@ -31,6 +31,7 @@ class HtmlParser $course->course_id = $courseItem->attr('data-id'); $course->numberofchapters = $courseItem->attr('data-chapter-count'); $course->timeswatched = $courseItem->attr('data-times-watched'); + $course->course_duration = $courseItem->first('.font-blue.fal.fa-clock.pr-1')?->parent()?->text(); $course->published_at = $courseItem->attr('data-date') > 0 ? Carbon::createFromTimestamp( $courseItem->attr('data-date') ) : null; @@ -55,10 +56,10 @@ class HtmlParser $chapter->duration = $chapterItem->first('.length-styling')?->text(); $chapter->order = $chapterId; $chapter->course_id = $courseId; - $chapter->link = config('symfonycast.base_url') . $chapterItem->first('a')->attr('href'); - $chapter->video_link = config('symfonycast.base_url') . $chapterItem->first('a')->attr( - 'href' - ) . '/download/video'; + if ($link = trim($chapterItem->first('a')->attr('href'), '#')) { + $chapter->link = config('symfonycast.base_url') . $link; + $chapter->video_link = config('symfonycast.base_url') . $link . '/download/video'; + } $chapter->title = preg_replace('/\v(?:[\v\h]+)/', '', $chapterItem->first('.col')->text()); $chapter->video_size = 0; $chapters->add($chapter); diff --git a/app/Http/SymfonyCastDl/SymfonyCastDlService.php b/app/Http/SymfonyCastDl/SymfonyCastDlService.php index fd0bab4..8cb25f0 100644 --- a/app/Http/SymfonyCastDl/SymfonyCastDlService.php +++ b/app/Http/SymfonyCastDl/SymfonyCastDlService.php @@ -41,24 +41,27 @@ class SymfonyCastDlService $courses = $this->htmlParser->getCourses($coursePage); $courses->each(fn($course) => $course->save()); - $singleCoursePage = $this->client->get($courses[3]->link); +// $singleCoursePage = $this->client->get($courses[3]->link); /** @var Course $course */ foreach ($courses as $course) { $singleCoursePage = $this->client->get($course->link); $chapters = $this->htmlParser->getCourseDetails($singleCoursePage, $course->id); - $chapters->each(fn($chapter) => $this->videoSize($chapter)->save()); +// $chapters->each(fn($chapter) => $this->videoSize($chapter)->save()); + $chapters->each(fn($chapter) => $chapter->save()); } } public function videoSize(Chapter $chapter): Chapter { try { - $response = $this->client->head($chapter->video_link); - if ($response->hasHeader('Content-Length')) { - $chapter->video_size = $response->getHeader('Content-Length')[0]; + if (!$chapter->video_size) { + $response = $this->client->head($chapter->video_link); + if ($response->hasHeader('Content-Length')) { + $chapter->video_size = $response->getHeader('Content-Length')[0]; + } + $chapter->save(); } - } catch (\GuzzleHttp\Exception\ClientException $exception) { - + } catch (\Exception $exception) { } return $chapter; } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 092e0a5..29e8d5d 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property string $title * @property string $duration * @property integer $course_id + * @property bool $sync_offline */ class Chapter extends Model { @@ -27,6 +28,7 @@ class Chapter extends Model 'title', 'duration', 'course_id', + 'sync_offline', ]; public function course(): BelongsTo diff --git a/database/migrations/2022_08_18_094159_create_course_table.php b/database/migrations/2022_08_18_094159_create_course_table.php index e3f9729..dc98aeb 100644 --- a/database/migrations/2022_08_18_094159_create_course_table.php +++ b/database/migrations/2022_08_18_094159_create_course_table.php @@ -16,6 +16,7 @@ return new class extends Migration { $table->id(); $table->integer('course_id')->unique(); $table->text('name'); + $table->text('course_duration')->nullable(); $table->text('thumbnail'); $table->text('link'); $table->enum('status', ['unfinished', 'upcoming', 'complete']); diff --git a/database/migrations/2022_08_18_094237_create_chapter_table.php b/database/migrations/2022_08_18_094237_create_chapter_table.php index 4cc0280..e8dad7f 100644 --- a/database/migrations/2022_08_18_094237_create_chapter_table.php +++ b/database/migrations/2022_08_18_094237_create_chapter_table.php @@ -14,8 +14,9 @@ return new class extends Migration { $table->integer('order'); $table->text('title'); $table->text('duration')->nullable(); - $table->text('link'); - $table->text('video_link'); + $table->text('link')->nullable(); + $table->text('video_link')->nullable(); + $table->boolean('sync_offline')->default(0); $table->integer('video_size'); $table->timestamps(); }); diff --git a/resources/views/course/index.blade.php b/resources/views/course/index.blade.php index 84b247f..11f3c89 100644 --- a/resources/views/course/index.blade.php +++ b/resources/views/course/index.blade.php @@ -3,12 +3,25 @@ {{ $course->name }} + Sync all chapters offline + + + + + + + @foreach($course->chapters()->get() as $chapter) + @endforeach + + + +
TitleDurationSync offline
{{ $chapter->title }}{{ $chapter->sync_offline?'Yes':'No' }} {{ $chapter->duration }}
Total: {{ $course->course_duration }}
diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 08d3f54..1026cce 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -14,7 +14,7 @@ {{ $course->id }} {{ $course->name }} - {{ $course->name }} + {{ $course->name }} {{ $course->status }} {{ $course->chapters_count }} / {{ $course->numberofchapters }} {{ $course->published_at?->diffForHumans() }} diff --git a/routes/web.php b/routes/web.php index 619edc8..ccecd2a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -16,6 +16,7 @@ use Illuminate\Support\Facades\Route; Route::get('/download', [\App\Http\Controllers\Index::class, 'download']); Route::get('/', [\App\Http\Controllers\Index::class, 'index']); Route::get('/course/{course}', [\App\Http\Controllers\CourseController::class, 'index'])->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');