1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Movies\Service; |
6
|
|
|
|
7
|
|
|
use App\Movies\Entity\Movie; |
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
9
|
|
|
|
10
|
|
|
class ImdbReleaseDateParserService |
11
|
|
|
{ |
12
|
|
|
private const IMDB_RELEASE_DATES_URL = 'https://www.imdb.com/title/{id}/releaseinfo?ref_=tt_dt_dt'; |
13
|
|
|
|
14
|
|
|
private $imdbMapper; |
15
|
|
|
|
16
|
|
|
public function __construct(ImdbDataMapper $mapper) |
17
|
|
|
{ |
18
|
|
|
$this->imdbMapper = $mapper; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getReleaseDates(Movie $movie): array |
22
|
|
|
{ |
23
|
|
|
if ($movie->getImdbId() === null) { |
24
|
|
|
return []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$html = $this->loadImdbReleaseDatesPageHtml($movie->getImdbId()); |
28
|
|
|
$crawler = new Crawler($html, $this->getEndpoint($movie->getImdbId())); |
29
|
|
|
|
30
|
|
|
$tds = $crawler->filterXPath('//*[@id="release_dates"]//td')->getIterator(); |
31
|
|
|
|
32
|
|
|
$result = []; |
33
|
|
|
$country = ''; |
34
|
|
|
foreach ($tds as $td) { |
35
|
|
|
if ($td->getAttribute('class') === 'release_date') { |
36
|
|
|
$result[$country] = $this->imdbMapper->dateToObject($td->textContent); |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($td->hasChildNodes() && $td->getElementsByTagName('a')->item(0) !== null) { |
41
|
|
|
$country = $td->getElementsByTagName('a')->item(0)->textContent; |
42
|
|
|
$country = $this->imdbMapper->countryToCode($country); |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function getEndpoint(string $imdbId): string |
51
|
|
|
{ |
52
|
|
|
return str_replace('{id}', $imdbId, self::IMDB_RELEASE_DATES_URL); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function loadImdbReleaseDatesPageHtml(string $imdbId): string |
56
|
|
|
{ |
57
|
|
|
$endpoint = $this->getEndpoint($imdbId); |
58
|
|
|
|
59
|
|
|
$c = curl_init($endpoint); |
60
|
|
|
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
61
|
|
|
|
62
|
|
|
$html = curl_exec($c); |
63
|
|
|
|
64
|
|
|
if ($html === false) { |
65
|
|
|
//todo write to log curl_error() |
66
|
|
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
curl_close($c); |
70
|
|
|
|
71
|
|
|
return $html; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|