From 1055fd4e8ba4f837fa7fcbacd3fe9e86aa0106f1 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 14 Jul 2015 13:36:44 +0200 Subject: [PATCH] Add TwitchApiBridge --- bridges/TwitchApiBridge.php | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 bridges/TwitchApiBridge.php diff --git a/bridges/TwitchApiBridge.php b/bridges/TwitchApiBridge.php new file mode 100644 index 00000000..19c9878c --- /dev/null +++ b/bridges/TwitchApiBridge.php @@ -0,0 +1,132 @@ + + array( + 'method' => 'GET', + 'header' => 'Accept: application/vnd.twitchtv.v3+json' + ) + ); + + $context = stream_context_create($opts); + + $channel = ''; + $limit = TWITCH_LIMIT; + $broadcasts = TWITCH_BROADCASTS; + $requests = 1; + + if(isset($param['channel'])) { + $channel = $param['channel']; + } else { + $this->returnError('You must specify a valid channel name! Received: &channel=' . $param['channel'], 400); + } + + $this->channel = $channel; + + if(isset($param['limit'])) { + try { + $limit = (int)$param['limit']; + } catch (Exception $e){ + $this->returnError('The limit you specified is not valid! Received: &limit=' . $param['limit'] . ' Expected: &limit= where is any integer number.', 400); + } + } else { + $limit = TWITCH_LIMIT; + } + + // 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. + if($limit < 1) { $limit = 1; } + if($limit > 100) { + $requests = (int)($limit / 100); + if($limit % 100 != 0) { $requests++; } + } + + if(isset($param['broadcasts']) && ($param['broadcasts'] == 'true' || $param['broadcasts'] == 'false')) { + $broadcasts = $param['broadcasts']; + } else { + $this->returnError('The value for broadcasts you specified is not valid! Received: &broadcasts=' . $param['broadcasts'] . ' Expected: &broadcasts=false or &broadcasts=true', 400); + } + + // Build the initial request, see also: https://github.com/justintv/Twitch-API/blob/master/v3_resources/videos.md#get-channelschannelvideos + $request = ''; + + if($requests == 1) { + $request = 'https://api.twitch.tv/kraken/channels/' . $channel . '/videos?limit=' . $limit . '&broadcasts=' . $broadcasts; + } else { + $request = 'https://api.twitch.tv/kraken/channels/' . $channel . '/videos?limit=100&broadcasts=' . $broadcasts; + } + + /* Finally we're ready to request data from the API. Each response provides information for the next request. */ + for($i = 0; $i < $requests; $i++) { + $response = file_get_contents($request, false, $context); + + if($response == false) { + $this->returnError('Request failed! Check if the channel name is valid!', 400); + } + + $data = json_decode($response); + + foreach($data->videos as $video) { + $item = new \Item(); + $item->id = $video->_id; + $item->uri = $video->url; + $item->thumbnailUri = $video->preview; + $item->title = htmlspecialchars($video->title); + $item->timestamp = strtotime($video->recorded_at); + $item->content = '
' . $item->title . ''; + $this->items[] = $item; + + // Stop once the number of requested items is reached + if(count($this->items) >= $limit) { + break; + } + } + + // Get next request (if available) + if(isset($data->_links->next)) { + $request = $data->_links->next; + } else { + break; + } + } + } + + public function getName(){ + return (!empty($this->channel) ? $this->channel . ' - ' : '') . 'Twitch API Bridge'; + } + + public function getURI(){ + return 'https://www.twitch.tv'; + } + + public function getCacheDuration(){ + return 10800; // 3 hours + } +} +?> \ No newline at end of file