- Portage des fonctionnalités de la branche main

- Ajout de node et npm dans la Dockerfile

- Ajout des Factories et Fixtures

- Ajout de npm-install dans Make install
This commit is contained in:
Jérémy Guillot
2024-06-03 19:41:24 +02:00
parent 41a1a8c44c
commit 291e85338a
53 changed files with 11825 additions and 18 deletions

View File

@@ -0,0 +1,209 @@
<?php
namespace App\Controller;
use App\Entity\Manga;
use App\Repository\MangaRepository;
use App\Service\MangaExportService;
use App\Service\LelScansProviderService;
use App\Service\MangaScraperService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\String\Slugger\AsciiSlugger;
class MangaController extends AbstractController
{
private MangaScraperService $mangaScraperService;
private MangaExportService $mangaExportService;
private LelScansProviderService $mangaProviderService;
private MangaRepository $mangaRepository;
public function __construct(MangaScraperService $mangaScraperService, MangaExportService $mangaExportService, LelScansProviderService $mangaProviderService, MangaRepository $mangaRepository)
{
$this->mangaScraperService = $mangaScraperService;
$this->mangaExportService = $mangaExportService;
$this->mangaProviderService = $mangaProviderService;
$this->mangaRepository = $mangaRepository;
}
#[Route('/manga', name: 'app_manga')]
public function index(): Response
{
// $this->breadcrumbs->addItem("Accueil", $this->generateUrl("app_manga"));
// $this->breadcrumbs->addItem("Mangas", $this->generateUrl("manga_show"));
$mangas = $this->mangaRepository->findAll();
return $this->render('manga/index.html.twig', [
'controller_name' => 'MangaController',
'mangas' => $mangas,
]);
}
#[Route('/manga/{mangaSlug}', name: 'manga_show')]
public function showChapters(string $mangaSlug): Response
{
$manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
if (!$manga) {
$manga = new Manga();
$manga->setSlug($mangaSlug);
$manga->setTitle($this->slugToTitle($mangaSlug));
$this->mangaRepository->save($manga, true);
}
$availableChapters = $this->mangaProviderService->getChapterList($mangaSlug);
return $this->render('manga/show_chapters.html.twig', [
'controller_name' => 'MangaController',
'manga' => $manga,
'availableChapters' => $availableChapters,
]);
}
#[Route('/manga/{mangaSlug}/{chapterNumber}/{pageNumber}', name: 'read_chapter_page')]
public function readChapterPage(string $mangaSlug, float $chapterNumber, int $pageNumber = 0): Response
{
$manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
if (!$manga) {
throw $this->createNotFoundException("Le manga demandé n'existe pas.");
}
$chapter = $manga->getChapterByNumber($chapterNumber);
if (!$chapter) {
throw $this->createNotFoundException("Le chapitre demandé n'existe pas.");
}
$currentPage = $chapter->getPageByNumber($pageNumber);
if (!$currentPage) {
throw $this->createNotFoundException("La page demandée n'existe pas.");
}
return $this->render('manga/manga_reader.html.twig', [
'manga' => $manga,
'chapter' => $chapter,
'pages' => $chapter->getPagesLink(),
'currentPage' => $currentPage,
]);
}
#[Route('/manga/{mangaSlug}/chapter/{chapterNumber}/download', name: 'download_chapter')]
public function downloadChapter(string $mangaSlug, float $chapterNumber): BinaryFileResponse
{
$response = $this->mangaExportService->downloadCbz($this->slugToTitle($mangaSlug), $chapterNumber);
if($response === false){
throw $this->createNotFoundException("Le chapitre demandé n'existe pas.");
}
// Définir les en-têtes pour le téléchargement
$response->headers->set('Content-Type', 'application/x-cbz');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
"{$mangaSlug}_{$chapterNumber}.cbz"
);
return $response;
}
#[Route('/scrape', name: 'manga_scrape', methods: 'POST')]
public function scrapeByMangaAndChapter(Request $request): Response
{
$mangaSlug = $request->request->get('mangaSlug');
$chapterNumber = $request->request->get('chapterNumber');
$response = $this->scrapeChapter($mangaSlug, $chapterNumber);
$manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
$availableChapters = $this->mangaProviderService->getChapterList($mangaSlug);
return $this->render('manga/show_chapters.html.twig', [
'controller_name' => 'MangaController',
'manga' => $manga,
'availableChapters' => $availableChapters,
]);
}
#[Route('/scrapeFrom', name: 'manga_scrape_from_chapter', methods: 'POST')]
public function scrapeByMangaFromChapter(Request $request): Response
{
$mangaSlug = $request->request->get('mangaSlug');
$chapterNumber = $request->request->get('chapterNumber');
do{
$response = $this->scrapeChapter($mangaSlug, $chapterNumber);
$chapterNumber++;
}while($response !== false);
$availableChapters = $this->mangaProviderService->getChapterList($mangaSlug);
return $this->redirectToRoute('manga_show', ['mangaSlug' => $mangaSlug, 'availableChapters' => $availableChapters]);
}
#[Route('/manga/exportFrom/{mangaSlug}/{chapterNumber}', name: 'manga_export')]
public function exportMangaCbz(string $mangaSlug, float $chapterNumber)
{
$response = $this->exportCbz($this->slugToTitle($mangaSlug), $chapterNumber);
dd($response);
}
#[Route('/getList', name: 'get_manga_list')]
public function getMangaList()
{
$list = $this->mangaProviderService->getMangaList();
}
private function scrapeChapter(string $mangaSlug, float $chapterNumber): array|bool
{
$url = 'https://lelscans.net/scan-' . $mangaSlug . '/' . $chapterNumber;
$manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
if(!is_null($manga)){
$scrapedManga = $this->mangaScraperService->scrapeMangaChapter($url, $manga->getTitle(), $chapterNumber);
}else{
$title = $this->slugToTitle($mangaSlug);
$manga = new Manga();
$manga->setTitle($title);
$manga->setSlug($mangaSlug);
$this->mangaRepository->save($manga);
$scrapedManga = $this->mangaScraperService->scrapeMangaChapter($url, $title, $chapterNumber);
}
return $scrapedManga;
}
private function exportCbz(string $mangaSlug, float $chapterNumber):array
{
$exported = [];
do{
$response = $this->mangaExportService->exportMangaChapter($mangaSlug, $chapterNumber);
if($response === 'already_exported'){
$exported[] = $mangaSlug . ' - ' . $chapterNumber . ' ' . $response;
}elseif($response === true){
$exported[] = $mangaSlug . ' - ' . $chapterNumber . ' exported';
}else{
$exported[] = $mangaSlug . ' - ' . $chapterNumber . ' something went wrong';
}
$chapterNumber++;
}while($response !== false);
return $exported;
}
private function slugToTitle(string $slug): string
{
$slugger = new AsciiSlugger();
$title = $slugger->slug($slug)->replace('-', ' ')->title(true)->toString();
return $title;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Controller;
use App\Repository\MangaRepository;
use App\Service\LelScansProviderService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\String\Slugger\AsciiSlugger;
class MenuController extends AbstractController
{
private MangaRepository $mangaRepository;
private LelScansProviderService $mangaProviderService;
public function __construct(MangaRepository $mangaRepository, LelScansProviderService $mangaProviderService)
{
$this->mangaRepository = $mangaRepository;
$this->mangaProviderService = $mangaProviderService;
}
public function menu(): Response
{
$availableManga = $this->mangaProviderService->getMangaList();
foreach($availableManga as $key => $manga) {
$availableManga[$key]['slug'] = $this->titleToSlug($manga['name']);
}
$mangas = $this->mangaRepository->findAll();
return $this->render('menu/menu.html.twig', [
'availableManga' => $availableManga,
'mangas' => $mangas,
]);
}
private function slugToTitle(string $slug): string
{
$slugger = new AsciiSlugger();
return $slugger->slug($slug)->replace('-', ' ')->title(true)->toString();
}
private function titleToSlug(string $title): string
{
$slugger = new AsciiSlugger();
return $slugger->slug($title)->lower()->toString();
}
}

View File

@@ -3,6 +3,9 @@
namespace App\DataFixtures;
use App\Factory\ApiTokenFactory;
use App\Factory\ChapterFactory;
use App\Factory\MangaFactory;
use App\Factory\PageFactory;
use App\Factory\UserFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
@@ -11,6 +14,32 @@ class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
UserFactory::createMany(20);
ApiTokenFactory::createMany(60, function () {
return [
'ownedBy' => UserFactory::random()
];
});
$mangas = MangaFactory::createMany(5);
foreach ($mangas as $manga) {
for ($i = 1; $i <= 10; $i++) {
$manga->addChapter(ChapterFactory::createOne([
'manga' => $manga,
'number' => $i
])->object());
}
foreach ($manga->getChapters() as $chapter) {
for ($i = 1; $i <= 15; $i++) {
$chapter->addPagesLink(PageFactory::createOne([
'chapter' => $chapter,
'number' => $i
])->object());
}
}
}
}
}

View File

@@ -10,7 +10,7 @@ use Random\RandomException;
class ApiToken
{
private const string PERSONAL_ACCESS_TOKEN_PREFIX = 'ipm_';
private const string PERSONAL_ACCESS_TOKEN_PREFIX = 'mgr_';
public const string SCOPE_USER_EDIT = 'ROLE_USER_EDIT';
public const string SCOPE_PROJECT_CREATE = 'ROLE_PROJECT_CREATE';
public const string SCOPE_PROJECT_EDIT = 'ROLE_PROJECT_EDIT';

120
src/Entity/Chapter.php Normal file
View File

@@ -0,0 +1,120 @@
<?php
namespace App\Entity;
use App\Repository\ChapterRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ChapterRepository::class)]
class Chapter
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?float $number = null;
#[ORM\Column]
private array $pages = [];
#[ORM\ManyToOne(inversedBy: 'chapters')]
#[ORM\JoinColumn(nullable: false)]
private ?Manga $manga = null;
#[ORM\OneToMany(mappedBy: 'chapter', targetEntity: Page::class, orphanRemoval: true)]
private Collection $pagesLink;
public function __construct()
{
$this->pagesLink = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?float
{
return $this->number;
}
public function setNumber(float $number): self
{
$this->number = $number;
return $this;
}
public function getPages(): array
{
return $this->pages;
}
public function setPages(array $pages): self
{
$this->pages = $pages;
return $this;
}
public function getManga(): ?Manga
{
return $this->manga;
}
public function setManga(?Manga $manga): self
{
$this->manga = $manga;
return $this;
}
/**
* @return Collection<int, Page>
*/
public function getPagesLink(): Collection
{
return $this->pagesLink;
}
public function addPagesLink(Page $pagesLink): self
{
if (!$this->pagesLink->contains($pagesLink)) {
$this->pagesLink->add($pagesLink);
$pagesLink->setChapter($this);
}
return $this;
}
public function removePagesLink(Page $pagesLink): self
{
if ($this->pagesLink->removeElement($pagesLink)) {
// set the owning side to null (unless already changed)
if ($pagesLink->getChapter() === $this) {
$pagesLink->setChapter(null);
}
}
return $this;
}
public function getPageByNumber(int $number): ?Page
{
/**
* @var Page $page
*/
foreach ($this->pagesLink as $page) {
if ($page->getNumber() === $number) {
return $page;
}
}
return null;
}
}

100
src/Entity/Manga.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
namespace App\Entity;
use App\Repository\MangaRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: MangaRepository::class)]
class Manga
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\OneToMany(mappedBy: 'manga', targetEntity: Chapter::class, orphanRemoval: true)]
private Collection $chapters;
#[ORM\Column(length: 255)]
private ?string $slug = null;
public function __construct()
{
$this->chapters = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection<int, Chapter>
*/
public function getChapters(): Collection
{
return $this->chapters;
}
public function getChapterByNumber(float $number): ?Chapter
{
foreach ($this->chapters as $chapter) {
if ($chapter->getNumber() === $number) {
return $chapter;
}
}
return null;
}
public function addChapter(Chapter $chapter): self
{
if (!$this->chapters->contains($chapter)) {
$this->chapters->add($chapter);
$chapter->setManga($this);
}
return $this;
}
public function removeChapter(Chapter $chapter): self
{
if ($this->chapters->removeElement($chapter)) {
// set the owning side to null (unless already changed)
if ($chapter->getManga() === $this) {
$chapter->setManga(null);
}
}
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}

81
src/Entity/Page.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
namespace App\Entity;
use App\Repository\PageRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PageRepository::class)]
class Page
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?int $number = null;
#[ORM\Column(length: 255)]
private ?string $imageUrl = null;
#[ORM\ManyToOne(inversedBy: 'pagesLink')]
#[ORM\JoinColumn(nullable: false)]
private ?Chapter $chapter = null;
#[ORM\Column(length: 255)]
private ?string $imageLocalUrl = null;
public function getId(): ?int
{
return $this->id;
}
public function getNumber(): ?int
{
return $this->number;
}
public function setNumber(int $number): self
{
$this->number = $number;
return $this;
}
public function getImageUrl(): ?string
{
return $this->imageUrl;
}
public function setImageUrl(string $imageUrl): self
{
$this->imageUrl = $imageUrl;
return $this;
}
public function getChapter(): ?Chapter
{
return $this->chapter;
}
public function setChapter(?Chapter $chapter): self
{
$this->chapter = $chapter;
return $this;
}
public function getImageLocalUrl(): ?string
{
return $this->imageLocalUrl;
}
public function setImageLocalUrl(string $imageLocalUrl): self
{
$this->imageLocalUrl = $imageLocalUrl;
return $this;
}
}

126
src/Entity/Source.php Normal file
View File

@@ -0,0 +1,126 @@
<?php
namespace App\Entity;
use App\Repository\SourceRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SourceRepository::class)]
class Source
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $name = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\Column(length: 255)]
private ?string $baseUrl = null;
#[ORM\Column(nullable: true)]
private array $scrappingParameters = [];
#[ORM\Column]
private ?bool $isActive = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getBaseUrl(): ?string
{
return $this->baseUrl;
}
public function setBaseUrl(string $baseUrl): self
{
$this->baseUrl = $baseUrl;
return $this;
}
public function getScrappingParameters(): array
{
return $this->scrappingParameters;
}
public function setScrappingParameters(?array $scrappingParameters): self
{
$this->scrappingParameters = $scrappingParameters;
return $this;
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\EventListener;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Entity\Page;
use App\Event\MangaScrapedEvent;
use Doctrine\ORM\EntityManagerInterface;
class MangaScrapedListener
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function onMangaScraped(MangaScrapedEvent $event): void
{
$mangaData = $event->getMangaData();
$manga = $this->entityManager->getRepository(Manga::class)->findOneBy(['title' => $mangaData['title']]);
if (!$manga) {
$manga = new Manga();
$manga->setTitle($mangaData['title']);
$this->entityManager->persist($manga);
}
$chapter = $manga->getChapterByNumber($mangaData['chapter']);
if (!$chapter) {
$chapter = (new Chapter())
->setNumber($mangaData['chapter']);
$manga->addChapter($chapter);
$this->entityManager->persist($chapter);
$this->entityManager->persist($manga);
}
foreach ($mangaData['pages'] as $pageData) {
$page = $chapter->getPageByNumber($pageData['page_number']);
if (!$page) {
$page = (new Page())
->setNumber($pageData['page_number'])
->setImageUrl($pageData['image_url'])
->setImageLocalUrl($pageData['local_image_url']);
$chapter->addPagesLink($page);
$this->entityManager->persist($chapter);
$this->entityManager->persist($page);
}
}
$this->entityManager->flush();
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\EventSubscriber;
use Symfony\Contracts\EventDispatcher\Event;
class MangaScrapedEvent extends Event
{
public const NAME = 'manga.scraped';
private string $mangaTitle;
private float $chapterNumber;
private array $pagesData;
public function __construct(string $mangaTitle, float $chapterNumber, array $pagesData)
{
$this->mangaTitle = $mangaTitle;
$this->chapterNumber = $chapterNumber;
$this->pagesData = $pagesData;
}
public function getMangaData(): array
{
return [
'title' => $this->mangaTitle,
'chapter' => $this->chapterNumber,
'pages' => $this->pagesData
];
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Factory;
use App\Entity\Chapter;
use App\Repository\ChapterRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<Chapter>
*
* @method Chapter|Proxy create(array|callable $attributes = [])
* @method static Chapter|Proxy createOne(array $attributes = [])
* @method static Chapter|Proxy find(object|array|mixed $criteria)
* @method static Chapter|Proxy findOrCreate(array $attributes)
* @method static Chapter|Proxy first(string $sortedField = 'id')
* @method static Chapter|Proxy last(string $sortedField = 'id')
* @method static Chapter|Proxy random(array $attributes = [])
* @method static Chapter|Proxy randomOrCreate(array $attributes = [])
* @method static ChapterRepository|RepositoryProxy repository()
* @method static Chapter[]|Proxy[] all()
* @method static Chapter[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Chapter[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Chapter[]|Proxy[] findBy(array $attributes)
* @method static Chapter[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Chapter[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class ChapterFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'manga' => MangaFactory::new(),
'number' => self::faker()->randomNumber(2),
'pages' => [],
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(Chapter $chapter): void {})
;
}
protected static function getClass(): string
{
return Chapter::class;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Factory;
use App\Entity\Manga;
use App\Repository\MangaRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<Manga>
*
* @method Manga|Proxy create(array|callable $attributes = [])
* @method static Manga|Proxy createOne(array $attributes = [])
* @method static Manga|Proxy find(object|array|mixed $criteria)
* @method static Manga|Proxy findOrCreate(array $attributes)
* @method static Manga|Proxy first(string $sortedField = 'id')
* @method static Manga|Proxy last(string $sortedField = 'id')
* @method static Manga|Proxy random(array $attributes = [])
* @method static Manga|Proxy randomOrCreate(array $attributes = [])
* @method static MangaRepository|RepositoryProxy repository()
* @method static Manga[]|Proxy[] all()
* @method static Manga[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Manga[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Manga[]|Proxy[] findBy(array $attributes)
* @method static Manga[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Manga[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class MangaFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'slug' => self::faker()->text(255),
'title' => self::faker()->text(255),
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(Manga $manga): void {})
;
}
protected static function getClass(): string
{
return Manga::class;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Factory;
use App\Entity\Page;
use App\Repository\PageRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<Page>
*
* @method Page|Proxy create(array|callable $attributes = [])
* @method static Page|Proxy createOne(array $attributes = [])
* @method static Page|Proxy find(object|array|mixed $criteria)
* @method static Page|Proxy findOrCreate(array $attributes)
* @method static Page|Proxy first(string $sortedField = 'id')
* @method static Page|Proxy last(string $sortedField = 'id')
* @method static Page|Proxy random(array $attributes = [])
* @method static Page|Proxy randomOrCreate(array $attributes = [])
* @method static PageRepository|RepositoryProxy repository()
* @method static Page[]|Proxy[] all()
* @method static Page[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Page[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Page[]|Proxy[] findBy(array $attributes)
* @method static Page[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Page[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class PageFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'chapter' => ChapterFactory::new(),
'imageLocalUrl' => self::faker()->text(255),
'imageUrl' => self::faker()->text(255),
'number' => self::faker()->randomNumber(2),
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(Page $page): void {})
;
}
protected static function getClass(): string
{
return Page::class;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Factory;
use App\Entity\Source;
use App\Repository\SourceRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<Source>
*
* @method Source|Proxy create(array|callable $attributes = [])
* @method static Source|Proxy createOne(array $attributes = [])
* @method static Source|Proxy find(object|array|mixed $criteria)
* @method static Source|Proxy findOrCreate(array $attributes)
* @method static Source|Proxy first(string $sortedField = 'id')
* @method static Source|Proxy last(string $sortedField = 'id')
* @method static Source|Proxy random(array $attributes = [])
* @method static Source|Proxy randomOrCreate(array $attributes = [])
* @method static SourceRepository|RepositoryProxy repository()
* @method static Source[]|Proxy[] all()
* @method static Source[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Source[]|Proxy[] createSequence(iterable|callable $sequence)
* @method static Source[]|Proxy[] findBy(array $attributes)
* @method static Source[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static Source[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class SourceFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'baseUrl' => self::faker()->text(255),
'createdAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
'isActive' => self::faker()->boolean(),
'updatedAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(Source $source): void {})
;
}
protected static function getClass(): string
{
return Source::class;
}
}

View File

@@ -54,7 +54,6 @@ final class UserFactory extends ModelFactory
'roles' => [],
'firstName' => self::faker()->randomElement(self::FIRST_NAMES),
'lastName' => self::faker()->randomElement(self::LAST_NAMES),
'company' => CompanyFactory::randomOrCreate(),
];
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Chapter;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Chapter>
*
* @method Chapter|null find($id, $lockMode = null, $lockVersion = null)
* @method Chapter|null findOneBy(array $criteria, array $orderBy = null)
* @method Chapter[] findAll()
* @method Chapter[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ChapterRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Chapter::class);
}
public function save(Chapter $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Chapter $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Chapter[] Returns an array of Chapter objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('c.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Chapter
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Manga;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Manga>
*
* @method Manga|null find($id, $lockMode = null, $lockVersion = null)
* @method Manga|null findOneBy(array $criteria, array $orderBy = null)
* @method Manga[] findAll()
* @method Manga[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class MangaRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Manga::class);
}
public function save(Manga $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Manga $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Manga[] Returns an array of Manga objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('m.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Manga
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Page;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Page>
*
* @method Page|null find($id, $lockMode = null, $lockVersion = null)
* @method Page|null findOneBy(array $criteria, array $orderBy = null)
* @method Page[] findAll()
* @method Page[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PageRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Page::class);
}
public function save(Page $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Page $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Page[] Returns an array of Page objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('p.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Page
// {
// return $this->createQueryBuilder('p')
// ->andWhere('p.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Source;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Source>
*
* @method Source|null find($id, $lockMode = null, $lockVersion = null)
* @method Source|null findOneBy(array $criteria, array $orderBy = null)
* @method Source[] findAll()
* @method Source[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class SourceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Source::class);
}
public function save(Source $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Source $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Source[] Returns an array of Source objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('s.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Source
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Service;
use Symfony\Component\BrowserKit\HttpBrowser as Client;
use Symfony\Component\DomCrawler\Crawler;
class LelScansProviderService implements MangaProviderInterface
{
const PROVIDER_URL = 'https://lelscans.net/';
const MANGA_SLUG = '/{manga}/{chapter}/{page}';
private Client $client;
public function __construct()
{
$this->client = new Client();
}
public function getMangaList(): array
{
$crawler = $this->client->request('GET', self::PROVIDER_URL);
$mangaList = [];
$crawler->filter('select > option')->each(function (Crawler $node) use (&$mangaList) {
$mangaName = $node->text();
$mangaUrl = $node->attr('value');
if ($mangaName && $mangaUrl && !preg_match('/^\d+(\.\d+)?$/', $mangaName)) {
$mangaList[] = [
'name' => $mangaName,
'url' => $mangaUrl,
];
}
});
return $mangaList;
}
public function getChapterList($mangaSlug): array
{
$crawler = $this->client->request('GET', self::PROVIDER_URL . 'lecture-en-ligne-' . $mangaSlug . '.php');
$chapterList = [];
$crawler->filter('select > option')->each(function (Crawler $node) use (&$chapterList) {
$chapterName = $node->text();
$chapterUrl = $node->attr('value');
if ($chapterName && $chapterUrl && preg_match('/^\d+(\.\d+)?$/', $chapterName)) {
$chapterList[] = [
'number' => $chapterName,
];
}
});
return $chapterList;
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Service;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use ZipArchive;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class MangaExportService
{
const IMG_BASE_DIR = '/public/manga-images';
const EXPORT_BASE_DIR = '/public/manga-export';
private string $projectDir;
public function __construct($projectDir)
{
$this->projectDir = $projectDir;
}
public function exportMangaChapter(string $mangaTitle, int $chapterNumber): bool|string
{
$chapterDir = $this->getMangaDir($mangaTitle, $chapterNumber);
$cbzFilePath = $this->getExportDir($mangaTitle, $chapterNumber);
if(!is_dir($chapterDir)){
return false;
}
$cbzDirectory = dirname($cbzFilePath);
if (!is_dir($cbzDirectory)) {
mkdir($cbzDirectory, 0755, true);
}
$fileSystem = new Filesystem();
if($fileSystem->exists($cbzFilePath)){
return 'already_exported';
}
return $this->createCbzFromDirectory($chapterDir, $cbzFilePath);
}
public function downloadCbz(string $mangaTitle, int $chapterNumber): BinaryFileResponse|bool
{
$filePathCbz = $this->getExportDir($mangaTitle, $chapterNumber);
$fileSystem = new Filesystem();
if($fileSystem->exists($filePathCbz)){
return new BinaryFileResponse($filePathCbz);
}
$chapterDir = $this->getMangaDir($mangaTitle, $chapterNumber);
if(is_dir($chapterDir)){
if($this->exportMangaChapter($mangaTitle, $chapterNumber)){
return new BinaryFileResponse($filePathCbz);
}
}
return false;
}
private function createCbzFromDirectory(string $sourceDirectory, string $cbzFilePath): bool
{
$zip = new ZipArchive();
// Ouvre le fichier .cbz en écriture
if ($zip->open($cbzFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return false;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceDirectory),
RecursiveIteratorIterator::LEAVES_ONLY
);
// Ajoute les fichiers d'image au fichier .cbz
foreach ($files as $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($sourceDirectory) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return true;
}
private function getMangaDir(string $mangaTitle, int $chapterNumber): string
{
return sprintf('%s/%s/%d', $this->projectDir . self::IMG_BASE_DIR, $mangaTitle, $chapterNumber);
}
private function getExportDir(string $mangaTitle, int $chapterNumber): string
{
return sprintf('%s/%s/%d', $this->projectDir . self::EXPORT_BASE_DIR, $mangaTitle, $chapterNumber) . '.cbz';
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Service;
class MangaProviderFactory
{
public static function create($providerName): MangaProviderInterface
{
return match ($providerName) {
'LelScans' => new LelScansProviderService(),
'AutreManga' => new AutreMangaProviderService(),
default => throw new \Exception("Provider {$providerName} non supporté."),
};
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace App\Service;
interface MangaProviderInterface
{
public function getMangaList(): array;
public function getChapterList(string $mangaSlug): array;
}

View File

@@ -0,0 +1,145 @@
<?php
namespace App\Service;
use App\Event\MangaScrapedEvent;
use GuzzleHttp\Client;
use PHPUnit\Util\PHP\AbstractPhpProcess;
use Psr\Container\ContainerInterface;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class MangaScraperService
{
const IMG_BASE_DIR = '/public/manga-images';
private string $projectDir;
private EventDispatcherInterface $eventDispatcher;
public function __construct($projectDir, EventDispatcherInterface $eventDispatcher)
{
$this->projectDir = $projectDir;
$this->eventDispatcher = $eventDispatcher;
}
public function extractMangaPageData(string $html): array
{
$baseUrl = 'https://lelscans.net';
$crawler = new Crawler($html);
$imgUrl = $crawler->filter('img')->attr('src');
$nextLink = $crawler->filter('a[title="Suivant"]');
if (!preg_match('/^https?:\/\//', $imgUrl)) {
$urlComponents = parse_url($baseUrl);
$scheme = $urlComponents['scheme'];
$host = $urlComponents['host'];
// Construit l'URL absolue de l'image
$imgUrl = $scheme . '://' . $host . '/' . ltrim($imgUrl, '/');
}
if($nextLink->count() > 0){
$nextUrl = $nextLink->attr('href');
}else{
$nextUrl = null;
}
return [
'image_url' => $imgUrl,
'next_page_url' => $nextUrl,
];
}
public function scrapeMangaChapter(string $chapterUrl, string $mangaTitle, float $chapterNumber): array|bool
{
if(!$this->isChapterAvailable($chapterUrl, $chapterNumber)){
return false;
}
$pageData = [];
$currentPageUrl = $chapterUrl;
$mangaDir = sprintf('%s/%s', $this->projectDir . self::IMG_BASE_DIR, $mangaTitle);
if (!is_dir($mangaDir)) {
mkdir($mangaDir, 0755, true);
}
// Créez le dossier du chapitre s'il n'existe pas
$chapterDir = sprintf('%s/%s', $mangaDir, $chapterNumber);
if (!is_dir($chapterDir)) {
mkdir($chapterDir, 0755, true);
}
do {
$html = $this->fetchHtml($currentPageUrl);
$page = $this->extractMangaPageData($html);
$pageData[] = $page;
$currentPageUrl = $page['next_page_url'];
// Construisez le nom de fichier de l'image
$imageName = sprintf('%03d.jpg', count($pageData));
// Construisez le chemin du fichier de l'image
$imagePath = sprintf('%s/%s', $chapterDir, $imageName);
// Téléchargez et enregistrez l'image
$this->downloadAndSaveImage($page['image_url'], $imagePath);
// Modifiez les données de la page pour inclure l'URL de l'image stockée localement
$pageData[count($pageData) - 1]['local_image_url'] = sprintf('/manga-images/%s/%s/%s', $mangaTitle, $chapterNumber, $imageName);
$pageData[count($pageData) - 1]['page_number'] = count($pageData);
} while ($currentPageUrl);
$event = new MangaScrapedEvent($mangaTitle, $chapterNumber, $pageData);
$this->eventDispatcher->dispatch($event, MangaScrapedEvent::NAME);
return $pageData;
}
private function fetchHtml(string $url): string
{
$client = new Client();
$response = $client->get($url);
return (string) $response->getBody();
}
private function downloadAndSaveImage(string $imageUrl, string $destinationPath): void
{
$client = new Client();
$response = $client->get($imageUrl);
file_put_contents($destinationPath, $response->getBody()->getContents());
}
private function isChapterAvailable(string $chapterUrl, float $chapterNumber): bool
{
$html = $this->fetchHtml($chapterUrl);
$crawler = new Crawler($html);
$nextLink = $crawler->filter('a[title="Suivant"]');
if($nextLink->count() === 0){
return false;
}else{
$nextUrl = $nextLink->attr('href');
}
$routeCollection = new RouteCollection();
$routeCollection->add('manga_chapter', new Route('/scan-{manga}/{chapter}/{page}'));
$context = new RequestContext('/');
$matcher = new UrlMatcher($routeCollection, $context);
$path = parse_url($nextUrl, PHP_URL_PATH);
$parameters = $matcher->match($path);
if((float) $parameters['chapter'] !== $chapterNumber){
return false;
}
return true;
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Service;
use Goutte\Client;
class SushiScanProviderService implements MangaProviderInterface
{
const PROVIDER_URL = 'https://sushiscan.com/';
const MANGA_SLUG = '/{manga}/{chapter}/{page}';
private Client $client;
public function __construct()
{
$this->client = new Client();
}
/**
* @return array
*/
public function getMangaList(): array
{
// TODO: Implement getMangaList() method.
}
/**
* @param string $mangaSlug
* @return array
*/
public function getChapterList(string $mangaSlug): array
{
// TODO: Implement getChapterList() method.
}
}