TwitchApiBridge.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. define('TWITCH_LIMIT', 10); // The default limit
  3. define('TWITCH_BROADCASTS', 'false'); // The default flag for broadcasts
  4. class TwitchApiBridge extends BridgeAbstract{
  5. // for use in the getName function!
  6. private $channel;
  7. public function loadMetadatas() {
  8. $this->maintainer = "logmanoriginal";
  9. $this->name = "Twitch API Bridge";
  10. $this->uri = "http://www.twitch.tv";
  11. $this->description = "Returns the newest broadcasts or highlights by channel name using the Twitch API (v3)";
  12. $this->update = "2016-01-09";
  13. $this->parameters["Get channel without limit"] =
  14. '[
  15. {
  16. "name" : "Channel",
  17. "identifier" : "channel"
  18. },
  19. {
  20. "name" : "Broadcasts",
  21. "identifier" : "broadcasts",
  22. "type" : "list",
  23. "values" : [
  24. {
  25. "name" : "Show broadcasts",
  26. "value" : "true"
  27. },
  28. {
  29. "name" : "Show highlights",
  30. "value" : "false"
  31. }
  32. ]
  33. }
  34. ]';
  35. $this->parameters["Get channel with limit"] =
  36. '[
  37. {
  38. "name" : "Channel",
  39. "identifier" : "channel"
  40. },
  41. {
  42. "name" : "Limit",
  43. "identifier" : "limit",
  44. "type" : "number"
  45. },
  46. {
  47. "name" : "Broadcasts",
  48. "identifier" : "broadcasts",
  49. "type" : "list",
  50. "values" : [
  51. {
  52. "name" : "Show broadcasts",
  53. "value" : "true"
  54. },
  55. {
  56. "name" : "Show highlights",
  57. "value" : "false"
  58. }
  59. ]
  60. }
  61. ]';
  62. }
  63. public function collectData(array $param){
  64. /* In accordance with API description:
  65. * "When specifying a version for a request to the Twitch API, set the Accept HTTP header to the API version you prefer."
  66. * Now we prefer v3 right now and need to build the context options. */
  67. $opts = array('https' =>
  68. array(
  69. 'method' => 'GET',
  70. 'header' => 'Accept: application/vnd.twitchtv.v3+json'
  71. )
  72. );
  73. $context = stream_context_create($opts);
  74. $channel = '';
  75. $limit = TWITCH_LIMIT;
  76. $broadcasts = TWITCH_BROADCASTS;
  77. $requests = 1;
  78. if(isset($param['channel'])) {
  79. $channel = $param['channel'];
  80. } else {
  81. $this->returnError('You must specify a valid channel name! Received: &channel=' . $param['channel'], 400);
  82. }
  83. $this->channel = $channel;
  84. if(isset($param['limit'])) {
  85. try {
  86. $limit = (int)$param['limit'];
  87. } catch (Exception $e){
  88. $this->returnError('The limit you specified is not valid! Received: &limit=' . $param['limit'] . ' Expected: &limit=<num> where <num> is any integer number.', 400);
  89. }
  90. } else {
  91. $limit = TWITCH_LIMIT;
  92. }
  93. // 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.
  94. if($limit < 1) { $limit = 1; }
  95. if($limit > 100) {
  96. $requests = (int)($limit / 100);
  97. if($limit % 100 != 0) { $requests++; }
  98. }
  99. if(isset($param['broadcasts']) && ($param['broadcasts'] == 'true' || $param['broadcasts'] == 'false')) {
  100. $broadcasts = $param['broadcasts'];
  101. } else {
  102. $this->returnError('The value for broadcasts you specified is not valid! Received: &broadcasts=' . $param['broadcasts'] . ' Expected: &broadcasts=false or &broadcasts=true', 400);
  103. }
  104. // Build the initial request, see also: https://github.com/justintv/Twitch-API/blob/master/v3_resources/videos.md#get-channelschannelvideos
  105. $request = '';
  106. if($requests == 1) {
  107. $request = 'https://api.twitch.tv/kraken/channels/' . $channel . '/videos?limit=' . $limit . '&broadcasts=' . $broadcasts;
  108. } else {
  109. $request = 'https://api.twitch.tv/kraken/channels/' . $channel . '/videos?limit=100&broadcasts=' . $broadcasts;
  110. }
  111. /* Finally we're ready to request data from the API. Each response provides information for the next request. */
  112. for($i = 0; $i < $requests; $i++) {
  113. $response = file_get_contents($request, false, $context);
  114. if($response == false) {
  115. $this->returnError('Request failed! Check if the channel name is valid!', 400);
  116. }
  117. $data = json_decode($response);
  118. foreach($data->videos as $video) {
  119. $item = new \Item();
  120. $item->id = $video->_id;
  121. $item->uri = $video->url;
  122. $item->thumbnailUri = $video->preview;
  123. $item->title = htmlspecialchars($video->title);
  124. $item->timestamp = strtotime($video->recorded_at);
  125. $item->content = '<a href="' . $item->uri . '"><img src="' . $item->thumbnailUri . '" /></a><br><a href="' . $item->uri . '">' . $item->title . '</a>';
  126. $this->items[] = $item;
  127. // Stop once the number of requested items is reached
  128. if(count($this->items) >= $limit) {
  129. break;
  130. }
  131. }
  132. // Get next request (if available)
  133. if(isset($data->_links->next)) {
  134. $request = $data->_links->next;
  135. } else {
  136. break;
  137. }
  138. }
  139. }
  140. public function getName(){
  141. return (!empty($this->channel) ? $this->channel . ' - ' : '') . 'Twitch API Bridge';
  142. }
  143. public function getURI(){
  144. return 'https://www.twitch.tv';
  145. }
  146. public function getCacheDuration(){
  147. return 10800; // 3 hours
  148. }
  149. }
  150. ?>