diff --git a/.gitignore b/.gitignore index 7e75b14c..0a93fac2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.php-cs-fixer.cache composer.phar .phpunit.result.cache +/.phpdoc diff --git a/src/Model/Manga/Manga.php b/src/Model/Manga/Manga.php index dacb5242..cee4c031 100644 --- a/src/Model/Manga/Manga.php +++ b/src/Model/Manga/Manga.php @@ -211,8 +211,8 @@ public static function fromParser(MangaParser $parser): Manga $instance->explicitGenres = $parser->getExplicitGenres(); $instance->demographics = $parser->getDemographics(); $instance->themes = $parser->getThemes(); - $instance->score = $parser->getMangaScore(); - $instance->scoredBy = $parser->getMangaScoredBy(); + $instance->score = $parser->getScore(); + $instance->scoredBy = $parser->getScoredBy(); $instance->rank = $parser->getMangaRank(); $instance->popularity = $parser->getMangaPopularity(); $instance->members = $parser->getMangaMembers(); diff --git a/src/Parser/Anime/AnimeParser.php b/src/Parser/Anime/AnimeParser.php index 4d0f7580..49e55dd5 100644 --- a/src/Parser/Anime/AnimeParser.php +++ b/src/Parser/Anime/AnimeParser.php @@ -556,18 +556,19 @@ public function getRating(): ?string */ public function getScore(): ?float { - $score = trim( - $this->crawler->filterXPath('//div[@class="fl-l score"]')->text() - ); + $score = $this->crawler->filterXPath('//span[@itemprop="ratingValue"]'); + + if (!$score->count()) { + return null; + } + + $score = JString::cleanse($score->text()); if ($score === 'N/A') { return null; } return (float) $score; - - // doesn't work for some IDs like `29711` - //return Parser::textOrNull($this->crawler->filterXPath('//span[@itemprop="ratingValue"]')); } /** @@ -576,7 +577,13 @@ public function getScore(): ?float */ public function getScoredBy(): ?int { - $scoredBy = $this->crawler->filterXPath('//div[@class="fl-l score"]')->attr('data-user'); + $scoredBy = $this->crawler->filterXPath('//span[@itemprop="ratingCount"]'); + + if (!$scoredBy->count()) { + return null; + } + + $scoredBy = JString::cleanse($scoredBy->text()); $scoredByNum = str_replace( [',', ' users', ' user'], diff --git a/src/Parser/Manga/MangaParser.php b/src/Parser/Manga/MangaParser.php index 8e1daa5d..7a8c3b1f 100644 --- a/src/Parser/Manga/MangaParser.php +++ b/src/Parser/Manga/MangaParser.php @@ -456,20 +456,21 @@ function (Crawler $crawler) { * @throws \RuntimeException * @throws \InvalidArgumentException */ - public function getMangaScore(): ?float + public function getScore(): ?float { - $score = $this->crawler - ->filter('span[itemprop="ratingValue"]'); + $score = $this->crawler->filterXPath('//span[@itemprop="ratingValue"]'); if (!$score->count()) { return null; } - if (strpos($score->text(), 'N/A')) { + $score = JString::cleanse($score->text()); + + if ($score === 'N/A') { return null; } - return (float)$score->text(); + return (float) $score; } /** @@ -477,16 +478,27 @@ public function getMangaScore(): ?float * @throws \RuntimeException * @throws \InvalidArgumentException */ - public function getMangaScoredBy(): ?int + public function getScoredBy(): ?int { - $scoredBy = $this->crawler - ->filter('span[itemprop="ratingCount"]'); + $scoredBy = $this->crawler->filterXPath('//span[@itemprop="ratingCount"]'); if (!$scoredBy->count()) { return null; } - return (int)$scoredBy->text(); + $scoredBy = JString::cleanse($scoredBy->text()); + + $scoredByNum = str_replace( + [',', ' users', ' user'], + '', + $scoredBy + ); + + if (!is_numeric($scoredByNum)) { + return null; + } + + return (int) $scoredByNum; } /**