-
Notifications
You must be signed in to change notification settings - Fork 2
/
Reader.php
188 lines (150 loc) · 4.87 KB
/
Reader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace TroiaStudio\YoutubeAPI;
use Nette;
use Composer\CaBundle\CaBundle;
use GuzzleHttp\Client;
class Reader
{
use Nette\SmartObject;
//private $link = 'https://www.googleapis.com/youtube/v3/videos';
private $link = 'https://www.googleapis.com/youtube/v3/videos?key=%s&part=snippet,contentDetails,statistics,status&id=%s';
/**
* @var string
*/
public $apiKey;
/**
* @var string
*/
public $url;
/**
* @var string
*/
private $videoId;
/**
* @var array
*/
private $videos = [];
/**
* @var Video
*/
public $video;
/**
* @var Video
*/
private $lastVideo;
/** @var Client */
private $httpClient;
/** @var bool */
private $exception;
public function __construct($apiKey = null, $httpClient = null, $exception = true)
{
if ($apiKey !== null) {
$this->apiKey = $apiKey;
}
if ($httpClient === null) {
$httpClient = new Client([
'verify' => CaBundle::getSystemCaRootBundlePath()
]);
}
$this->httpClient = $httpClient;
$this->exception = $exception;
}
public function setUrl($url)
{
$this->url = $url;
if ($this->isUrl($url)) {
$id = $this->getVideoId($url);
if (!$id) {
throw new \Exception("Probably problem with get id from '{$url}' ");
}
} else {
$id = $url;
}
$this->createVideo($this->getData($id), $id);
}
private function getVideoId($url)
{
$pattern = '#^(?:https?://|//)?(?:www\.|m\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v/|watch\?v=|watch\?.+&v=))([\w-]{11})(?![\w-])#';
preg_match($pattern, $url, $matches);
if (isset($matches[1])) {
$this->videoId = $matches[1];
return $matches[1];
}
return false;
}
private function isUrl($url)
{
$regex = "(((https?|HTTPS?|ftp)\:)?\/\/)?"; // SCHEME
$regex .= "([a-zA-Z0-9+!*(),;?&=\$_.-]+(\:[a-zA-Z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
$regex .= "([a-zA-Z0-9-.]*)\.([a-zA-Z]{2,3})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-zA-Z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-zA-Z+&\$_.-][a-zA-Z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-zA-Z_.-][a-zA-Z0-9+\$_.-]*)?"; // Anchor
if (preg_match("/^$regex$/", $url)) {
return TRUE;
}
return false;
}
public function getVideo($id = null)
{
if ($id === null) {
if ($this->lastVideo === null) {
$this->setUrl($id);
}
return $this->lastVideo;
}
if ($this->isUrl($id)) {
$id = $this->getVideoId($id);
}
if (!isset($this->videos[$id])) {
$this->setUrl($id);
}
return $this->videos[$id];
}
private function getData($videoId)
{
$url = sprintf($this->link, $this->apiKey, $videoId);
$response = $this->httpClient->get($url, [
'http_errors' => false,
]);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException("Unable to parse Youtube video: '{$videoId}' ({$response->getStatusCode()}) {$response->getReasonPhrase()}");
}
return $response->getBody()->getContents();
}
private function createVideo($data, $videoId)
{
$json = Nette\Utils\Json::decode($data);
if (!isset($json->items[0]->snippet) || !isset($json->items[0]->contentDetails) || !isset($json->items[0]->status) || !isset($json->items[0]->statistics)) {
throw new \RuntimeException("Empty YouTube response, probably wrong '{$videoId}' video id.");
}
$snippet = $json->items[0]->snippet;
$details = $json->items[0]->contentDetails;
$statistics = $json->items[0]->statistics;
$status = $json->items[0]->status;
$video = new Video;
$video->id = $videoId;
$video->title = $snippet->title;
$video->description = $snippet->description;
$video->url = 'https://www.youtube.com/watch?v=' . $videoId;
$video->embed = 'https://www.youtube.com/embed/' . $videoId;
$video->views = $statistics->viewCount;
$video->duration = $details->duration;
$video->published = new Nette\Utils\DateTime($snippet->publishedAt);
$thumbFormats = [
'default',
'medium',
'high',
'standard',
'maxres'
];
foreach ($thumbFormats as $thumb) {
if (isset($snippet->thumbnails->$thumb)) {
$video->thumbs[$thumb] = $snippet->thumbnails->$thumb;
}
}
$this->lastVideo = $video;
$this->videos[$videoId] = $video;
}
}