1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\PonthubBundle\Service; |
4
|
|
|
|
5
|
|
|
use KI\CoreBundle\Service\CurlService; |
6
|
|
|
|
7
|
|
|
// Échange des informations avec l'API Imdb pour récupérer des informations |
8
|
|
|
// sur les films et les séries (utilisé par Ponthub) |
9
|
|
|
// Testé par PonthubControllerTest |
10
|
|
|
class ImdbService |
11
|
|
|
{ |
12
|
|
|
protected $curlService; |
13
|
|
|
protected $baseUrl = 'http://www.omdbapi.com/'; |
14
|
|
|
|
15
|
|
|
public function __construct(CurlService $curlService) |
16
|
|
|
{ |
17
|
|
|
$this->curlService = $curlService; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function search($name) |
21
|
|
|
{ |
22
|
|
|
$url = $this->baseUrl.'?s='.urlencode($name); |
23
|
|
|
$response = json_decode($this->curlService->curl($url), true); |
24
|
|
|
|
25
|
|
|
$return = []; |
26
|
|
|
if (isset($response['Search'])) { |
27
|
|
|
foreach ($response['Search'] as $result) { |
28
|
|
|
$return[] = [ |
29
|
|
|
'name' => $result['Title'], |
30
|
|
|
'year' => $result['Year'], |
31
|
|
|
'type' => $result['Type'], |
32
|
|
|
'id' => $result['imdbID'] |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function infos($id) |
41
|
|
|
{ |
42
|
|
|
$url = $this->baseUrl.'?i='.urlencode($id); |
43
|
|
|
$response = json_decode($this->curlService->curl($url), true); |
44
|
|
|
|
45
|
|
|
if (!isset($response['Title'])) |
46
|
|
|
return null; |
47
|
|
|
|
48
|
|
|
if (preg_match('#\d–\d#', $response['Year'])) |
49
|
|
|
$response['Year'] = explode('–', $response['Year'])[0]; |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'title' => $response['Title'], |
53
|
|
|
'year' => (int)$response['Year'], |
54
|
|
|
'duration' => 60*str_replace(' min', '', $response['Runtime']), |
55
|
|
|
'genres' => explode(', ', $response['Genre']), |
56
|
|
|
'director' => $response['Director'], |
57
|
|
|
'actors' => explode(', ', $response['Actors']), |
58
|
|
|
'description' => $response['Plot'], |
59
|
|
|
'image' => $response['Poster'], |
60
|
|
|
'rating' => isset($response['Metascore']) ? $response['Metascore'] : $response['imdbRating']*10 |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|