|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Movies\Parser; |
|
4
|
|
|
|
|
5
|
|
|
class Kinobaza |
|
6
|
|
|
{ |
|
7
|
|
|
private const URL = 'https://kinobaza.com.ua'; |
|
8
|
|
|
private const SEARCH_ENDPOINT = self::URL . '/titles?q={title}&ys={year}&ye={year}&per_page=1&display=list_old'; |
|
9
|
|
|
|
|
10
|
|
|
public function find(string $title, int $year): array |
|
11
|
|
|
{ |
|
12
|
|
|
$endpoint = strtr(self::SEARCH_ENDPOINT, [ |
|
13
|
|
|
'{title}' => urlencode($title), |
|
14
|
|
|
'{year}' => $year, |
|
15
|
|
|
]); |
|
16
|
|
|
|
|
17
|
|
|
$html = $this->getHtml($endpoint); |
|
18
|
|
|
|
|
19
|
|
|
if (empty($html)) { |
|
20
|
|
|
return []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$html = substr($html, strpos($html, '<body>')); |
|
24
|
|
|
|
|
25
|
|
|
$title = $this->getTitle($html); |
|
26
|
|
|
$overview = $this->getOverview($html); |
|
27
|
|
|
|
|
28
|
|
|
if (!$title) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return [ |
|
33
|
|
|
'title' => $title, |
|
34
|
|
|
'overview' => $overview, |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function getHtml(string $endpoint): string |
|
39
|
|
|
{ |
|
40
|
|
|
$c = curl_init($endpoint); |
|
41
|
|
|
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
|
42
|
|
|
|
|
43
|
|
|
$html = curl_exec($c); |
|
44
|
|
|
|
|
45
|
|
|
if ($html === false) { |
|
46
|
|
|
//todo write to log curl_error() |
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
curl_close($c); |
|
51
|
|
|
|
|
52
|
|
|
return $html; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getTitle(string $html): string |
|
56
|
|
|
{ |
|
57
|
|
|
$beginStr = 'itemprop="name">'; |
|
58
|
|
|
$startPos = strpos($html, $beginStr); |
|
59
|
|
|
|
|
60
|
|
|
if ($startPos === false) { |
|
61
|
|
|
return ''; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$startPos += strlen($beginStr); |
|
65
|
|
|
$endPos = strpos($html, '</span>', $startPos); |
|
66
|
|
|
|
|
67
|
|
|
$length = $endPos - $startPos; |
|
68
|
|
|
return substr($html, $startPos, $length); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getOverview(string $html): string |
|
72
|
|
|
{ |
|
73
|
|
|
$beginStr = '<div class="col-12 mt-2">'; |
|
74
|
|
|
$startPos = strpos($html, $beginStr); |
|
75
|
|
|
|
|
76
|
|
|
if ($startPos === false) { |
|
77
|
|
|
return ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$startPos += strlen($beginStr); |
|
81
|
|
|
$endPos = strpos($html, '</div>', $startPos); |
|
82
|
|
|
|
|
83
|
|
|
$length = $endPos - $startPos; |
|
84
|
|
|
return substr($html, $startPos, $length); |
|
85
|
|
|
} |
|
86
|
|
|
} |