Started working on parsing symfony cast pages.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\SymfonyCastDl\HtmlParser;
|
||||
use App\Http\SymfonyCastDl\SymfonyCastDlService;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
@@ -10,4 +12,9 @@ use Illuminate\Routing\Controller as BaseController;
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
|
||||
public function index(HtmlParser $htmlParser)
|
||||
{
|
||||
$service = new SymfonyCastDlService($htmlParser);
|
||||
}
|
||||
}
|
||||
|
||||
48
app/Http/SymfonyCastDl/HtmlParser.php
Normal file
48
app/Http/SymfonyCastDl/HtmlParser.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\SymfonyCastDl;
|
||||
|
||||
use DiDom\Document;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
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): array
|
||||
{
|
||||
$courses = [];
|
||||
$document = new Document($response->getBody()->getContents());
|
||||
foreach ($document->find('div.js-course-item') as $courseItem) {
|
||||
$course = [];
|
||||
$course['name'] = $courseItem->first('h3')->text();
|
||||
$course['link'] = $courseItem->first('a')->attr('href');
|
||||
$course['status'] = $courseItem->attr('data-status');
|
||||
$course['chapter-count'] = $courseItem->attr('data-chapter-count');
|
||||
$course['times-watched'] = $courseItem->attr('data-times-watched');
|
||||
|
||||
$courses[] = $course;
|
||||
}
|
||||
return $courses;
|
||||
}
|
||||
|
||||
public function getCourseDetails(Response $response): array
|
||||
{
|
||||
$document = new Document($response->getBody()->getContents());
|
||||
$info = ['chapters' => []];
|
||||
foreach ($document->find('ul.chapter-list li') as $chapter) {
|
||||
$info['chapters'][] = [
|
||||
'link' => $chapter->first('a')->attr('href'),
|
||||
'title' => preg_replace('/\v(?:[\v\h]+)/', '', $chapter->first('.col')->text()),
|
||||
'duration' => $chapter->first('.length-styling')->text(),
|
||||
];
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
}
|
||||
40
app/Http/SymfonyCastDl/SymfonyCastDlService.php
Normal file
40
app/Http/SymfonyCastDl/SymfonyCastDlService.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\SymfonyCastDl;
|
||||
|
||||
use GuzzleHttp\TransferStats;
|
||||
use DiDom\Document;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class SymfonyCastDlService
|
||||
{
|
||||
public function __construct(HtmlParser $htmlParser)
|
||||
{
|
||||
$client = new Client([
|
||||
'base_uri' => "https://symfonycasts.com",
|
||||
'cookies' => true
|
||||
]);
|
||||
|
||||
$response = $client->get('login');
|
||||
|
||||
|
||||
$token = $htmlParser->getCsrfToken($response);
|
||||
|
||||
$response = $client->post('login', [
|
||||
'form_params' => [
|
||||
// 'email' => 'krzysiej@gmail.com',
|
||||
// 'password' => '',
|
||||
'_csrf_token' => $token
|
||||
],
|
||||
'on_stats' => function (TransferStats $stats) use (&$currentUrl) {
|
||||
$currentUrl = $stats->getEffectiveUri();
|
||||
}
|
||||
]);
|
||||
|
||||
$coursePage = $client->get('courses/filtering');
|
||||
// dump($htmlParser->getCourses($coursePage));
|
||||
|
||||
$singleCoursePage = $client->get('screencast/api-platform');
|
||||
dd($htmlParser->getCourseDetails($singleCoursePage));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user