1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Provider; |
4
|
|
|
|
5
|
|
|
class YouTubeProvider extends AbstractOembedProvider implements ProviderInterface |
6
|
|
|
{ |
7
|
|
|
const URL_OEMBED = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json'; |
8
|
|
|
const URL_IMAGE_MAX_RES = 'https://i.ytimg.com/vi/%s/maxresdefault.jpg'; |
9
|
|
|
const URL_IMAGE_HQ = 'https://i.ytimg.com/vi/%s/hqdefault.jpg'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string $id |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
1 |
|
public function getOembedUrl($id) |
16
|
|
|
{ |
17
|
1 |
|
return sprintf(self::URL_OEMBED, $id); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $id |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
1 |
|
public function getImageUrl($id) |
25
|
|
|
{ |
26
|
|
|
// try to get max res image (only available for 720P videos) |
27
|
1 |
|
$urlMaxRes = sprintf(self::URL_IMAGE_MAX_RES, $id); |
28
|
1 |
|
if ($this->urlExists($urlMaxRes)) { |
29
|
|
|
return $urlMaxRes; |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return sprintf(self::URL_IMAGE_HQ, $id); // this one always exists |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $value |
37
|
|
|
* @return string |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
1 |
|
public function parseProviderReference($value) |
41
|
|
|
{ |
42
|
1 |
|
if (strpos($value, 'youtube.com')) { |
43
|
1 |
|
$url = parse_url($value); |
44
|
1 |
|
if (empty($url['query'])) { |
45
|
|
|
throw new \Exception('The supplied URL does not look like a Youtube URL'); |
46
|
|
|
} |
47
|
1 |
|
parse_str($url['query'], $params); |
48
|
1 |
|
if (empty($params['v'])) { |
49
|
|
|
throw new \Exception('The supplied URL does not look like a Youtube URL'); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $params['v']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (strpos($value, 'youtu.be')) { |
56
|
|
|
$url = parse_url($value); |
57
|
|
|
$id = substr($url['path'], 1); |
58
|
|
|
|
59
|
|
|
return $id; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function getIcon() |
69
|
|
|
{ |
70
|
3 |
|
return 'fa fa-youtube-play'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
4 |
|
public function getName() |
77
|
|
|
{ |
78
|
4 |
|
return 'youtube'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
1 |
|
public function getType() |
85
|
|
|
{ |
86
|
1 |
|
return AbstractProvider::TYPE_VIDEO; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
public function getEmbedTemplate() |
93
|
|
|
{ |
94
|
1 |
|
return 'MediaMonksSonataMediaBundle:Provider:youtube_embed.html.twig'; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|