TwitchApiBridge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. define('TWITCH_LIMIT', 10); // The default limit
  3. class TwitchApiBridge extends BridgeAbstract{
  4. const MAINTAINER = "logmanoriginal";
  5. const NAME = "Twitch API Bridge";
  6. const URI = "http://www.twitch.tv";
  7. const CACHE_TIMEOUT = 10800; // 3h
  8. const DESCRIPTION = "Returns the newest broadcasts or highlights by channel name using the Twitch API (v3)";
  9. const PARAMETERS = array(
  10. 'Show Channel Videos'=>array(
  11. 'channel'=>array(
  12. 'name'=>'Channel',
  13. 'required'=>true
  14. ),
  15. 'broadcasts'=>array(
  16. 'name'=>'Show Broadcasts rather than Highlights',
  17. 'type'=>'checkbox'
  18. ),
  19. 'limit'=>array(
  20. 'name'=>'Limit',
  21. 'type'=>'number'
  22. )
  23. )
  24. );
  25. public function collectData(){
  26. /* In accordance with API description:
  27. * "When specifying a version for a request to the Twitch API, set the Accept HTTP header to the API version you prefer."
  28. * Now we prefer v3 right now and need to build the context options. */
  29. $opts = array('https' =>
  30. array(
  31. 'method' => 'GET',
  32. 'header' => 'Accept: application/vnd.twitchtv.v3+json'
  33. )
  34. );
  35. $context = stream_context_create($opts);
  36. $limit = $this->getInput('limit');
  37. if(!$limit){
  38. $limit = TWITCH_LIMIT;
  39. }
  40. // 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.
  41. $requests=1;
  42. if($limit < 1) { $limit = 1; }
  43. if($limit > 100) {
  44. $requests = (int)($limit / 100);
  45. if($limit % 100 != 0) { $requests++; }
  46. }
  47. if($this->getInput('broadcasts')){
  48. $broadcasts='true';
  49. }else{
  50. $broadcasts='false';
  51. }
  52. // Build the initial request, see also: https://github.com/justintv/Twitch-API/blob/master/v3_resources/videos.md#get-channelschannelvideos
  53. $request = '';
  54. if($requests == 1) {
  55. $request = 'https://api.twitch.tv/kraken/channels/' . $this->getInput('channel') . '/videos?limit=' . $limit . '&broadcasts=' . $broadcasts;
  56. } else {
  57. $request = 'https://api.twitch.tv/kraken/channels/' . $this->getInput('channel') . '/videos?limit=100&broadcasts=' . $broadcasts;
  58. }
  59. /* Finally we're ready to request data from the API. Each response provides information for the next request. */
  60. for($i = 0; $i < $requests; $i++) {
  61. $response = getSimpleHTMLDOM($request, false, $context);
  62. if($response == false) {
  63. returnServerError('Request failed! Check if the channel name is valid!');
  64. }
  65. $data = json_decode($response);
  66. foreach($data->videos as $video) {
  67. $item = array();
  68. $item['id'] = $video->_id;
  69. $item['uri'] = $video->url;
  70. $item['title'] = htmlspecialchars($video->title);
  71. $item['timestamp'] = strtotime($video->recorded_at);
  72. $item['content'] = '<a href="' . $item['uri'] . '"><img src="' . $video->preview . '" /></a><br><a href="' . $item['uri'] . '">' . $item['title'] . '</a>';
  73. $this->items[] = $item;
  74. // Stop once the number of requested items is reached
  75. if(count($this->items) >= $limit) {
  76. break;
  77. }
  78. }
  79. // Get next request (if available)
  80. if(isset($data->_links->next)) {
  81. $request = $data->_links->next;
  82. } else {
  83. break;
  84. }
  85. }
  86. }
  87. public function getName(){
  88. return $this->getInput('channel') . ' - Twitch API Bridge';
  89. }
  90. }
  91. ?>