1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ritero\SDK\TwitchTV; |
4
|
|
|
|
5
|
|
|
use ritero\SDK\TwitchTV\Methods; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* TwitchTV API SDK for PHP |
9
|
|
|
* |
10
|
|
|
* PHP SDK for interacting with the TwitchTV API |
11
|
|
|
* |
12
|
|
|
* @author Josef Ohnheiser <[email protected]> |
13
|
|
|
* @license https://github.com/jofner/Twitch-SDK/blob/master/LICENSE.md MIT |
14
|
|
|
* @homepage https://github.com/jofner/Twitch-SDK |
15
|
|
|
* @version 2.0.0-dev |
16
|
|
|
* @todo refactoring for all methods of SDK, revision by methods |
17
|
|
|
*/ |
18
|
|
|
class TwitchSDK |
19
|
|
|
{ |
20
|
|
|
/** @var array */ |
21
|
|
|
private $config = array(); |
22
|
|
|
|
23
|
|
|
/** @var TwitchRequest */ |
24
|
|
|
protected $request; |
25
|
|
|
|
26
|
|
|
/** @var Helper */ |
27
|
|
|
protected $helper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* TwitchAPI URI's |
31
|
|
|
*/ |
32
|
|
|
const URI_STREAMS_SEARCH = 'search/streams/'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* TwitchSDK constructor |
36
|
|
|
* @param array $config |
37
|
|
|
* @throws TwitchException |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $config = array()) |
40
|
|
|
{ |
41
|
|
|
if (!in_array('curl', get_loaded_extensions())) { |
42
|
|
|
throw new TwitchException('cURL extension is not installed and is required'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (count($config) > 0) { |
46
|
|
|
$this->setConfig($config); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Develop workaround for requests |
51
|
|
|
* @todo class calls refactoring needed for future use |
52
|
|
|
*/ |
53
|
|
|
$this->request = new TwitchRequest; |
54
|
|
|
$this->helper = new Helper; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* config setter |
59
|
|
|
* @param array $config |
60
|
|
|
* @return TwitchSDK |
61
|
|
|
* @throws TwitchException |
62
|
|
|
*/ |
63
|
|
|
public function setConfig(array $config) |
64
|
|
|
{ |
65
|
|
|
if ($this->configValidate($config) === true) { |
66
|
|
|
$this->config = $config; |
67
|
|
|
} else { |
68
|
|
|
throw new TwitchException('Wrong Twitch API config parameters'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get value from config |
76
|
|
|
* @param string $key |
77
|
|
|
* @return mixed |
78
|
|
|
* @throws TwitchException |
79
|
|
|
*/ |
80
|
|
|
private function getConfigParam($key) |
81
|
|
|
{ |
82
|
|
|
if (!array_key_exists($key, $this->config)) { |
83
|
|
|
throw new TwitchException('Missing configuration parameter'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->config[$key]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Basic information about the API and authentication status |
91
|
|
|
* @param null $token |
92
|
|
|
* @return \stdClass |
93
|
|
|
* @throws TwitchException |
94
|
|
|
*/ |
95
|
|
|
public function status($token = null) |
96
|
|
|
{ |
97
|
|
|
$auth = null; |
98
|
|
|
|
99
|
|
|
if ($token !== null) { |
100
|
|
|
if (count($this->config) === 0) { |
101
|
|
|
$this->configException(); |
102
|
|
|
} else { |
103
|
|
|
$auth = $this->helper->buildQueryString(array('oauth_token' => $token)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->request->request($auth); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the specified user |
112
|
|
|
* @param $username |
113
|
|
|
* @return \stdClass |
114
|
|
|
* @throws TwitchException |
115
|
|
|
*/ |
116
|
|
|
public function userGet($username) |
117
|
|
|
{ |
118
|
|
|
$user = new Methods\User($this->request); |
119
|
|
|
return $user->getUser($username); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get a user's list of followed channels |
124
|
|
|
* @param string $user |
125
|
|
|
* @param integer $limit |
126
|
|
|
* @param integer $offset |
127
|
|
|
* @param string $direction |
128
|
|
|
* @param string $sortby |
129
|
|
|
* @return \stdClass |
130
|
|
|
* @throws TwitchException |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$queryString = $this->helper->buildQueryString(array( |
135
|
|
|
'limit' => $limit, |
136
|
|
|
'offset' => $offset, |
137
|
|
|
'direction' => $direction, |
138
|
|
|
'sortby' => $sortby, |
139
|
|
|
)); |
140
|
|
|
|
141
|
|
|
$follow = new Methods\Follow($this->request); |
142
|
|
|
|
143
|
|
|
return $follow->userFollowChannels($user, $queryString); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the status of a follow relationship |
148
|
|
|
* @param string $user |
149
|
|
|
* @param string $channel |
150
|
|
|
* @return \stdClass |
151
|
|
|
* @throws TwitchException |
152
|
|
|
*/ |
153
|
|
|
public function userFollowRelationship($user, $channel) |
154
|
|
|
{ |
155
|
|
|
$follow = new Methods\Follow($this->request); |
156
|
|
|
|
157
|
|
|
return $follow->userIsFollowingChannel($user, $channel); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set user to follow given channel |
162
|
|
|
* - requires scope 'user_follows_edit' |
163
|
|
|
* @param string $user |
164
|
|
|
* @param string $channel |
165
|
|
|
* @param string $userToken |
166
|
|
|
* @param bool $notifications |
167
|
|
|
* @return \stdClass |
168
|
|
|
* @throws TwitchException |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function userFollowChannel($user, $channel, $userToken, $notifications = false) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$queryString = $this->helper->buildQueryString(array( |
173
|
|
|
'oauth_token' => $userToken, |
174
|
|
|
'notifications' => $notifications, |
175
|
|
|
)); |
176
|
|
|
|
177
|
|
|
$follow = new Methods\Follow($this->request); |
178
|
|
|
|
179
|
|
|
return $follow->followChannel($user, $channel, $queryString); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set user to unfollow given channel |
184
|
|
|
* - requires scope 'user_follows_edit' |
185
|
|
|
* @param string $user |
186
|
|
|
* @param string $channel |
187
|
|
|
* @param string $userToken |
188
|
|
|
* @return \stdClass |
189
|
|
|
* @throws TwitchException |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function userUnfollowChannel($user, $channel, $userToken) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$queryString = $this->helper->buildQueryString(array( |
194
|
|
|
'oauth_token' => $userToken, |
195
|
|
|
)); |
196
|
|
|
|
197
|
|
|
$follow = new Methods\Follow($this->request); |
198
|
|
|
|
199
|
|
|
return $follow->unfollowChannel($user, $channel, $queryString); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the specified channel |
204
|
|
|
* @param string $channelName |
205
|
|
|
* @return \stdClass |
206
|
|
|
* @throws TwitchException |
207
|
|
|
*/ |
208
|
|
|
public function channelGet($channelName) |
209
|
|
|
{ |
210
|
|
|
$channel = new Methods\Channel($this->request); |
211
|
|
|
|
212
|
|
|
return $channel->getChannels($channelName); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Return team list for specified channel |
217
|
|
|
* @param string $channelName |
218
|
|
|
* @return \stdClass |
219
|
|
|
* @throws TwitchException |
220
|
|
|
*/ |
221
|
|
|
public function channelTeamsGet($channelName) |
222
|
|
|
{ |
223
|
|
|
$channel = new Methods\Channel($this->request); |
224
|
|
|
|
225
|
|
|
return $channel->getTeams($channelName); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get the specified team |
230
|
|
|
* @param $teamName |
231
|
|
|
* @return \stdClass |
232
|
|
|
* @throws TwitchException |
233
|
|
|
*/ |
234
|
|
|
public function teamGet($teamName) |
235
|
|
|
{ |
236
|
|
|
$team = new Methods\Team($this->request); |
237
|
|
|
|
238
|
|
|
return $team->getTeam($teamName); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns a list of active teams |
243
|
|
|
* @param integer $limit |
244
|
|
|
* @param integer $offset |
245
|
|
|
* @return \stdClass |
246
|
|
|
* @throws TwitchException |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function teamList($limit = null, $offset = null) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$queryString = $this->helper->buildQueryString(array( |
251
|
|
|
'limit' => $limit, |
252
|
|
|
'offset' => $offset, |
253
|
|
|
)); |
254
|
|
|
|
255
|
|
|
$team = new Methods\Team($this->request); |
256
|
|
|
|
257
|
|
|
return $team->getTeams($queryString); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get all team members |
262
|
|
|
* @param $teamName |
263
|
|
|
* @return mixed |
264
|
|
|
* @throws TwitchException |
265
|
|
|
*/ |
266
|
|
|
public function teamMembersAll($teamName) |
267
|
|
|
{ |
268
|
|
|
return $this->request->teamRequest($teamName . '/all_channels')->channels; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Returns an array of users who follow the specified channel |
273
|
|
|
* @param string $channelName |
274
|
|
|
* @param integer $limit |
275
|
|
|
* @param integer $offset |
276
|
|
|
* @param string $cursor |
277
|
|
|
* @param string $direction |
278
|
|
|
* @return \stdClass |
279
|
|
|
* @throws TwitchException |
280
|
|
|
*/ |
281
|
|
View Code Duplication |
public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
$queryString = $this->helper->buildQueryString(array( |
284
|
|
|
'limit' => $limit, |
285
|
|
|
'offset' => $offset, |
286
|
|
|
'cursor' => $cursor, |
287
|
|
|
'direction' => $direction, |
288
|
|
|
)); |
289
|
|
|
|
290
|
|
|
$follow = new Methods\Follow($this->request); |
291
|
|
|
|
292
|
|
|
return $follow->getChannelFollows($channelName, $queryString); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get the specified channel's stream |
297
|
|
|
* @param $channel |
298
|
|
|
* @return \stdClass |
299
|
|
|
* @throws TwitchException |
300
|
|
|
*/ |
301
|
|
|
public function streamGet($channel) |
302
|
|
|
{ |
303
|
|
|
$stream = new Methods\Stream($this->request); |
304
|
|
|
|
305
|
|
|
return $stream->getStream($channel); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Search live streams |
310
|
|
|
* @param $query |
311
|
|
|
* @param null $limit |
312
|
|
|
* @param null $offset |
313
|
|
|
* @return \stdClass |
314
|
|
|
* @throws TwitchException |
315
|
|
|
* @deprecated will be replaced by getStreams() function |
316
|
|
|
*/ |
317
|
|
View Code Duplication |
public function streamSearch($query, $limit = null, $offset = null) |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
$queryString = $this->helper->buildQueryString(array( |
320
|
|
|
'query' => $query, |
321
|
|
|
'limit' => $limit, |
322
|
|
|
'offset' => $offset, |
323
|
|
|
)); |
324
|
|
|
|
325
|
|
|
return $this->request->request(self::URI_STREAMS_SEARCH . $queryString); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Summarize streams |
330
|
|
|
* @param null $game |
331
|
|
|
* @param array|null $channels |
332
|
|
|
* @param null $hls |
333
|
|
|
* @return \stdClass |
334
|
|
|
* @throws TwitchException |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
if (!empty($channels)) { |
339
|
|
|
$channels = implode(',', $channels); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$queryString = $this->helper->buildQueryString(array( |
343
|
|
|
'game' => $game, |
344
|
|
|
'channel' => $channels, |
345
|
|
|
'hls' => $hls, |
346
|
|
|
)); |
347
|
|
|
|
348
|
|
|
$stream = new Methods\Stream($this->request); |
349
|
|
|
|
350
|
|
|
return $stream->getSummary($queryString); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Get featured streams |
355
|
|
|
* @param null $limit |
356
|
|
|
* @param null $offset |
357
|
|
|
* @param null $hls |
358
|
|
|
* @return \stdClass |
359
|
|
|
* @throws TwitchException |
360
|
|
|
*/ |
361
|
|
View Code Duplication |
public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
|
|
|
362
|
|
|
{ |
363
|
|
|
$queryString = $this->helper->buildQueryString(array( |
364
|
|
|
'limit' => $limit, |
365
|
|
|
'offset' => $offset, |
366
|
|
|
'hls' => $hls, |
367
|
|
|
)); |
368
|
|
|
|
369
|
|
|
$stream = new Methods\Stream($this->request); |
370
|
|
|
|
371
|
|
|
return $stream->getFeatured($queryString); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Get streams by channel |
376
|
|
|
* @param $channels |
377
|
|
|
* @param null $limit |
378
|
|
|
* @param null $offset |
379
|
|
|
* @param null $embeddable |
380
|
|
|
* @param null $hls |
381
|
|
|
* @return \stdClass |
382
|
|
|
* @deprecated will be replaced by getStreams() function |
383
|
|
|
*/ |
384
|
|
|
public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null) |
385
|
|
|
{ |
386
|
|
|
$channelsString = implode(',', $channels); |
387
|
|
|
|
388
|
|
|
return $this->getStreams(null, $limit, $offset, $channelsString, $embeddable, $hls); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Get streams by game |
393
|
|
|
* @param $game |
394
|
|
|
* @param null $limit |
395
|
|
|
* @param null $offset |
396
|
|
|
* @param null $embeddable |
397
|
|
|
* @param null $hls |
398
|
|
|
* @return \stdClass |
399
|
|
|
* @deprecated will be replaced by getStreams() function |
400
|
|
|
*/ |
401
|
|
|
public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null) |
402
|
|
|
{ |
403
|
|
|
return $this->getStreams($game, $limit, $offset, null, $embeddable, $hls); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get video |
408
|
|
|
* @param string $videoId |
409
|
|
|
* @return \stdClass |
410
|
|
|
* @throws TwitchException |
411
|
|
|
*/ |
412
|
|
|
public function videoGet($videoId) |
413
|
|
|
{ |
414
|
|
|
$video = new Methods\Video($this->request); |
415
|
|
|
|
416
|
|
|
return $video->getVideo($videoId); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Returns top videos |
421
|
|
|
* @param integer $limit |
422
|
|
|
* @param integer $offset |
423
|
|
|
* @param string $game |
424
|
|
|
* @param string $period |
425
|
|
|
* @return \stdClass |
426
|
|
|
* @throws TwitchException |
427
|
|
|
*/ |
428
|
|
|
public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
429
|
|
|
{ |
430
|
|
|
$queryString = $this->helper->buildQueryString(array( |
431
|
|
|
'limit' => $limit, |
432
|
|
|
'offset' => $offset, |
433
|
|
|
'game' => $game, |
434
|
|
|
'period' => $period, |
435
|
|
|
)); |
436
|
|
|
|
437
|
|
|
$video = new Methods\Video($this->request); |
438
|
|
|
|
439
|
|
|
return $video->getTop($queryString); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Get videos for a channel |
444
|
|
|
* @param $channel |
445
|
|
|
* @param null $limit |
446
|
|
|
* @param null $offset |
447
|
|
|
* @param bool $broadcasts |
448
|
|
|
* @param bool $hls |
449
|
|
|
* @return \stdClass |
450
|
|
|
* @throws TwitchException |
451
|
|
|
*/ |
452
|
|
View Code Duplication |
public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
|
|
|
453
|
|
|
{ |
454
|
|
|
$queryString = $this->helper->buildQueryString(array( |
455
|
|
|
'limit' => $limit, |
456
|
|
|
'offset' => $offset, |
457
|
|
|
'broadcasts' => $broadcasts, |
458
|
|
|
'hls' => $hls, |
459
|
|
|
)); |
460
|
|
|
|
461
|
|
|
$video = new Methods\Video($this->request); |
462
|
|
|
|
463
|
|
|
return $video->getChannelVideos($channel, $queryString); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Returns a links object to all other chat endpoints |
468
|
|
|
* @param string $channelName |
469
|
|
|
* @return \stdClass |
470
|
|
|
* @throws TwitchException |
471
|
|
|
*/ |
472
|
|
|
public function chatGet($channelName) |
473
|
|
|
{ |
474
|
|
|
$chat = new Methods\Chat($this->request); |
475
|
|
|
|
476
|
|
|
return $chat->getChat($channelName); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Get a chat's emoticons |
481
|
|
|
* @return \stdClass |
482
|
|
|
* @throws TwitchException |
483
|
|
|
*/ |
484
|
|
|
public function chatEmoticons() |
485
|
|
|
{ |
486
|
|
|
$chat = new Methods\Chat($this->request); |
487
|
|
|
|
488
|
|
|
return $chat->getEmoticons(); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Returns a list of emoticons |
493
|
|
|
* @param string $emoteset |
494
|
|
|
* @return \stdClass |
495
|
|
|
* @throws TwitchException |
496
|
|
|
*/ |
497
|
|
View Code Duplication |
public function chatEmoticonsImages($emoteset = null) |
|
|
|
|
498
|
|
|
{ |
499
|
|
|
$queryString = $this->helper->buildQueryString(array( |
500
|
|
|
'emotesets' => $emoteset, |
501
|
|
|
)); |
502
|
|
|
|
503
|
|
|
$chat = new Methods\Chat($this->request); |
504
|
|
|
|
505
|
|
|
return $chat->getEmoticonImages($queryString); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Returns a list of chat badges |
510
|
|
|
* @param string $channelName |
511
|
|
|
* @return \stdClass |
512
|
|
|
* @throws TwitchException |
513
|
|
|
*/ |
514
|
|
|
public function chatBadges($channelName) |
515
|
|
|
{ |
516
|
|
|
$chat = new Methods\Chat($this->request); |
517
|
|
|
|
518
|
|
|
return $chat->getBadges($channelName); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Get top games |
523
|
|
|
* @param integer $limit |
524
|
|
|
* @param integer $offset |
525
|
|
|
* @return \stdClass |
526
|
|
|
* @throws TwitchException |
527
|
|
|
*/ |
528
|
|
View Code Duplication |
public function gamesTop($limit = null, $offset = null) |
|
|
|
|
529
|
|
|
{ |
530
|
|
|
$queryString = $this->helper->buildQueryString(array( |
531
|
|
|
'limit' => $limit, |
532
|
|
|
'offset' => $offset, |
533
|
|
|
)); |
534
|
|
|
|
535
|
|
|
$game = new Methods\Game($this->request); |
536
|
|
|
|
537
|
|
|
return $game->getTop($queryString); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Get HTML code for stream embedding |
542
|
|
|
* @param $channel |
543
|
|
|
* @param int $width |
544
|
|
|
* @param int $height |
545
|
|
|
* @param int $volume |
546
|
|
|
* @return string |
547
|
|
|
*/ |
548
|
|
View Code Duplication |
public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
|
|
|
549
|
|
|
{ |
550
|
|
|
return '<object type="application/x-shockwave-flash" |
551
|
|
|
height="' . $height . '" |
552
|
|
|
width="' . $width . '" |
553
|
|
|
id="live_embed_player_flash" |
554
|
|
|
data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=' . $channel . '" |
555
|
|
|
bgcolor="#000000"> |
556
|
|
|
<param name="allowFullScreen" |
557
|
|
|
value="true" /> |
558
|
|
|
<param name="allowScriptAccess" |
559
|
|
|
value="always" /> |
560
|
|
|
<param name="allowNetworking" |
561
|
|
|
value="all" /> |
562
|
|
|
<param name="movie" |
563
|
|
|
value="http://www.twitch.tv/widgets/live_embed_player.swf" /> |
564
|
|
|
<param name="flashvars" |
565
|
|
|
value="hostname=www.twitch.tv&channel=' . $channel . '&auto_play=true&start_volume=' . $volume . '" /> |
566
|
|
|
</object>'; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Get HTML code for video embedding |
571
|
|
|
* @param $channel |
572
|
|
|
* @param $chapterid |
573
|
|
|
* @param int $width |
574
|
|
|
* @param int $height |
575
|
|
|
* @param int $volume |
576
|
|
|
* @return string |
577
|
|
|
*/ |
578
|
|
View Code Duplication |
public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
|
|
|
579
|
|
|
{ |
580
|
|
|
return '<object bgcolor="#000000" |
581
|
|
|
data="http://www.twitch.tv/widgets/archive_embed_player.swf" |
582
|
|
|
width="' . $width . '" |
583
|
|
|
height="' . $height . '" |
584
|
|
|
id="clip_embed_player_flash" |
585
|
|
|
type="application/x-shockwave-flash"> |
586
|
|
|
<param name="movie" |
587
|
|
|
value="http://www.twitch.tv/widgets/archive_embed_player.swf" /> |
588
|
|
|
<param name="allowScriptAccess" |
589
|
|
|
value="always" /> |
590
|
|
|
<param name="allowNetworking" |
591
|
|
|
value="all" /> |
592
|
|
|
<param name="allowFullScreen" |
593
|
|
|
value="true" /> |
594
|
|
|
<param name="flashvars" |
595
|
|
|
value="channel=' . $channel . '&start_volume=' . $volume . '&auto_play=false&chapter_id=' . $chapterid . '" /> |
596
|
|
|
</object>'; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* Get HTML code for chat embedding |
601
|
|
|
* @param $channel |
602
|
|
|
* @param int $width |
603
|
|
|
* @param int $height |
604
|
|
|
* @return string |
605
|
|
|
*/ |
606
|
|
|
public function embedChat($channel, $width = 400, $height = 300) |
607
|
|
|
{ |
608
|
|
|
return '<iframe frameborder="0" |
609
|
|
|
scrolling="no" |
610
|
|
|
id="chat_embed" |
611
|
|
|
src="http://twitch.tv/chat/embed?channel=' . $channel . '&popout_chat=true" |
612
|
|
|
height="' . $height . '" |
613
|
|
|
width="' . $width . '"> |
614
|
|
|
</iframe>'; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Get login URL for authentication |
619
|
|
|
* @param string $scope Specify which permissions your app requires (space separated list) |
620
|
|
|
* @return string |
621
|
|
|
* @throws TwitchException |
622
|
|
|
*/ |
623
|
|
View Code Duplication |
public function authLoginURL($scope) |
|
|
|
|
624
|
|
|
{ |
625
|
|
|
if (count($this->config) === 0) { |
626
|
|
|
$this->configException(); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
$queryString = $this->helper->buildQueryString(array( |
630
|
|
|
'response_type' => 'code', |
631
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
632
|
|
|
'redirect_uri' => $this->getConfigParam('redirect_uri'), |
633
|
|
|
'scope' => $scope, |
634
|
|
|
)); |
635
|
|
|
|
636
|
|
|
$auth = new Methods\Auth($this->request); |
637
|
|
|
|
638
|
|
|
return $auth->getLoginURL($queryString); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Get authentication access token |
643
|
|
|
* @param string $code returned after app authorization by user |
644
|
|
|
* @return \stdClass |
645
|
|
|
* @throws TwitchException |
646
|
|
|
*/ |
647
|
|
View Code Duplication |
public function authAccessTokenGet($code) |
|
|
|
|
648
|
|
|
{ |
649
|
|
|
if (count($this->config) === 0) { |
650
|
|
|
$this->configException(); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
$queryString = $this->helper->buildQueryString(array( |
654
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
655
|
|
|
'client_secret' => $this->getConfigParam('client_secret'), |
656
|
|
|
'grant_type' => 'authorization_code', |
657
|
|
|
'redirect_uri' => $this->getConfigParam('redirect_uri'), |
658
|
|
|
'code' => $code, |
659
|
|
|
)); |
660
|
|
|
|
661
|
|
|
$auth = new Methods\Auth($this->request); |
662
|
|
|
|
663
|
|
|
return $auth->getAccessToken($queryString); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* Get the authenticated user |
668
|
|
|
* - requires scope 'user_read' |
669
|
|
|
* @param string |
670
|
|
|
* @return \stdClass |
671
|
|
|
* @throws TwitchException |
672
|
|
|
*/ |
673
|
|
View Code Duplication |
public function authUserGet($token) |
|
|
|
|
674
|
|
|
{ |
675
|
|
|
if (count($this->config) === 0) { |
676
|
|
|
$this->configException(); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
$queryString = $this->helper->buildQueryString(array( |
680
|
|
|
'oauth_token' => $token, |
681
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
682
|
|
|
)); |
683
|
|
|
|
684
|
|
|
$user = new Methods\User($this->request); |
685
|
|
|
return $user->getUserAuth($queryString); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Get the authenticated channel |
690
|
|
|
* - requires scope 'channel_read' |
691
|
|
|
* @param string |
692
|
|
|
* @return \stdClass |
693
|
|
|
* @throws TwitchException |
694
|
|
|
*/ |
695
|
|
View Code Duplication |
public function authChannelGet($token) |
|
|
|
|
696
|
|
|
{ |
697
|
|
|
if (count($this->config) === 0) { |
698
|
|
|
$this->configException(); |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
$queryString = $this->helper->buildQueryString(array( |
702
|
|
|
'oauth_token' => $token, |
703
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
704
|
|
|
)); |
705
|
|
|
|
706
|
|
|
$channels = new Methods\Channel($this->request); |
707
|
|
|
|
708
|
|
|
return $channels->getChannel($queryString); |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Update channel's status or game |
713
|
|
|
* - requires scope 'channel_editor' |
714
|
|
|
* @param $token |
715
|
|
|
* @param string $channelName |
716
|
|
|
* @param string $status |
717
|
|
|
* @param string $game |
718
|
|
|
* @param integer $delay |
719
|
|
|
* @return \stdClass |
720
|
|
|
* @throws TwitchException |
721
|
|
|
*/ |
722
|
|
|
public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
723
|
|
|
{ |
724
|
|
|
if (count($this->config) === 0) { |
725
|
|
|
$this->configException(); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
$queryString = $this->helper->buildQueryString(array( |
729
|
|
|
'oauth_token' => $token, |
730
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
731
|
|
|
)); |
732
|
|
|
|
733
|
|
|
$data = $this->helper->buildQueryString(array( |
734
|
|
|
'channel[status]' => $status, |
735
|
|
|
'channel[game]' => $game, |
736
|
|
|
'channel[delay]' => $delay, |
737
|
|
|
)); |
738
|
|
|
|
739
|
|
|
$channel = new Methods\Channel($this->request); |
740
|
|
|
|
741
|
|
|
return $channel->setChannel($channelName, $queryString, $data); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Resets channel's stream key |
746
|
|
|
* - requires scope 'channel_stream' |
747
|
|
|
* @param string $token |
748
|
|
|
* @param string $channelName |
749
|
|
|
* @return \stdClass |
750
|
|
|
* @throws TwitchException |
751
|
|
|
*/ |
752
|
|
View Code Duplication |
public function authChannelResetKey($token, $channelName) |
|
|
|
|
753
|
|
|
{ |
754
|
|
|
if (count($this->config) === 0) { |
755
|
|
|
$this->configException(); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
$queryString = $this->helper->buildQueryString(array( |
759
|
|
|
'oauth_token' => $token, |
760
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
761
|
|
|
)); |
762
|
|
|
|
763
|
|
|
$channel = new Methods\Channel($this->request); |
764
|
|
|
|
765
|
|
|
return $channel->resetStreamKey($channelName, $queryString); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* Returns an array of users who are editors of specified channel |
770
|
|
|
* - requires scope 'channel_read' |
771
|
|
|
* @param string |
772
|
|
|
* @param string |
773
|
|
|
* @return \stdClass |
774
|
|
|
* @throws TwitchException |
775
|
|
|
*/ |
776
|
|
View Code Duplication |
public function authChannelEditors($token, $channel) |
|
|
|
|
777
|
|
|
{ |
778
|
|
|
if (count($this->config) === 0) { |
779
|
|
|
$this->configException(); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
$queryString = $this->helper->buildQueryString(array( |
783
|
|
|
'oauth_token' => $token, |
784
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
785
|
|
|
)); |
786
|
|
|
|
787
|
|
|
$channels = new Methods\Channel($this->request); |
788
|
|
|
|
789
|
|
|
return $channels->getEditors($channel, $queryString); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Returns an array of subscriptions who are subscribed to specified channel |
794
|
|
|
* - requires scope 'channel_subscriptions' |
795
|
|
|
* @param string $token - user's access token |
796
|
|
|
* @param string $channel |
797
|
|
|
* @param integer $limit - can be up to 100 |
798
|
|
|
* @param integer $offset |
799
|
|
|
* @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
800
|
|
|
* @return \stdClass |
801
|
|
|
* @throws TwitchException |
802
|
|
|
*/ |
803
|
|
View Code Duplication |
public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
|
|
|
804
|
|
|
{ |
805
|
|
|
if (count($this->config) === 0) { |
806
|
|
|
$this->configException(); |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
$queryString = $this->helper->buildQueryString(array( |
810
|
|
|
'oauth_token' => $token, |
811
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
812
|
|
|
'direction' => $direction, |
813
|
|
|
'limit' => $limit, |
814
|
|
|
'offset' => $offset |
815
|
|
|
)); |
816
|
|
|
|
817
|
|
|
$subscription = new Methods\Subscription($this->request); |
818
|
|
|
|
819
|
|
|
$subscription->getSubscriptions($channel, $queryString); |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Returns user object if that user is subscribed |
824
|
|
|
* - requires scope 'channel_check_subscription' for channel |
825
|
|
|
* @param string $token |
826
|
|
|
* @param string $channel |
827
|
|
|
* @param string $user |
828
|
|
|
* @return \stdClass |
829
|
|
|
* @throws TwitchException |
830
|
|
|
*/ |
831
|
|
View Code Duplication |
public function authSubscribedUser($token, $channel, $user) |
|
|
|
|
832
|
|
|
{ |
833
|
|
|
if (count($this->config) === 0) { |
834
|
|
|
$this->configException(); |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
$queryString = $this->helper->buildQueryString(array( |
838
|
|
|
'oauth_token' => $token, |
839
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
840
|
|
|
)); |
841
|
|
|
|
842
|
|
|
$subscription = new Methods\Subscription($this->request); |
843
|
|
|
|
844
|
|
|
return $subscription->getSubscribedUser($channel, $user, $queryString); |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
/** |
848
|
|
|
* Returns a channel object that user subscribes to |
849
|
|
|
* - requires scope 'user_subscriptions' for user |
850
|
|
|
* @param string $token |
851
|
|
|
* @param string $user |
852
|
|
|
* @param string $channel |
853
|
|
|
* @return \stdClass |
854
|
|
|
* @throws TwitchException |
855
|
|
|
*/ |
856
|
|
View Code Duplication |
public function authSubscribedToChannel($token, $user, $channel) |
|
|
|
|
857
|
|
|
{ |
858
|
|
|
if (count($this->config) === 0) { |
859
|
|
|
$this->configException(); |
860
|
|
|
} |
861
|
|
|
|
862
|
|
|
$queryString = $this->helper->buildQueryString(array( |
863
|
|
|
'oauth_token' => $token, |
864
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
865
|
|
|
)); |
866
|
|
|
|
867
|
|
|
$subscription = new Methods\Subscription($this->request); |
868
|
|
|
|
869
|
|
|
return $subscription->getSubscribedToChannel($user, $channel, $queryString); |
870
|
|
|
} |
871
|
|
|
|
872
|
|
|
/** |
873
|
|
|
* List the live streams that the authenticated user is following |
874
|
|
|
* - requires scope 'user_read' |
875
|
|
|
* @param string |
876
|
|
|
* @param integer $limit |
877
|
|
|
* @param integer $offset |
878
|
|
|
* @param bool $hls |
879
|
|
|
* @return \stdClass |
880
|
|
|
* @throws TwitchException |
881
|
|
|
*/ |
882
|
|
View Code Duplication |
public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
|
|
|
883
|
|
|
{ |
884
|
|
|
if (count($this->config) === 0) { |
885
|
|
|
$this->configException(); |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
$queryString = $this->helper->buildQueryString(array( |
889
|
|
|
'oauth_token' => $token, |
890
|
|
|
'client_id' => $this->getConfigParam('client_id'), |
891
|
|
|
'limit' => $limit, |
892
|
|
|
'offset' => $offset, |
893
|
|
|
'hls' => $hls, |
894
|
|
|
)); |
895
|
|
|
|
896
|
|
|
$user = new Methods\User($this->request); |
897
|
|
|
return $user->getFollowedStreams($queryString); |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
/** |
901
|
|
|
* Get streams helper |
902
|
|
|
* @param null $game |
903
|
|
|
* @param null $limit |
904
|
|
|
* @param null $offset |
905
|
|
|
* @param null $channels |
906
|
|
|
* @param null $embeddable |
907
|
|
|
* @param null $hls |
908
|
|
|
* @return \stdClass |
909
|
|
|
* @throws TwitchException |
910
|
|
|
*/ |
911
|
|
|
public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
912
|
|
|
{ |
913
|
|
|
$params = array( |
914
|
|
|
'game' => $game, |
915
|
|
|
'limit' => $limit, |
916
|
|
|
'offset' => $offset, |
917
|
|
|
'channel' => !empty($channels) ? $channels : null, |
918
|
|
|
'embeddable' => $embeddable, |
919
|
|
|
'hls' => $hls, |
920
|
|
|
); |
921
|
|
|
|
922
|
|
|
$queryString = $this->helper->buildQueryString($params); |
923
|
|
|
$stream = new Methods\Stream($this->request); |
924
|
|
|
|
925
|
|
|
return $stream->getStreams($queryString); |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* Validate parameters for authentication |
930
|
|
|
* @param array |
931
|
|
|
* @return boolean |
932
|
|
|
*/ |
933
|
|
|
private function configValidate($config) |
934
|
|
|
{ |
935
|
|
|
$check = array('client_id', 'client_secret', 'redirect_uri'); |
936
|
|
|
|
937
|
|
|
foreach ($check AS $val) { |
938
|
|
|
if (!array_key_exists($val, $config) || |
939
|
|
|
(empty($config[$val]) || |
940
|
|
|
!is_string($config[$val])) |
941
|
|
|
) { |
942
|
|
|
return false; |
943
|
|
|
} |
944
|
|
|
} |
945
|
|
|
|
946
|
|
|
return true; |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
/** |
950
|
|
|
* Configuration exception |
951
|
|
|
* @throws TwitchException |
952
|
|
|
*/ |
953
|
|
|
private function configException() |
954
|
|
|
{ |
955
|
|
|
throw new TwitchException('Cannot call authenticate functions without valid API configuration'); |
956
|
|
|
} |
957
|
|
|
} |
958
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.