- Gettings chapters from non En/Fr sources
- mercure fonctionne!
This commit is contained in:
Jérémy Guillot
2024-06-16 13:14:32 +02:00
parent bc85649789
commit 671551c7f8
11 changed files with 313 additions and 65 deletions

View File

@@ -10,6 +10,7 @@ use App\Service\LelScansProviderService;
use App\Service\MangaScraperService;
use App\Service\NotificationService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
@@ -17,7 +18,7 @@ use Symfony\Component\Messenger\Attribute\AsMessageHandler;
readonly class DownloadChapterHandler
{
public function __construct(
private ChapterRepository $chapterRepository,
private ChapterRepository $chapterRepository,
private MangaScraperService $mangaScraperService,
private NotificationService $notificationService
)
@@ -34,24 +35,52 @@ readonly class DownloadChapterHandler
if (!$chapter) {
$this->notificationService->sendUpdate('notification', ['status' => 'error', 'message' => 'Chapter not found.']);
throw new BadRequestHttpException('Chapter not found');
}elseif ($chapter->getLocalPath() !== null){
} elseif ($chapter->getLocalPath() !== null) {
$this->notificationService->sendUpdate('notification', ['status' => 'error', 'message' => 'Chapter already scraped.']);
throw new BadRequestHttpException('Chapter already downloaded');
}
$lelScanSource = new ContentSource();
$lelScanSource->setBaseUrl('https://lelscans.net')
->setImageSelector('#image img')
->setChapterUrlFormat('https://lelscans.net/scan-%s/%s')
->setNextPageSelector('a[title="Suivant"]')
->setScrapingType('html');
$sources = [
(new ContentSource())
->setBaseUrl('https://lelscans.net')
->setImageSelector('#image img')
->setChapterUrlFormat('https://lelscans.net/scan-%s/%s')
->setNextPageSelector('a[title="Suivant"]')
->setScrapingType('html'),
try {
$this->mangaScraperService->scrapeChapter($chapter, $lelScanSource);
} catch (Exception $e) {
$this->notificationService->sendUpdate('notification', ['status' => 'error', 'message' => 'An error occurred while scraping the chapter.']);
throw new Exception('Error scraping chapter: ' . $e->getMessage());
(new ContentSource())
->setBaseUrl('https://api.mangadex.org/')
->setImageSelector('img')
->setChapterUrlFormat('at-home/server/%s')
->setScrapingType('mangadex')
];
$scrapedSuccessfully = false;
foreach ($sources as $source) {
try {
$this->mangaScraperService->scrapeChapter($chapter, $source);
$scrapedSuccessfully = true;
break;
} catch (Exception $e) {
$this->notificationService->sendUpdate('notification', [
'status' => 'warning',
'message' => 'An error occurred while scraping with source: ' . $source->getBaseUrl() . '. Trying next source...'
]);
} catch (GuzzleException $e) {
}
}
if (!$scrapedSuccessfully) {
$this->notificationService->sendUpdate('notification', [
'status' => 'error',
'message' => 'All sources failed to scrape the chapter ' . $chapter->getManga()->getTitle() . ' ' . $chapter->getNumber() . '.'
]);
throw new Exception('All sources failed to scrape the chapter ' . $chapter->getManga()->getTitle() . ' ' . $chapter->getNumber() . '.');
}
$this->notificationService->sendUpdate('notification', ['status' => 'success', 'message' => 'Chapter scraped successfully.']);
}
}