2015-07-14 14:36:44 +03:00
|
|
|
<?php
|
2015-11-04 12:47:21 +03:00
|
|
|
define('TWITCH_LIMIT', 10); // The default limit
|
2015-07-14 14:36:44 +03:00
|
|
|
|
|
|
|
class TwitchApiBridge extends BridgeAbstract{
|
2015-11-05 21:05:46 +03:00
|
|
|
|
2016-08-27 22:03:26 +03:00
|
|
|
public $maintainer = "logmanoriginal";
|
|
|
|
public $name = "Twitch API Bridge";
|
|
|
|
public $uri = "http://www.twitch.tv";
|
|
|
|
public $description = "Returns the newest broadcasts or highlights by channel name using the Twitch API (v3)";
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2016-08-27 22:03:26 +03:00
|
|
|
public $parameters = array(
|
|
|
|
'Show Channel Videos'=>array(
|
2016-08-27 19:06:19 +03:00
|
|
|
'channel'=>array(
|
|
|
|
'name'=>'Channel',
|
|
|
|
'required'=>true
|
|
|
|
),
|
|
|
|
'broadcasts'=>array(
|
|
|
|
'name'=>'Show Broadcasts rather than Highlights',
|
|
|
|
'type'=>'checkbox'
|
|
|
|
),
|
|
|
|
'limit'=>array(
|
|
|
|
'name'=>'Limit',
|
|
|
|
'type'=>'number'
|
2016-08-22 02:25:56 +03:00
|
|
|
)
|
2016-08-27 22:03:26 +03:00
|
|
|
)
|
|
|
|
);
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2015-07-14 14:36:44 +03:00
|
|
|
|
2016-08-22 02:25:56 +03:00
|
|
|
/* In accordance with API description:
|
|
|
|
* "When specifying a version for a request to the Twitch API, set the Accept HTTP header to the API version you prefer."
|
2015-07-14 14:36:44 +03:00
|
|
|
* Now we prefer v3 right now and need to build the context options. */
|
|
|
|
$opts = array('https' =>
|
|
|
|
array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'header' => 'Accept: application/vnd.twitchtv.v3+json'
|
|
|
|
)
|
|
|
|
);
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
$context = stream_context_create($opts);
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2016-08-28 02:25:33 +03:00
|
|
|
if(!isset($this->getInput('limit')) ||
|
|
|
|
empty($this->getInput('limit'))){
|
2015-07-14 14:36:44 +03:00
|
|
|
$limit = TWITCH_LIMIT;
|
2016-08-27 19:06:19 +03:00
|
|
|
}else{
|
2016-08-28 02:25:33 +03:00
|
|
|
$limit = (int)$this->getInput('limit');
|
2015-07-14 14:36:44 +03:00
|
|
|
}
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
// The Twitch API allows a limit between 1 .. 100. Therefore any value below must be set to 1, any greater must result in multiple requests.
|
2016-08-27 19:06:19 +03:00
|
|
|
$requests=1;
|
2015-07-14 14:36:44 +03:00
|
|
|
if($limit < 1) { $limit = 1; }
|
2016-08-22 02:25:56 +03:00
|
|
|
if($limit > 100) {
|
|
|
|
$requests = (int)($limit / 100);
|
2015-07-14 14:36:44 +03:00
|
|
|
if($limit % 100 != 0) { $requests++; }
|
|
|
|
}
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2016-08-28 02:25:33 +03:00
|
|
|
if($this->getInput('broadcasts')){
|
2016-08-27 19:06:19 +03:00
|
|
|
$broadcasts='true';
|
|
|
|
}else{
|
|
|
|
$broadcasts='false';
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
// Build the initial request, see also: https://github.com/justintv/Twitch-API/blob/master/v3_resources/videos.md#get-channelschannelvideos
|
|
|
|
$request = '';
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
if($requests == 1) {
|
2016-08-28 02:25:33 +03:00
|
|
|
$request = 'https://api.twitch.tv/kraken/channels/' . $this->getInput('channel') . '/videos?limit=' . $limit . '&broadcasts=' . $broadcasts;
|
2015-07-14 14:36:44 +03:00
|
|
|
} else {
|
2016-08-28 02:25:33 +03:00
|
|
|
$request = 'https://api.twitch.tv/kraken/channels/' . $this->getInput('channel') . '/videos?limit=100&broadcasts=' . $broadcasts;
|
2015-07-14 14:36:44 +03:00
|
|
|
}
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
/* Finally we're ready to request data from the API. Each response provides information for the next request. */
|
|
|
|
for($i = 0; $i < $requests; $i++) {
|
2016-08-22 01:04:15 +03:00
|
|
|
$response = $this->getSimpleHTMLDOM($request, false, $context);
|
2016-08-22 02:25:56 +03:00
|
|
|
|
|
|
|
if($response == false) {
|
|
|
|
$this->returnServerError('Request failed! Check if the channel name is valid!');
|
2015-07-14 14:36:44 +03:00
|
|
|
}
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
$data = json_decode($response);
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
foreach($data->videos as $video) {
|
2016-08-22 19:55:59 +03:00
|
|
|
$item = array();
|
|
|
|
$item['id'] = $video->_id;
|
|
|
|
$item['uri'] = $video->url;
|
|
|
|
$item['title'] = htmlspecialchars($video->title);
|
|
|
|
$item['timestamp'] = strtotime($video->recorded_at);
|
|
|
|
$item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $video->preview . '" /></a><br><a href="' . $item['uri'] . '">' . $item['title'] . '</a>';
|
2015-07-14 14:36:44 +03:00
|
|
|
$this->items[] = $item;
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
// Stop once the number of requested items is reached
|
|
|
|
if(count($this->items) >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 02:25:56 +03:00
|
|
|
|
2015-07-14 14:36:44 +03:00
|
|
|
// Get next request (if available)
|
|
|
|
if(isset($data->_links->next)) {
|
|
|
|
$request = $data->_links->next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(){
|
2016-08-28 02:25:33 +03:00
|
|
|
return $this->getInput('channel') . ' - Twitch API Bridge';
|
2015-07-14 14:36:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration(){
|
|
|
|
return 10800; // 3 hours
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 12:29:25 +03:00
|
|
|
?>
|