70 lines
2.8 KiB
PHP
70 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\SymfonyCastDl;
|
|
|
|
use App\Models\Chapter;
|
|
use App\Models\Course;
|
|
use Carbon\Carbon;
|
|
use DiDom\Document;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class HtmlParser
|
|
{
|
|
public function getCsrfToken(Response $response): string
|
|
{
|
|
$document = new Document($response->getBody()->getContents());
|
|
return $document->first('input[name="_csrf_token"]')->attr('value');
|
|
}
|
|
|
|
public function getCourses(Response $response): Collection
|
|
{
|
|
$courses = new Collection();
|
|
$document = new Document($response->getBody()->getContents());
|
|
foreach ($document->find('div.js-course-item') as $courseItem) {
|
|
$courseId = $courseItem->attr('data-id');
|
|
$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 = 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');
|
|
$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;
|
|
$courses->add($course);
|
|
}
|
|
return $courses;
|
|
}
|
|
|
|
public function getCourseDetails(Response $response, int $courseId): Collection
|
|
{
|
|
$document = new Document($response->getBody()->getContents());
|
|
$chapters = new Collection();
|
|
$chapterId = 0;
|
|
foreach ($document->find('ul.chapter-list li') as $chapterItem) {
|
|
if ($chapterItem->first('.col')) {
|
|
$chapterId++;
|
|
|
|
$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 = last(explode('/', $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);
|
|
}
|
|
}
|
|
|
|
return $chapters;
|
|
}
|
|
|
|
}
|