forked from AlexandrosD/YouTube
/
youtube.user.php
189 lines (146 loc) · 5.57 KB
/
youtube.user.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
<?php
/**
* YouTube PHP library
*
* @author Alexandros D
* @copyright Alexandros D 2011
* @license GNU/GPL v2
* @since 0.9.0
*
* @todo Add phpDoc Info
* @todo Implement getInfo, getPlaylists, getUploads
* @todo Implement getSubscriptions
*
*/
require_once 'youtube.php';
require_once 'youtube.videolist.php';
require_once 'youtube.playlist.php';
class YouTubeUser {
private $_loaded;
private $_username;
private $_channelTitle;
private $_gender;
private $_age;
private $_location;
private $_youtube;
private $_playlists;
private $_playlists_count;
private $_playlists_maxResults;
private $_playlists_startIndex;
private $_uploads;
private $_uploads_maxResults;
private $_uploads_startIndex;
private $_subscriptions;
private $_subscriptions_maxResults;
private $_subscriptions_startIndex;
private $_favorites;
private $_favorites_maxResults;
private $_favorites_startIndex;
private $_developerKey;
public function __construct ( $username , $developerKey = NULL ) {
$this->_username = $username;
$this->_developerKey = $developerKey;
$this->_youtube = new YouTube( $this->_developerKey );
}
/**
* Load user's details
*
* @return Array
*/
public function getInfo() {
if ( !$this->_loaded )
$this->_loadInfo();
$info = array (
"username" => $this->_username,
"channelTitle" => $this->_channelTitle,
"gender" => $this->_gender,
"age" => $this->_age,
"location" => $this->_location
);
return $info;
}
public function getPlaylists( $maxResults = 0 , $startIndex = 0 ) {
if ( !$this->_playlists_maxResults == $maxResults || !$this->_playlists_startIndex == $startIndex || !$this->_playlists )
$this->_loadPlaylists( $maxResults , $startIndex);
return $this->_playlists;
}
public function getPlaylistsCount() {
return $this->_playlists_count;
}
public function getUploads( $maxResults = 0 , $startIndex = 0 ) {
if ( !$this->_uploads_maxResults == $maxResults || !$this->_uploads_startIndex == $startIndex || !$this->_uploads )
$this->_loadUploads( $maxResults , $startIndex);
return $this->_uploads;
}
public function getFavorites( $maxResults = 0 , $startIndex = 0 ) {
if ( !$this->_favorites_maxResults == $maxResults || !$this->_favorites_startIndex == $startIndex || !$this->_favorites )
$this->_loadFavorites( $maxResults , $startIndex);
return $this->_favorites;
}
public function getSubscriptions( $maxResults = 0 , $startIndex = 0 ) {
//TODO - Phase II
return FALSE;
}
/*****************************************************************
*********** PRIVATE FUNCTIONS
****************************************************************/
private function _loadInfo() {
$data = $this->_youtube->getUserData( $this->_username );
$xml = new SimpleXMLElement( $data );
$namespaces = $xml->getNameSpaces(true);
if ($namespaces) {
$yt = $xml->children($namespaces['yt']);
}
else {
return FALSE;
}
$this->_channelTitle = (string) $xml->title;
$this->_age = (string) $yt->age;
$this->_gender = (string) $yt->gender;
$this->_location = (string) $yt->location;
$this->_loaded = TRUE;
}
private function _loadPlaylists( $maxResults = 0 , $startIndex = 0 ) {
$this->_playlists_maxResults = $maxResults;
$this->_playlists_startIndex = $startIndex;
$data = $this->_youtube->getPlaylistsByUser( $this->_username , $maxResults , $startIndex );
$ids = array();
$xml = new SimpleXMLElement( $data );
//count playlists
$ns = $xml->getNamespaces(true);
if ($ns) {
$openSearch = $xml->children($ns['openSearch']);
$this->_playlists_count = (int) $openSearch->totalResults;
}
if ($xml->entry) {
foreach ($xml->entry as $playlist) {
$namespaces = $playlist->getNamespaces(true);
$yt = $playlist->children($namespaces['yt']);
$ids[] = $yt->playlistId;
}
}
$playlists = array();
foreach ($ids as $id) {
$playlists[] = new YouTubePlaylist($id , $this->_developerKey);
}
$this->_playlists = $playlists;
}
private function _loadUploads( $maxResults = 0 , $startIndex = 0 ) {
$this->_uploads_maxResults = $maxResults;
$this->_uploads_startIndex = $startIndex;
$uploads = new YouTubeVideoList( $this->_developerKey );
$uploads->load( "UploadsByUser" , $this->_username , $maxResults , $startIndex );
$this->_uploads = $uploads;
}
private function _loadFavorites( $maxResults = 0 , $startIndex = 0 ) {
$this->_favorites_maxResults = $maxResults;
$this->_favorites_startIndex = $startIndex;
$favorites = new YouTubeVideoList( $this->_developerKey );
$favorites->load( "FavoritesByUser" , $this->_username , $maxResults , $startIndex );
$this->_favorites = $favorites;
}
private function _loadSubscriptions( $maxResults = 0 , $startIndex = 0 ) {
//TODO - Phase II
return FALSE;
}
}