Added:
- AdditionnalData for buttons - refresh manga metadata and chapters
This commit is contained in:
@@ -18,18 +18,18 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
|
||||
public function search(?string $title): Collection
|
||||
{
|
||||
if($title === null) {
|
||||
if ($title === null) {
|
||||
return new ArrayCollection();
|
||||
}
|
||||
|
||||
try{
|
||||
try {
|
||||
$results = $this->client->get('/manga', [
|
||||
'title' => $title,
|
||||
'contentRating' => ['safe', 'suggestive'],
|
||||
'includes' => ['cover_art', 'author'],
|
||||
'limit' => 25
|
||||
]);
|
||||
}catch(\Exception $e){
|
||||
} catch (\Exception $e) {
|
||||
$this->notificationService->sendUpdate('notification', ['status' => 'error', 'message' => 'An error occurred while fetching data from Mangadex.']);
|
||||
return new ArrayCollection();
|
||||
}
|
||||
@@ -42,22 +42,21 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
->setSlug($this->slugger->slug($result['attributes']['title']['en'])->lower())
|
||||
->setDescription($result['attributes']['description']['fr'] ?? $result['attributes']['description']['en'] ?? '')
|
||||
->setPublicationYear($result['attributes']['year'])
|
||||
->setStatus($result['attributes']['status'])
|
||||
;
|
||||
->setStatus($result['attributes']['status']);
|
||||
$tags = [];
|
||||
foreach($result['attributes']['tags'] as $tag){
|
||||
foreach ($result['attributes']['tags'] as $tag) {
|
||||
$tags[] = $tag['attributes']['name']['en'];
|
||||
}
|
||||
|
||||
$mangas[count($mangas) - 1]->setGenres($tags);
|
||||
|
||||
foreach($result['relationships'] as $relationship) {
|
||||
if($relationship['type'] === 'author') {
|
||||
foreach ($result['relationships'] as $relationship) {
|
||||
if ($relationship['type'] === 'author') {
|
||||
$mangas[count($mangas) - 1]->setAuthor($relationship['attributes']['name']);
|
||||
}
|
||||
|
||||
if($relationship['type'] === 'cover_art') {
|
||||
$mangas[count($mangas) - 1]->setImageUrl('https://mangadex.org/covers/' . $result['id'] . '/' .$relationship['attributes']['fileName']);
|
||||
if ($relationship['type'] === 'cover_art') {
|
||||
$mangas[count($mangas) - 1]->setImageUrl('https://mangadex.org/covers/' . $result['id'] . '/' . $relationship['attributes']['fileName']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +67,7 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
'manga' => $test
|
||||
]);
|
||||
|
||||
foreach($mangas as $manga) {
|
||||
foreach ($mangas as $manga) {
|
||||
$manga->setRating($ratings['statistics'][$manga->getExternalId()]['rating']['average']);
|
||||
}
|
||||
|
||||
@@ -77,12 +76,11 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
|
||||
public function getFeed(Manga $manga): array
|
||||
{
|
||||
if($manga->getExternalId() === null) {
|
||||
if ($manga->getExternalId() === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$chapters = [];
|
||||
$chapterEntities = [];
|
||||
$page = 0;
|
||||
|
||||
do {
|
||||
@@ -95,44 +93,40 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
$page++;
|
||||
} while (count($chapters) < $results['total']);
|
||||
|
||||
foreach($chapters as $result) {
|
||||
$chapterNumber = (float)$result['attributes']['chapter'];
|
||||
|
||||
// Utilisez la méthode exists de Doctrine pour vérifier si un chapitre avec le même numéro existe déjà
|
||||
$chapterExists = $manga->getChapters()->exists(function($key, $existingChapter) use ($chapterNumber) {
|
||||
return $existingChapter->getNumber() === $chapterNumber;
|
||||
});
|
||||
|
||||
// Si le chapitre existe déjà, on skip
|
||||
if ($chapterExists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Créez et ajoutez le nouveau chapitre
|
||||
$chapter = new Chapter();
|
||||
$chapter->setNumber($chapterNumber)
|
||||
->setTitle($result['attributes']['title'])
|
||||
->setVolume((int)$result['attributes']['volume'] ?? null)
|
||||
->setExternalId($result['id'])
|
||||
;
|
||||
|
||||
$chapterEntities[] = $chapter;
|
||||
// $manga->addChapter($chapter);
|
||||
}
|
||||
|
||||
return $chapterEntities;
|
||||
return $this->getChaptersFromFeed($chapters, $manga);
|
||||
}
|
||||
|
||||
private function getFeedWithPagination(string $externalId, int $page): array
|
||||
public function getLastFeed(Manga $manga, int $limit = 100): array
|
||||
{
|
||||
if ($manga->getExternalId() === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$chapters = [];
|
||||
|
||||
try {
|
||||
$results = $this->getFeedWithPagination($manga->getExternalId(), 0, $limit, 'desc');
|
||||
if (isset($results['data'])) {
|
||||
$chapters = $results['data'];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->notificationService->sendUpdate(['status' => 'error', 'message' => 'An error occurred while fetching recent chapters from Mangadex.']);
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->getChaptersFromFeed($chapters, $manga);
|
||||
}
|
||||
|
||||
private function getFeedWithPagination(string $externalId, int $page, int $limit = 500, string $order = 'asc'): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get('/manga/' . $externalId . '/feed', [
|
||||
'limit' => 500,
|
||||
'translatedLanguage' =>['en', 'fr'],
|
||||
'order' => ['chapter' => 'asc'],
|
||||
'offset' => $page * 500
|
||||
'limit' => $limit,
|
||||
'translatedLanguage' => ['en', 'fr'],
|
||||
'order' => ['chapter' => $order],
|
||||
'offset' => $page * $limit
|
||||
]);
|
||||
}catch(\Exception $e){
|
||||
} catch (\Exception $e) {
|
||||
$this->notificationService->sendUpdate(['status' => 'error', 'message' => 'An error occurred while fetching data from Mangadex.']);
|
||||
return [];
|
||||
}
|
||||
@@ -142,24 +136,24 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
|
||||
public function getMangaAggregate(Manga $manga): array
|
||||
{
|
||||
if($manga->getExternalId() === null) {
|
||||
if ($manga->getExternalId() === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client->get('/manga/' . $manga->getExternalId() . '/aggregate');
|
||||
}catch(\Exception $e){
|
||||
} catch (\Exception $e) {
|
||||
// $this->notificationService->sendUpdate(['status' => 'error', 'message' => 'An error occurred while fetching data from Mangadex.']);
|
||||
return [];
|
||||
}
|
||||
|
||||
$chapterEntities = [];
|
||||
if($response['result'] === 'ok'){
|
||||
foreach($response['volumes'] as $volume){
|
||||
$volumeNumber = $volume['volume'] === 'none' ? 0 : (float) $volume['volume'];
|
||||
foreach($volume['chapters'] as $chapter){
|
||||
if ($response['result'] === 'ok') {
|
||||
foreach ($response['volumes'] as $volume) {
|
||||
$volumeNumber = $volume['volume'] === 'none' ? 0 : (float)$volume['volume'];
|
||||
foreach ($volume['chapters'] as $chapter) {
|
||||
$chapterEntity = new Chapter();
|
||||
$chapterEntity->setNumber((float) $chapter['chapter'])
|
||||
$chapterEntity->setNumber((float)$chapter['chapter'])
|
||||
->setTitle('Chapter ' . $chapter['chapter'])
|
||||
->setVolume($volumeNumber)
|
||||
->setExternalId('');
|
||||
@@ -171,4 +165,89 @@ readonly class MangadexProvider implements MetadataProviderInterface
|
||||
}
|
||||
return $chapterEntities;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $chapters
|
||||
* @param Manga $manga
|
||||
* @param array $chapterEntities
|
||||
* @return array
|
||||
*/
|
||||
public function getChaptersFromFeed(mixed $chapters, Manga $manga): array
|
||||
{
|
||||
$chapterEntities = [];
|
||||
$uniqueChapterNumbers = [];
|
||||
|
||||
foreach ($chapters as $result) {
|
||||
$chapterNumber = (float)$result['attributes']['chapter'];
|
||||
|
||||
// Vérifiez si le chapitre existe déjà dans la base de données
|
||||
$chapterExists = $manga->getChapters()->exists(function ($key, $existingChapter) use ($chapterNumber) {
|
||||
return $existingChapter->getNumber() === $chapterNumber;
|
||||
});
|
||||
|
||||
// Si le chapitre existe déjà dans la base de données ou dans notre nouvelle liste, on skip
|
||||
if ($chapterExists || in_array($chapterNumber, $uniqueChapterNumbers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Créez et ajoutez le nouveau chapitre
|
||||
$chapter = new Chapter();
|
||||
$chapter->setNumber($chapterNumber)
|
||||
->setTitle($result['attributes']['title'])
|
||||
->setVolume((int)$result['attributes']['volume'] ?? null)
|
||||
->setExternalId($result['id']);
|
||||
|
||||
$chapterEntities[] = $chapter;
|
||||
$uniqueChapterNumbers[] = $chapterNumber;
|
||||
}
|
||||
|
||||
// Trier les chapitres par numéro
|
||||
usort($chapterEntities, function ($a, $b) {
|
||||
return $a->getNumber() <=> $b->getNumber();
|
||||
});
|
||||
|
||||
return $chapterEntities;
|
||||
}
|
||||
|
||||
public function addAllChaptersToManga(Manga $manga): array
|
||||
{
|
||||
$mangaFeed = $this->getFeed($manga);
|
||||
$mangaAggregate = $this->getMangaAggregate($manga);
|
||||
|
||||
$allChapters = array_merge($mangaFeed, $mangaAggregate);
|
||||
|
||||
if (empty($allChapters)) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'status' => 'error',
|
||||
'message' => 'No chapters found for this manga.'
|
||||
]);
|
||||
return [];
|
||||
}
|
||||
|
||||
$mergedChapters = [];
|
||||
foreach ($allChapters as $chapter) {
|
||||
$number = $chapter->getNumber();
|
||||
|
||||
$existingChapter = $manga->getChapterByNumber($number);
|
||||
if ($existingChapter) {
|
||||
if ($existingChapter->getExternalId() !== $chapter->getExternalId() && is_null($existingChapter->getExternalId())) {
|
||||
$this->updateChapter($existingChapter, $chapter);
|
||||
$mergedChapters[$number] = $existingChapter;
|
||||
}
|
||||
} else {
|
||||
// Add new chapter
|
||||
$manga->addChapter($chapter);
|
||||
$mergedChapters[$number] = $chapter;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($mergedChapters);
|
||||
}
|
||||
|
||||
private function updateChapter(Chapter $existingChapter, Chapter $newChapter): void
|
||||
{
|
||||
$existingChapter->setVolume($newChapter->getVolume());
|
||||
$existingChapter->setExternalId($newChapter->getExternalId());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user