94 lines
3.0 KiB
PHP
94 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\SymfonyCastDl;
|
|
|
|
use App\Jobs\GetVideoFileSize;
|
|
use App\Models\Chapter;
|
|
use App\Models\Course;
|
|
use GuzzleHttp\TransferStats;
|
|
use GuzzleHttp\Client;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
|
|
class SymfonyCastDlService
|
|
{
|
|
public Client $client;
|
|
|
|
public function __construct(public HtmlParser $htmlParser)
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => config('symfonycast.base_url'),
|
|
'cookies' => true
|
|
]);
|
|
$response = $this->client->get('login');
|
|
$token = $htmlParser->getCsrfToken($response);
|
|
$this->client->post('login', [
|
|
'form_params' => [
|
|
'email' => config('symfonycast.login'),
|
|
'password' => config('symfonycast.password'),
|
|
'_csrf_token' => $token
|
|
],
|
|
'on_stats' => function (TransferStats $stats) use (&$currentUrl) {
|
|
$currentUrl = $stats->getEffectiveUri();
|
|
}
|
|
]);
|
|
}
|
|
|
|
public function getInfo(): void
|
|
{
|
|
$coursePage = $this->client->get('courses/filtering');
|
|
$courses = $this->htmlParser->getCourses($coursePage);
|
|
$courses->each->save();
|
|
/** @var Course $course */
|
|
foreach ($courses as $course) {
|
|
$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));
|
|
}
|
|
}
|
|
|
|
public function updateCourse(Course $course): void
|
|
{
|
|
$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));
|
|
}
|
|
|
|
public function getChapterInfo(Chapter $chapter): string
|
|
{
|
|
$course = $chapter->course;
|
|
$chapterPage = $this->client->get('/screencast/' . $course->link . DIRECTORY_SEPARATOR . $chapter->link);
|
|
return $this->htmlParser->getVideoSource($chapterPage);
|
|
}
|
|
|
|
public function videoSize(Chapter $chapter): Chapter
|
|
{
|
|
if (!$chapter->video_size) {
|
|
$response = $this->client->head($chapter->video_link);
|
|
if ($response->hasHeader('Content-Length')) {
|
|
$chapter->video_size = (int)$response->getHeader('Content-Length')[0];
|
|
$chapter->save();
|
|
}
|
|
}
|
|
return $chapter;
|
|
}
|
|
|
|
public function downloadFile(Chapter $chapter): void
|
|
{
|
|
if (is_null($chapter->video_link)) {
|
|
return;
|
|
}
|
|
if (!is_dir($chapter->directory_path)) {
|
|
mkdir(directory: $chapter->directory_path, recursive: true);
|
|
}
|
|
if (!$chapter->is_video_file) {
|
|
$this->client->request(
|
|
'GET',
|
|
$chapter->video_link,
|
|
['sink' => $chapter->video_path],
|
|
);
|
|
}
|
|
}
|
|
}
|