Skip to content

Commit

Permalink
Merge pull request #605 from leepeuker/fix-imdb-rating
Browse files Browse the repository at this point in the history
Fix imdb rating sync (imdb html changed)
  • Loading branch information
leepeuker committed Jul 6, 2024
2 parents 0f5de85 + 1b73ce1 commit 4facfb9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.5"

services:
app:
build:
Expand Down
14 changes: 7 additions & 7 deletions src/Api/Imdb/ImdbWebScrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function extractProductionStatus(string $imdbRatingPage) : ?string

private function extractRatingAverage(string $imdbRatingPage, string $imdbId) : ?float
{
preg_match('/cMEQkK">(\d([.,])\d)/', $imdbRatingPage, $averageRatingMatches);
preg_match('/cxhhrI">(\d([.,])\d)/', $imdbRatingPage, $averageRatingMatches);
if (empty($averageRatingMatches[1]) === true) {
$this->logger->warning('IMDb: Could not extract rating average.', ['url' => $this->urlGenerator->buildMovieUrl($imdbId)]);

Expand All @@ -83,32 +83,32 @@ private function extractRatingAverage(string $imdbRatingPage, string $imdbId) :
private function extractRatingVoteCount(string $imdbRatingPage, string $imdbId) : ?int
{
// Handle numbers without suffix
preg_match('/gPVQxL">([0-9]+)</', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]+)</', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)$voteCountMatches[1];
}
preg_match('/gPVQxL">([0-9]{1,3}([.,]?[0-9]{3})+)/', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]{1,3}([.,]?[0-9]{3})+)/', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)str_replace([',', '.'], '', $voteCountMatches[1]);
}

// Handle numbers with K suffix
preg_match('/gPVQxL">([0-9]+)K</', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]+)K</', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)$voteCountMatches[1] * 1000;
}
preg_match('/gPVQxL">([0-9]{1,3}[.,][0-9]{1,3})K</', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]{1,3}[.,][0-9]{1,3})K</', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)((float)$voteCountMatches[1] * 1000);
}

// Handle simple numbers with M suffix
preg_match('/gPVQxL">([0-9]+)M</', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]+)M</', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)$voteCountMatches[1] * 1000000;
}
// Handle simple numbers with K suffix
preg_match('/gPVQxL">([0-9]{1,3}[.,][0-9]{1,3})M</', $imdbRatingPage, $voteCountMatches);
preg_match('/gUihYJ">([0-9]{1,3}[.,][0-9]{1,3})M</', $imdbRatingPage, $voteCountMatches);
if (empty($voteCountMatches[1]) === false) {
return (int)((float)$voteCountMatches[1] * 1000000);
}
Expand Down
46 changes: 23 additions & 23 deletions tests/unit/Api/Imdb/ImdbWebScrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,63 +27,63 @@ public function provideFindRatingData() : array
{
return [
[
'cMEQkK">7.9</span>
gPVQxL">229.240</div>',
'cxhhrI">7.9</span>
gUihYJ">229.240</div>',
ImdbRating::create(7.9, 229240)
],
'returns no rating if current production status is found' => [
'hjAonB">Post-production
cMEQkK">7.9</span>
gPVQxL">229.240</div>',
cxhhrI">7.9</span>
gUihYJ">229.240</div>',
null,
],
[
'cMEQkK">7,9</span>
gPVQxL">229,240</div>',
'cxhhrI">7,9</span>
gUihYJ">229,240</div>',
ImdbRating::create(7.9, 229240)
],
[
'cMEQkK">7,9</span>
gPVQxL">229240</div>',
'cxhhrI">7,9</span>
gUihYJ">229240</div>',
ImdbRating::create(7.9, 229240)
],
[
'cMEQkK">7,9</span>
gPVQxL">1.229,240</div>',
'cxhhrI">7,9</span>
gUihYJ">1.229,240</div>',
ImdbRating::create(7.9, 1229240)
],
[
'cMEQkK">7,9</span>
gPVQxL">40</div>',
'cxhhrI">7,9</span>
gUihYJ">40</div>',
ImdbRating::create(7.9, 40)
],
[
'cMEQkK">7,9</span>
gPVQxL">40K</div>',
'cxhhrI">7,9</span>
gUihYJ">40K</div>',
ImdbRating::create(7.9, 40000)
],
[
'cMEQkK">7,9</span>
gPVQxL">4.1K</div>',
'cxhhrI">7,9</span>
gUihYJ">4.1K</div>',
ImdbRating::create(7.9, 4100)
],
[
'cMEQkK">7,9</span>
gPVQxL">14.12K</div>',
'cxhhrI">7,9</span>
gUihYJ">14.12K</div>',
ImdbRating::create(7.9, 14120)
],
[
'cMEQkK">7,9</span>
gPVQxL">10M</div>',
'cxhhrI">7,9</span>
gUihYJ">10M</div>',
ImdbRating::create(7.9, 10000000)
],
[
'cMEQkK">7,9</span>
gPVQxL">10.1M</div>',
'cxhhrI">7,9</span>
gUihYJ">10.1M</div>',
ImdbRating::create(7.9, 10100000)
],
[
'cMEQkK">7,9</span>',
'cxhhrI">7,9</span>',
null
],
[
Expand Down

0 comments on commit 4facfb9

Please sign in to comment.