Skip to content

Commit

Permalink
fix mal april fools 2024 affecting score
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir committed Apr 1, 2024
1 parent db7c2d2 commit eac0085
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.php-cs-fixer.cache
composer.phar
.phpunit.result.cache
/.phpdoc
4 changes: 2 additions & 2 deletions src/Model/Manga/Manga.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 14 additions & 7 deletions src/Parser/Anime/AnimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'));
}

/**
Expand All @@ -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'],
Expand Down
30 changes: 21 additions & 9 deletions src/Parser/Manga/MangaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,37 +456,49 @@ 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;
}

/**
* @return int
* @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;
}

/**
Expand Down

0 comments on commit eac0085

Please sign in to comment.