TheTVDBBridge.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class TheTVDBBridge extends BridgeAbstract {
  3. const MAINTAINER = 'Astyan';
  4. const NAME = 'TheTVDB';
  5. const URI = 'http://thetvdb.com/';
  6. const APIURI = 'https://api.thetvdb.com/';
  7. const CACHE_TIMEOUT = 43200; // 12h
  8. const DESCRIPTION = 'Returns latest episodes of a serie with theTVDB api. You can contribute to theTVDB.';
  9. const PARAMETERS = array(
  10. array(
  11. 'serie_id' => array(
  12. 'type' => 'number',
  13. 'name' => 'ID',
  14. 'required' => true,
  15. ),
  16. 'nb_episode' => array(
  17. 'type' => 'number',
  18. 'name' => 'Number of episodes',
  19. 'defaultValue' => 10,
  20. 'required' => true,
  21. ),
  22. )
  23. );
  24. const APIACCOUNT = 'RSSBridge';
  25. const APIKEY = '76DE1887EA401C9A';
  26. const APIUSERKEY = 'B52869AC6005330F';
  27. private function getApiUri(){
  28. return self::APIURI;
  29. }
  30. private function getToken(){
  31. //login and get token, don't use curlJob to do less adaptations
  32. $login_array = array(
  33. 'apikey' => self::APIKEY,
  34. 'username' => self::APIACCOUNT,
  35. 'userkey' => self::APIUSERKEY
  36. );
  37. $login_json = json_encode($login_array);
  38. $ch = curl_init($this->getApiUri() . 'login');
  39. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  40. curl_setopt($ch, CURLOPT_POSTFIELDS, $login_json);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  43. 'Content-Type: application/json',
  44. 'Accept: application/json'
  45. )
  46. );
  47. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  48. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  49. $result = curl_exec($ch);
  50. curl_close($ch);
  51. $token_json = (array)json_decode($result);
  52. if(isset($token_json['Error'])) {
  53. throw new Exception($token_json['Error']);
  54. die;
  55. }
  56. $token = $token_json['token'];
  57. return $token;
  58. }
  59. private function curlJob($token, $url){
  60. $token_header = 'Authorization: Bearer ' . $token;
  61. $ch = curl_init($url);
  62. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  65. 'Accept: application/json',
  66. $token_header
  67. )
  68. );
  69. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  70. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  71. $result = curl_exec($ch);
  72. curl_close($ch);
  73. $result_array = (array)json_decode($result);
  74. if(isset($result_array['Error'])) {
  75. throw new Exception($result_array['Error']);
  76. die;
  77. }
  78. return $result_array;
  79. }
  80. private function getLatestSeasonNumber($token, $serie_id){
  81. // get the last season
  82. $url = $this->getApiUri() . 'series/' . $serie_id . '/episodes/summary';
  83. $summary = $this->curlJob($token, $url);
  84. return max($summary['data']->airedSeasons);
  85. }
  86. private function getSerieName($token, $serie_id){
  87. $url = $this->getApiUri() . 'series/' . $serie_id;
  88. $serie = $this->curlJob($token, $url);
  89. return $serie['data']->seriesName;
  90. }
  91. private function getSeasonEpisodes($token,
  92. $serie_id,
  93. $season,
  94. $seriename,
  95. &$episodelist,
  96. $nbepisodemin,
  97. $page = 1){
  98. $url = $this->getApiUri()
  99. . 'series/'
  100. . $serie_id
  101. . '/episodes/query?airedSeason='
  102. . $season
  103. . '?page='
  104. . $page;
  105. $episodes = $this->curlJob($token, $url);
  106. // we don't check the number of page because we assume there is less
  107. //than 100 episodes in every season
  108. $episodes = (array)$episodes['data'];
  109. $episodes = array_slice($episodes, -$nbepisodemin, $nbepisodemin);
  110. foreach($episodes as $episode) {
  111. $episodedata = array();
  112. $episodedata['uri'] = $this->getURI()
  113. . '?tab=episode&seriesid='
  114. . $serie_id
  115. . '&seasonid='
  116. . $episode->airedSeasonID
  117. . '&id='
  118. . $episode->id;
  119. // check if the absoluteNumber exist
  120. if(isset($episode->absoluteNumber)) {
  121. $episodedata['title'] = 'S'
  122. . $episode->airedSeason
  123. . 'E'
  124. . $episode->airedEpisodeNumber
  125. . '('
  126. . $episode->absoluteNumber
  127. . ') : '
  128. . $episode->episodeName;
  129. } else {
  130. $episodedata['title'] = 'S'
  131. . $episode->airedSeason
  132. . 'E'
  133. . $episode->airedEpisodeNumber
  134. . ' : '
  135. . $episode->episodeName;
  136. }
  137. $episodedata['author'] = $seriename;
  138. $date = DateTime::createFromFormat(
  139. 'Y-m-d H:i:s',
  140. $episode->firstAired . ' 00:00:00'
  141. );
  142. $episodedata['timestamp'] = $date->getTimestamp();
  143. $episodedata['content'] = $episode->overview;
  144. $episodelist[] = $episodedata;
  145. }
  146. }
  147. public function collectData(){
  148. $serie_id = $this->getInput('serie_id');
  149. $nbepisode = $this->getInput('nb_episode');
  150. $episodelist = array();
  151. $token = $this->getToken();
  152. $maxseason = $this->getLatestSeasonNumber($token, $serie_id);
  153. $seriename = $this->getSerieName($token, $serie_id);
  154. $season = $maxseason;
  155. while(sizeof($episodelist) < $nbepisode && $season >= 1) {
  156. $nbepisodetmp = $nbepisode - sizeof($episodelist);
  157. $this->getSeasonEpisodes(
  158. $token,
  159. $serie_id,
  160. $season,
  161. $seriename,
  162. $episodelist,
  163. $nbepisodetmp
  164. );
  165. $season = $season - 1;
  166. }
  167. // add the 10 last specials episodes
  168. try { // catch to avoid error if empty
  169. $this->getSeasonEpisodes(
  170. $token,
  171. $serie_id,
  172. 0,
  173. $seriename,
  174. $episodelist,
  175. $nbepisode
  176. );
  177. } catch(Exception $e) {
  178. unset($e);
  179. }
  180. // sort and keep the 10 last episodes, works bad with the netflix serie
  181. // (all episode lauch at once)
  182. usort(
  183. $episodelist,
  184. function ($a, $b){
  185. return $a['timestamp'] < $b['timestamp'];
  186. }
  187. );
  188. $this->items = array_slice($episodelist, 0, $nbepisode);
  189. }
  190. }