- début refonte graphique
- début MangaDbProvider
This commit is contained in:
10
src/Service/MangaDbProviderInterface.php
Normal file
10
src/Service/MangaDbProviderInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
|
||||
interface MangaDbProviderInterface
|
||||
{
|
||||
public function search(string $title): Collection;
|
||||
}
|
||||
@@ -2,10 +2,9 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Event\MangaScrapedEvent;
|
||||
use App\EventSubscriber\MangaScrapedEvent;
|
||||
use GuzzleHttp\Client;
|
||||
use PHPUnit\Util\PHP\AbstractPhpProcess;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
@@ -15,7 +14,7 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class MangaScraperService
|
||||
{
|
||||
const IMG_BASE_DIR = '/public/manga-images';
|
||||
const string IMG_BASE_DIR = '/public/manga-images';
|
||||
private string $projectDir;
|
||||
private EventDispatcherInterface $eventDispatcher;
|
||||
|
||||
@@ -28,9 +27,10 @@ class MangaScraperService
|
||||
public function extractMangaPageData(string $html): array
|
||||
{
|
||||
$baseUrl = 'https://lelscans.net';
|
||||
|
||||
//pour éviter à PhpStorm de gueuler...
|
||||
$selector = 'img';
|
||||
$crawler = new Crawler($html);
|
||||
$imgUrl = $crawler->filter('img')->attr('src');
|
||||
$imgUrl = $crawler->filter($selector)->attr('src');
|
||||
$nextLink = $crawler->filter('a[title="Suivant"]');
|
||||
|
||||
if (!preg_match('/^https?:\/\//', $imgUrl)) {
|
||||
@@ -54,7 +54,10 @@ class MangaScraperService
|
||||
];
|
||||
}
|
||||
|
||||
public function scrapeMangaChapter(string $chapterUrl, string $mangaTitle, float $chapterNumber): array|bool
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function scrapeMangaChapter(string $chapterUrl, string $mangaTitle, float $chapterNumber): array|bool
|
||||
{
|
||||
if(!$this->isChapterAvailable($chapterUrl, $chapterNumber)){
|
||||
return false;
|
||||
@@ -101,7 +104,10 @@ class MangaScraperService
|
||||
return $pageData;
|
||||
}
|
||||
|
||||
private function fetchHtml(string $url): string
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
private function fetchHtml(string $url): string
|
||||
{
|
||||
$client = new Client();
|
||||
$response = $client->get($url);
|
||||
@@ -109,7 +115,10 @@ class MangaScraperService
|
||||
return (string) $response->getBody();
|
||||
}
|
||||
|
||||
private function downloadAndSaveImage(string $imageUrl, string $destinationPath): void
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
private function downloadAndSaveImage(string $imageUrl, string $destinationPath): void
|
||||
{
|
||||
$client = new Client();
|
||||
$response = $client->get($imageUrl);
|
||||
@@ -117,7 +126,10 @@ class MangaScraperService
|
||||
file_put_contents($destinationPath, $response->getBody()->getContents());
|
||||
}
|
||||
|
||||
private function isChapterAvailable(string $chapterUrl, float $chapterNumber): bool
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
private function isChapterAvailable(string $chapterUrl, float $chapterNumber): bool
|
||||
{
|
||||
$html = $this->fetchHtml($chapterUrl);
|
||||
$crawler = new Crawler($html);
|
||||
@@ -142,4 +154,4 @@ class MangaScraperService
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
67
src/Service/MangaUpdatesDbProvider.php
Normal file
67
src/Service/MangaUpdatesDbProvider.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Manga;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Symfony\Component\BrowserKit\HttpBrowser;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class MangaUpdatesDbProvider implements MangaDbProviderInterface
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
public function __construct(private SluggerInterface $slugger)
|
||||
{
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function search(string $title): Collection
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('PUT', 'https://api.mangaupdates.com/v1/account/login', [
|
||||
'json' => [
|
||||
'username' => 'Colgora',
|
||||
'password' => '7TK5jv33NDn*SLV',
|
||||
]
|
||||
])
|
||||
->withHeader('Content-Type', 'application/json');
|
||||
|
||||
$jwt = json_decode($response->getBody()->getContents(), true)['context']['session_token'];
|
||||
|
||||
$results = $this->client->request('POST', 'https://api.mangaupdates.com/v1/series/search', [
|
||||
'json' => [
|
||||
'search' => $title,
|
||||
'orderby' => 'score',
|
||||
]
|
||||
])->withHeader('Authorization', 'Bearer ' . $jwt)
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->getBody()
|
||||
->getContents();
|
||||
|
||||
$mangas = [];
|
||||
foreach (json_decode($results, true)['results'] as $record) {
|
||||
$record = $record['record'];
|
||||
$mangas[] = (new Manga())
|
||||
->setTitle($record['title'])
|
||||
->setSlug($this->slugger->slug($record['title'])->lower())
|
||||
->setDescription($record['description'])
|
||||
->setImageUrl($record['image']['url']['original'])
|
||||
->setGenres($record['genres'])
|
||||
->setPublicationYear((int)$record['year']);
|
||||
}
|
||||
|
||||
return new ArrayCollection($mangas);
|
||||
} catch (GuzzleException $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user