From 4329864f364ef57d662dc37a94236c342b5930ab Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 26 Oct 2022 03:51:32 +0500 Subject: [PATCH] anime/user reviews: fix entry details --- src/Model/User/Reviews/UserAnimeReview.php | 2 +- src/Model/User/Reviews/UserMangaReview.php | 4 +- src/Parser/Reviews/AnimeReviewParser.php | 52 +++++++++++-------- src/Parser/Reviews/MangaReviewParser.php | 59 ++++++++++++---------- 4 files changed, 65 insertions(+), 52 deletions(-) diff --git a/src/Model/User/Reviews/UserAnimeReview.php b/src/Model/User/Reviews/UserAnimeReview.php index dd2b9d99..518c418e 100644 --- a/src/Model/User/Reviews/UserAnimeReview.php +++ b/src/Model/User/Reviews/UserAnimeReview.php @@ -29,7 +29,7 @@ public static function fromParser(AnimeReviewParser $parser): UserAnimeReview { $instance = new self(); - $instance->entry = $parser->getAnime(); + $instance->entry = $parser->getAnime('user'); $instance->malId = $parser->getId(); $instance->url = $parser->getUrl(); $instance->type = $parser->getType(); diff --git a/src/Model/User/Reviews/UserMangaReview.php b/src/Model/User/Reviews/UserMangaReview.php index ef13d910..2f7af9c7 100644 --- a/src/Model/User/Reviews/UserMangaReview.php +++ b/src/Model/User/Reviews/UserMangaReview.php @@ -18,7 +18,7 @@ class UserMangaReview extends MangaReview * @var MangaMeta */ private MangaMeta $entry; - + /** * @param MangaReviewParser $parser * @return UserAnimeReview @@ -28,7 +28,7 @@ public static function fromParser(MangaReviewParser $parser): UserMangaReview { $instance = new self(); - $instance->entry = $parser->getManga(); + $instance->entry = $parser->getManga('user'); $instance->malId = $parser->getId(); $instance->url = $parser->getUrl(); $instance->type = $parser->getType(); diff --git a/src/Parser/Reviews/AnimeReviewParser.php b/src/Parser/Reviews/AnimeReviewParser.php index 355b7297..5cf04372 100644 --- a/src/Parser/Reviews/AnimeReviewParser.php +++ b/src/Parser/Reviews/AnimeReviewParser.php @@ -2,6 +2,7 @@ namespace Jikan\Parser\Reviews; +use Jikan\Exception\ParserException; use Jikan\Helper\JString; use Jikan\Helper\Parser; use Jikan\Model\Anime\AnimeReview; @@ -47,13 +48,20 @@ public function getModel(): AnimeReview /** * @return AnimeMeta */ - public function getAnime() : AnimeMeta + public function getAnime(string $page = null) : AnimeMeta { - return new AnimeMeta( - $this->getAnimeTitle(), - $this->getAnimeURL(), - $this->getAnimeImageUrl() - ); + return match ($page) { + 'user' => new AnimeMeta( + $this->getAnimeTitle(), + $this->getAnimeURL(), + $this->getAnimeImageUrlFromUserPage() + ), + default => new AnimeMeta( + $this->getAnimeTitle(), + $this->getAnimeURL(), + $this->getAnimeImageUrl() + ), + }; } /** @@ -84,7 +92,7 @@ public function getUrl(): string public function getAnimeTitle(): string { return $this->crawler - ->filterXPath('//div[1]/div/div[2]/div/a') + ->filterXPath('//div[contains(@class, "titleblock")]/a') ->text(); } @@ -95,16 +103,16 @@ public function getAnimeTitle(): string public function getAnimeUrl(): string { // User UserReviews page - $node = $this->crawler - ->filterXPath('//div[1]/div/div[2]/div/a'); - - if ($node->count()) { - return $node->attr('href'); - } +// $node = $this->crawler +// ->filterXPath('//div[1]/div/div[2]/div/a'); +// +// if ($node->count()) { +// return $node->attr('href'); +// } // Recent UserReviews page $node = $this->crawler - ->filterXPath('//div[1]/div[2]/div[1]/div[1]/a'); + ->filterXPath('//div[contains(@class, "titleblock")]/a'); return $node->attr('href'); } @@ -115,17 +123,19 @@ public function getAnimeUrl(): string */ public function getAnimeImageUrl(): string { - // User UserReviews page $node = $this->crawler - ->filterXPath('//div[1]/div/div[1]/a/img'); + ->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "body")]/div[contains(@class, "text")]/div[contains(@class, "thumb-right")]/a/img'); - if ($node->count()) { - return Parser::parseImageQuality($node->attr('data-src')); - } + return Parser::parseImageQuality($node->attr('data-src')); + } - // Recent UserReviews page + /** + * @return string + */ + public function getAnimeImageUrlFromUserPage(): string + { $node = $this->crawler - ->filterXPath('//div[1]/div[2]/div[1]/div[1]/a/img'); + ->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "thumb")]/a/img'); return Parser::parseImageQuality($node->attr('data-src')); } diff --git a/src/Parser/Reviews/MangaReviewParser.php b/src/Parser/Reviews/MangaReviewParser.php index 6a070335..bbb214e5 100644 --- a/src/Parser/Reviews/MangaReviewParser.php +++ b/src/Parser/Reviews/MangaReviewParser.php @@ -49,13 +49,20 @@ public function getModel(): MangaReview /** * @throws \Jikan\Exception\ParserException */ - public function getManga() : MangaMeta + public function getManga(string $page = null) : MangaMeta { - return new MangaMeta( - $this->getMangaTitle(), - $this->getMangaUrl(), - $this->getMangaImageUrl() - ); + return match ($page) { + 'user' => new MangaMeta( + $this->getMangaTitle(), + $this->getMangaUrl(), + $this->getMangaImageUrlFromUserPage() + ), + default => new MangaMeta( + $this->getMangaTitle(), + $this->getMangaUrl(), + $this->getMangaImageUrl() + ), + }; } /** @@ -196,34 +203,30 @@ public function getType(): ?string public function getMangaTitle(): string { return $this->crawler - ->filterXPath('//div[1]/div/div[2]/div/a') + ->filterXPath('//div[contains(@class, "titleblock")]/a') ->text(); } /** * @return string - * @throws \InvalidArgumentException - * @throws \Jikan\Exception\ParserException */ public function getMangaImageUrl(): string { - // User UserReviews page $node = $this->crawler - ->filterXPath('//div[1]/div/div[1]/a/img'); + ->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "body")]/div[contains(@class, "text")]/div[contains(@class, "thumb-right")]/a/img'); - if ($node->count()) { - return Parser::parseImageQuality($node->attr('data-src')); - } + return Parser::parseImageQuality($node->attr('data-src')); + } - // Recent UserReviews Anime page + /** + * @return string + */ + public function getMangaImageUrlFromUserPage(): string + { $node = $this->crawler - ->filterXPath('//div[1]/div[2]/div[1]/div[1]/a/img'); - - if ($node->count()) { - return Parser::parseImageQuality($node->attr('data-src')); - } + ->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "thumb")]/a/img'); - throw new ParserException("Couldn't find the URL of reviewed image."); + return Parser::parseImageQuality($node->attr('data-src')); } /** @@ -233,16 +236,16 @@ public function getMangaImageUrl(): string public function getMangaUrl(): string { // User UserReviews page - $node = $this->crawler - ->filterXPath('//div[1]/div/div[2]/div/a'); - - if ($node->count()) { - return $node->attr('href'); - } +// $node = $this->crawler +// ->filterXPath('//div[1]/div/div[2]/div/a'); +// +// if ($node->count()) { +// return $node->attr('href'); +// } // Recent UserReviews Anime page $node = $this->crawler - ->filterXPath('//div[1]/div[2]/div[1]/div[1]/a'); + ->filterXPath('//div[contains(@class, "titleblock")]/a'); return $node->attr('href'); }