forked from AlexandrosD/YouTube
/
youtube.videolist.php
executable file
·209 lines (176 loc) · 4.59 KB
/
youtube.videolist.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
<?php
/**
* YouTube PHP library :: YouTubeVideoList class
*
* @author Alexandros D
* @copyright Alexandros D 2011
* @license GNU/GPL v2
* @since 0.7.0
*
*/
require_once 'youtube.php';
require_once 'youtube.video.php';
class YouTubeVideoList {
protected $_videos;
protected $_totalVideos;
protected $_maxResults;
protected $_startIndex;
protected $_youtube;
protected $_developerKey;
/**
* Constructor
*
* @param String $developerKey The developer that wuill be used if calls are to be performed
*/
public function __construct( $developerKey = NULL ) {
$this->_developerKey = $developerKey;
}
/**************************************************************************
* Public Functions
**************************************************************************/
/**** GETTERS ****/
/**
* Fetch list's videos
*
* @return Array A YouTubeVideo() array holding playlist's videos
*/
public function getVideos() {
return $this->_videos;
}
/**
/**
* Count list's videos
*
* @return int The number of videos
*/
public function getVideosCount() {
return $this->_totalVideos;
}
/**
* Get Start Index
*
* @return int The starting index
*/
public function getStartIndex() {
return $this->_startIndex;
}
/**
* Get maximum Results
*
* @return int The maximum results
*/
public function getMaxResults() {
return $this->_maxResults;
}
/**** SETTERS *****/
/**
* Set list's videos
*
* @param array $videos A YouTubeVideo() array holding playlist's videos
*/
public function setVideos( $videos ) {
$this->_videos = $videos;
}
/**
/**
* Set Count list's videos
*
* @param int $count The number of videos
*/
public function setVideosCount( $count ) {
$this->_totalVideos = $count;
}
/**
* Set Start Index
*
* @param int $startIndex The starting index
*/
public function setStartIndex( $startIndex ) {
$this->_startIndex = $startIndex;
}
/**
* Set maximum Results
*
* @param int $maxResults The maximum results
*/
public function setMaxResults( $maxResults ) {
$this->_maxResults = $maxResults;
}
/**** POLPULATE / LOAD ****/
/**
* Populate the list with an ATOM 1.0, RSS 2.0 or JSON feed
* Notice: Currently ony ATOM 1.0 feed are supported
*
* @param String $data The data in plain text format
* @param String $format Supported values are 'atom' , 'rss' , 'json'
* @return boolean
*/
public function populate( $data , $format = "atom") {
if ( $format == 'rss' )
return $this->_parseRss( $data);
if ( $format == 'json' )
return $this->_parseJson( $data);
if ( $format == 'array' )
$this->_videos = $data;
/* else */
return $this->_parseAtom( $data);
}
/**
* Populate the list by loading data using YouTube class function calls
*
* @param String $source The source function that will be called. Supported values are:
* getRelatedVideos, getUploadsByUser, getSubscriptionsByUser, getPlaylist, getFavoritesByUser
* @param String $key The first & base parameter of each of these functions
* i.e The getRelatedVideos has a videoId as key while the getUploads by user has the username as key
* @param int $maxResults The maximum number of results to fetch
* @param int $startIndex the needle...
* @return boolean
*/
public function load( $source , $key , $maxResults = 0 , $startIndex = 0 ) {
$functionName = "get" . $source;
if ( !$this->_youtube )
$this->_youtube = new YouTube( $this->_developerKey );
$data = $this->_youtube-> $functionName ( $key , $maxResults , $startIndex );
if ($data) {
return $this->_parseAtom( $data );
}
/* else */
return FALSE;
}
/**************************************************************************
* Private Functions
**************************************************************************/
private function _parseAtom( $data ){
$xml = new SimpleXMLElement( $data );
//fetch videos
$this->_videos = array();
foreach ( $xml->entry as $entry ) {
$video = new YouTubeVideo( $entry , $this->_developerKey );
$this->_videos[] = $video;
}
//fetch list data
$namespaces = $xml->getNameSpaces(true);
if ($namespaces) {
$openSearch = $xml->children($namespaces['openSearch']);
}
else {
return FALSE;
}
$this->_totalVideos = (int) $openSearch->totalResults;
$this->_startIndex = (int) $openSearch->startIndex;
$this->_maxResults = (int) $openSearch->itemsPerPage;
if ($this->_videos) {
return TRUE;
}
/* else */
return FALSE;
}
private function _parseRss( $data ) {
//TODO
return FALSE;
}
private function _parseJson( $data ) {
//TODO
return FALSE;
}
}