-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUStream.php
More file actions
371 lines (310 loc) · 8.56 KB
/
Copy pathUStream.php
File metadata and controls
371 lines (310 loc) · 8.56 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* This class has been created to help developers adapt the UStream Services to their sites
*
* @author Klederson Bueno <klederson@klederson.com>
* @version 0.1a
* @license GPL
*/
class UStream {
static public $apiUrl = 'http://api.ustream.tv'; //complete url reference http://api.ustream.tv/[html|json|xml|php]/[subject]/[subjectUID|scope]/[command]/[otherparams]/?page=[n]&limit=[l]&key=[devkey]
/**
* The main response method *** DO NOT CHANGE IT BECAUSE THE CLASS USES JSON TO COMMUNICATE ***
* @var unknown_type
*/
static private $responseMode = 'json'; //html, json, xml, php
/**
* The main request mode you can change it using setRequestMode
* @var String
*/
static public $requestMode = 'channel'; //user, channel, stream, video, system
/**
* Your UStream API Key
* @var String
*/
static protected $apiKey = null;
public $cacheResult = null;
public $limit = 20;
public $page = 1;
public function __construct($apiKey, $requestMode = 'channel') {
self::$apiKey = $apiKey;
self::$requestMode = $requestMode;
$this->cacheResult = new stdClass();
}
###########################################
# Misc commands
###########################################
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @return stdObj
*/
public function getInfo($subject) {
$requestUrl = $this->buildRequestUrl($subject, 'getInfo');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @param String $property
*
* @return stdClass
*/
public function getValueOf($subject, $property) {
$requestUrl = $this->buildRequestUrl($subject, 'getValueOf/'.$property);
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $scope
* @param String $where
* @param String $how
* @param String $what
* @param Number $page
*
* @return stdClass
*/
public function search($scope, $where, $how, $what,$page = 1) {
$fullCommand = sprintf("search/%s:%s:%s", $where, $how, $what);
$requestUrl = $this->buildRequestUrl($scope, $fullCommand);
return $this->getResult($requestUrl, $page);
}
###########################################
# Stream commands
###########################################
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @return stdClass
*/
public function getRecent() {
$requestUrl = $this->buildRequestUrl('all', 'getRecent');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @return stdClass
*/
public function getMostViewers() {
$requestUrl = $this->buildRequestUrl('all', 'getMostViewers');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @return stdClass
*/
public function getRandom() {
$requestUrl = $this->buildRequestUrl('all', 'getRandom');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $page
* @return stdClass
*/
public function getAllNew($page = 1) {
$requestUrl = $this->buildRequestUrl('all', 'getAllNew');
return $this->getResult($requestUrl, $page);
}
###########################################
# Video commands
###########################################
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @return stdClass
*/
public function getTags($subject) {
$requestUrl = $this->buildRequestUrl($subject, 'getTags');
return $this->getResult($requestUrl);
}
###########################################
# Channel commands
###########################################
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @return stdClass
*/
public function getEmbedTag($subject) {
$requestUrl = $this->buildRequestUrl($subject, 'getEmbedTag');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @param Number $width
* @param Number $height
* @param Boolean $autoPlay
* @param Boolean $mute
*
* @return stdClass
*/
public function getCustomEmbedTag($subject,$width = 100, $height = 100, $autoPlay = false, $mute = false) {
$parms['autoplay'] = $autoPlay;
$parms['mute'] = $mute;
$parms['width'] = $width;
$parms['height'] = $height;
$requestUrl = $this->buildRequestUrl($subject, 'getCustomEmbedTag', $parms);
return $this->getResult($requestUrl);
}
###########################################
# User commands
###########################################
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
*
* @return stdClass
*/
public function getId($subject) {
$requestUrl = $this->buildRequestUrl($subject, 'getId');
return $this->getResult($requestUrl);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @param Number $page
*
* @return stdClass
*/
public function listAllChannels($subject, $page = 1) {
$requestUrl = $this->buildRequestUrl($subject, 'listAllChannels');
return $this->getResult($requestUrl, $page);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @param Number $page
*
* @return stdClass
*/
public function listAllVideos($subject, $page = 1) {
$requestUrl = $this->buildRequestUrl($subject, 'listAllVideos');
return $this->getResult($requestUrl, $page);
}
/**
* @see http://developer.ustream.tv/data_api/docs
*
* @param String $subject
* @param Number $page
*
* @return stdClass
*/
public function getComments($subject, $page = 1) {
$requestUrl = $this->buildRequestUrl($subject, 'getComments');
return $this->getResult($requestUrl, $page);
}
###########################################
# Util methods
###########################################
/**
* isLive checks if the channel is broadcasting live at the moment
*
* @param String $channel
* @return Boolean
*/
public function isLive($channel) {
$channelData = $this->getInfo($channel);
if($channelData->results->status == 'live') {
return true;
} else {
false;
}
}
/**
* This method builds the request url to call the UStream API this builds all requests to UStream
*
* @param String $subject
* @param String $command
* @param Array $parms
* @return String
*/
private function buildRequestUrl($subject, $command, array $parms = array()) {
foreach($parms as $index => $value) {
$trueParms = !empty($trueParms) && $trueParms != null ? $trueParms .';' : $trueParms;
$trueParms .= sprintf("%s:%s",$index,$value);
}
$trueParms = !empty($trueParms) && $trueParms != null ? '¶ms=' . $trueParms: $trueParms;
return $this->calledUrl = sprintf("%s/%s/%s/%s/%s?key=%s%s",self::$apiUrl, self::$responseMode, self::$requestMode, $subject, $command, self::$apiKey, $trueParms);
}
###########################################
# Class methods
###########################################
/**
* Perform the call based in a requestUrl and than add the parm limit (see setLimit) and page as parm
*
* @param String $requestUrl
* @param Number $page
*
* @return stdClass
*/
public function getResult($requestUrl, $page = 1) {
$requestUrl = sprintf("$requestUrl&limit=%s&page=%s",$this->limit,$this->page);
$this->cacheResult = json_decode(file_get_contents($requestUrl));
if($this->error() == true) {
return false;
} else {
return $this->cacheResult;
}
}
/**
* It checks if after the getResult call there are some errors
*
* @return Boolean
*/
private function error() {
if($this->cacheResult->error != null) {
return true;
} else {
return false;
}
}
/**
* This method gets the error message and code
*
* @return Array
*/
public function getError() {
$return['code'] = $this->cacheResult->error;
$return['message'] = $this->cacheResult->msg;
return $return;
}
/**
* Set the mode of response from UStream API
*
* @param String $responseMode
*/
public function setResponseMode($responseMode) {
self::$responseMode = $responseMode;
}
/**
* Set the request mode: channel, user, system, video, stream
*
* @param String $requestMode
*/
public function setRequestMode($requestMode) {
self::$requestMode = $requestMode;
}
/**
* Define your UStream API Key
*
* @param String $apiKey
*/
public function setApiKey($apiKey) {
self::$apiKey = $apiKey;
}
}
?>