-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube.class.php
309 lines (269 loc) · 9.74 KB
/
youtube.class.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* Gaurav Mishra | http://phpcollection.com/
* MSN: [email protected]
* Email: [email protected]
* Started: 02/03/2011 19:44 (IST)
* Tested: Yes
* PHP 4/5: 5
* No warranty is given to code used
*/
/**
* Youtube API
*
* @package Youtube API
* @url http://phpcollecttion.com/classes/youtube
* @desc Package which allows easy interaction with the Youtube v2 Data API
* @author Gaurav Mishra
* @copyright 2011
* @version 0.0.1
* @access public
* @license GPL
*/
/**
* Todo:-
*/
class Youtube
{
/** Your API KEY */
public $key;
/** URI to the API , used for making REST call. */
var $feed = "http://gdata.youtube.com/feeds/api/";
/**
* Youtube::__construct()
*
* @param mixed $apikey Your developer API Key
* @param bool $check Should we run checks to make sure all the extensions are loaded?
* @return void
*/
public function __construct($apikey = null, $partner_id = null, $check = true)
{
/** Construct class, runs on initialization **/
if ($check) {
/** If the check parametre is set **/
if (!extension_loaded('curl') /** Is the cURL extension loaded? **/ ) {
throw new Exception('You don\'t have cURL loaded.');
/** If not, throw an exception **/
}
if (!extension_loaded('SimpleXML') /** Is the SimpleXML extension loaded? **/ ) {
throw new Exception('You don\'t have the SimpleXML lib loaded.');
/** If not, throw an exception **/
}
}
}
function check_youtube_db($profile_id, $watchUrl){
global $db;
$q = "SELECT * FROM `youtube_data` WHERE `profile_ID` = '$profile_id' AND `watchUrl` = '$watchUrl'";
if($db->get_row($q) == null){
return false;
}else{
return true;
}
}
function top_videos($profile_id) {
global $db;
$q = "SELECT * from `youtube_data` WHERE `profile_ID` = '$profile_id' ORDER by `viewCount` DESC LIMIT 0, 7";
//echo $q;
$data = $db->get_results($q, ARRAY_A);
return $data;
}
function get_youtube_data($profile_id, $to , $from) {
global $db;
$query = "SELECT * from `youtube_data` WHERE `profile_id` = '$profile_id' AND `created` BETWEEN '$from' AND '$to'";
$data = $db->get_results($query, ARRAY_A);
return $data;
}
/**
* Youtube::search_videos()
*
* @param mixed $
* @return array $response Array of data of results
*/
function search_videos($query, $orderby = null, $time = null, $start = 0, $max = 0)
{
$url = "";
if ($start != null) {
$url .= "&start-index=$start";
}
if ($max != null) {
$url .= "&max-results=$max";
}
if ($orderby != null) {
$url .= "&orderby=$orderby";
}
if ($time != null) {
$url .= "&time=$time";
}
$query = urlencode($query);
/** URI used for making REST call. Each Web Service uses a unique URL.*/
$request = $this->feed . "videos?q=$query" . $url;
echo "Link is being prepared for the keyword ".$query."\n";
/** Get the response via CURL */
$response = $this->get($request);
//print_r($response);
/** search data from the xml result*/
$video_arrray = $this->get_search_data($response);
// print_r($video_array);
// exit();
/** Arrayed data of videos*/
return $video_arrray;
}
function search_channel($query, $orderby = null, $time = null, $start = 0, $max = 0)
{
$url = "";
if ($start != null) {
$url .= "&start-index=$start";
}
if ($max != null) {
$url .= "&max-results=$max";
}
if ($orderby != null) {
$url .= "&orderby=$orderby";
}
if ($time != null) {
$url .= "&time=$time";
}
$query = urlencode($query);
/** URI used for making REST call. Each Web Service uses a unique URL.*/
$request = $this->feed . "channels?q=$query" . $url;
/** Get the response via CURL */
$response = $this->get($request);
/** search data from the xml result*/
$video_arrray = $this->get_search_data($response);
/** Arrayed data of videos*/
return $video_arrray;
}
/**
* Youtube::video_data()
*
* @param void
* @return array $data Array of data of results
*/
function get_video_insight_by_id($video_id)
{
/** URI used for making REST call. Each Web Service uses a unique URL.*/
echo "video insight for $video_id is being called.\n";
$request = $this->feed . "videos/$video_id?v=2";
//print_r($request);
/** Get the response via CURL */
$response = $this->get($request);
/** Arrayed data of video*/
return $response;
}
function get_video_insight_by_url($request)
{
//echo $request;
/** Get the response via CURL */
$response = $this->get($request);
//print_r($response);
$response_data = $this->parseVideoEntry($response);
/** Arrayed data of video*/
return $response_data;
}
/**
* Youtube::get()
*
* @param mixed $request request data from the youtube gdata api !
* @return array $data arrayed data from the request
*/
function get($url, $username = '', $password = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
$Headers = curl_getinfo($ch);
curl_close($ch);
//print_r($Headers);
if ($Headers['http_code'] == 200) {
// $xml = file_get_contents($url);
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
//print_r($data);
return $data;
} else {
return null;
}
}
function get_search_data($xml)
{
$i = 0;
/** Traverse XML tree and save desired values from child nodes */
//print_r($xml->entry);
if($xml->entry != ''){
foreach ($xml->entry as $result) {
//print_r($result);
$data[$i]['id'] = (string )$result->id;
$data[$i]['published'] = (string )$result->published;
$data[$i]['updated'] = (string )$result->updated;
$data[$i]['title'] = (string )$result->title;
$data[$i]['content'] = (string )$result->content;
$data[$i]['author'] = (string )$result->author->name;
$data[$i]['author_uri'] = (string )$result->author->uri;
$data[$i]['video_link'] = (string )$result->link[0]->attributes()->href[0];
echo "A video with id=".$result->id." is being inserted in the Array\n";
$i++;
}
}else{
$data = array();
}
//print_r($data);
return $data;
}
function parseVideoEntry($entry)
{
$obj = new stdClass;
/** get nodes in media: namespace for media information */
$media = $entry->children('http://search.yahoo.com/mrss/');
//print_r($entry->published);
//print_r($media->group->published);
$obj->title = $media->group->title;
$obj->description = $media->group->description;
$obj->published = strtotime($entry->published);
/** get video player URL */
$attrs = $media->group->player->attributes();
$obj->watchURL = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$obj->thumbnailURL = $attrs['url'];
// get <yt:duration> node for video length
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$obj->length = $attrs['seconds'];
// get <yt:stats> node for viewer statistics
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$obj->viewCount = $attrs['viewCount'];
// get <gd:rating> node for video ratings
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->rating) {
$attrs = $gd->rating->attributes();
$obj->rating = $attrs['average'];
} else {
$obj->rating = 0;
}
// get <gd:comments> node for video comments
$gd = $entry->children('http://schemas.google.com/g/2005');
if ($gd->comments->feedLink) {
$attrs = $gd->comments->feedLink->attributes();
$obj->commentsURL = $attrs['href'];
$obj->commentsCount = $attrs['countHint'];
}
// get feed URL for video responses
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
2007#video.responses']");
if (count($nodeset) > 0) {
$obj->responsesURL = $nodeset[0]['href'];
}
// get feed URL for related videos
$entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
$nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
2007#video.related']");
if (count($nodeset) > 0) {
$obj->relatedURL = $nodeset[0]['href'];
}
// return object to caller
return $obj;
}
}
?>