-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-parser.php
72 lines (60 loc) · 2.35 KB
/
youtube-parser.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
<?php
/**
* User: Benftwc
* Date: 18/11/13 21:42
*/
function parseVideoEntry($entry) {
$obj= new stdClass;
// get author name and feed URL
$obj->author = $entry->author->name;
$obj->authorURL = $entry->author->uri;
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$obj->title = $media->group->title;
$obj->description = $media->group->description;
// 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;
}