Working interface and parsed symfony cast data to a database.
This commit is contained in:
@@ -2,39 +2,64 @@
|
||||
|
||||
namespace App\Http\SymfonyCastDl;
|
||||
|
||||
use App\Models\Chapter;
|
||||
use App\Models\Course;
|
||||
use GuzzleHttp\TransferStats;
|
||||
use DiDom\Document;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class SymfonyCastDlService
|
||||
{
|
||||
public function __construct(HtmlParser $htmlParser)
|
||||
public Client $client;
|
||||
|
||||
public function __construct(public HtmlParser $htmlParser)
|
||||
{
|
||||
$client = new Client([
|
||||
'base_uri' => "https://symfonycasts.com",
|
||||
$this->client = new Client([
|
||||
'base_uri' => config('symfonycast.base_url'),
|
||||
'cookies' => true
|
||||
]);
|
||||
|
||||
$response = $client->get('login');
|
||||
$response = $this->client->get('login');
|
||||
|
||||
|
||||
$token = $htmlParser->getCsrfToken($response);
|
||||
|
||||
$response = $client->post('login', [
|
||||
$response = $this->client->post('login', [
|
||||
'form_params' => [
|
||||
// 'email' => 'krzysiej@gmail.com',
|
||||
// 'password' => '',
|
||||
'email' => config('symfonycast.login'),
|
||||
'password' => config('symfonycast.password'),
|
||||
'_csrf_token' => $token
|
||||
],
|
||||
'on_stats' => function (TransferStats $stats) use (&$currentUrl) {
|
||||
$currentUrl = $stats->getEffectiveUri();
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
$coursePage = $client->get('courses/filtering');
|
||||
// dump($htmlParser->getCourses($coursePage));
|
||||
public function getInfo()
|
||||
{
|
||||
$coursePage = $this->client->get('courses/filtering');
|
||||
|
||||
$singleCoursePage = $client->get('screencast/api-platform');
|
||||
dd($htmlParser->getCourseDetails($singleCoursePage));
|
||||
$courses = $this->htmlParser->getCourses($coursePage);
|
||||
$courses->each(fn($course) => $course->save());
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
} catch (\GuzzleHttp\Exception\ClientException $exception) {
|
||||
|
||||
}
|
||||
return $chapter;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user