TwitchApiBridge.php 3.3 KB

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