sickrage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. trait SickRageHomepageItem
  3. {
  4. public function testConnectionSickRage()
  5. {
  6. if (empty($this->config['sickrageURL'])) {
  7. $this->setAPIResponse('error', 'SickRage URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['sickrageToken'])) {
  11. $this->setAPIResponse('error', 'SickRage Token is not defined', 422);
  12. return false;
  13. }
  14. $failed = false;
  15. $errors = '';
  16. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  17. foreach ($list as $key => $value) {
  18. try {
  19. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  20. $results = $downloader->sb();
  21. $downloadList = json_decode($results, true);
  22. if (is_array($downloadList) || is_object($downloadList)) {
  23. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  24. if (!is_array($queue)) {
  25. $ip = $value['url'];
  26. $errors .= $ip . ': ' . $queue;
  27. $failed = true;
  28. }
  29. } else {
  30. $ip = $value['url'];
  31. $errors .= $ip . ': Response was not JSON';
  32. $failed = true;
  33. }
  34. } catch (Exception $e) {
  35. $failed = true;
  36. $ip = $value['url'];
  37. $errors .= $ip . ': ' . $e->getMessage();
  38. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  39. }
  40. }
  41. if ($failed) {
  42. $this->setAPIResponse('error', $errors, 500);
  43. return false;
  44. } else {
  45. $this->setAPIResponse('success', null, 200);
  46. return true;
  47. }
  48. }
  49. public function getSickRageCalendar($startDate = null, $endDate = null)
  50. {
  51. if (!$this->config['homepageSickrageEnabled']) {
  52. $this->setAPIResponse('error', 'SickRage homepage item is not enabled', 409);
  53. return false;
  54. }
  55. if (!$this->qualifyRequest($this->config['homepageSickrageAuth'])) {
  56. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  57. return false;
  58. }
  59. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  60. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  61. return false;
  62. }
  63. if (empty($this->config['sickrageURL'])) {
  64. $this->setAPIResponse('error', 'SickRage URL is not defined', 422);
  65. return false;
  66. }
  67. if (empty($this->config['sickrageToken'])) {
  68. $this->setAPIResponse('error', 'SickRage Token is not defined', 422);
  69. return false;
  70. }
  71. $calendarItems = array();
  72. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  73. foreach ($list as $key => $value) {
  74. try {
  75. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  76. $sickrageFuture = $this->formatSickrageCalendarWanted($downloader->future(), $key);
  77. $sickrageHistory = $this->formatSickrageCalendarHistory($downloader->history("100", "downloaded"), $key);
  78. if (!empty($sickrageFuture)) {
  79. $calendarItems = array_merge($calendarItems, $sickrageFuture);
  80. }
  81. if (!empty($sickrageHistory)) {
  82. $calendarItems = array_merge($calendarItems, $sickrageHistory);
  83. }
  84. } catch (Exception $e) {
  85. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  86. }
  87. }
  88. $this->setAPIResponse('success', null, 200, $calendarItems);
  89. return $calendarItems;
  90. }
  91. public function formatSickrageCalendarWanted($array, $number)
  92. {
  93. $array = json_decode($array, true);
  94. $gotCalendar = array();
  95. $i = 0;
  96. foreach ($array['data']['missed'] as $child) {
  97. $i++;
  98. $seriesName = $child['show_name'];
  99. $seriesID = $child['tvdbid'];
  100. $episodeID = $child['tvdbid'];
  101. $episodeAirDate = $child['airdate'];
  102. $episodeAirDateTime = explode(" ", $child['airs']);
  103. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  104. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  105. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  106. if (new DateTime() < new DateTime($episodeAirDate)) {
  107. $unaired = true;
  108. }
  109. $downloaded = "0";
  110. if ($downloaded == "0" && isset($unaired)) {
  111. $downloaded = "text-info";
  112. } elseif ($downloaded == "1") {
  113. $downloaded = "text-success";
  114. } else {
  115. $downloaded = "text-danger";
  116. }
  117. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  118. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  119. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  120. $fanart = "/plugins/images/cache/no-np.png";
  121. if (file_exists($cacheFile)) {
  122. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  123. unset($cacheFile);
  124. }
  125. $details = array(
  126. "seasonCount" => "",
  127. "status" => $child['show_status'],
  128. "topTitle" => $seriesName,
  129. "bottomTitle" => $bottomTitle,
  130. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  131. "runtime" => "",
  132. "image" => $fanart,
  133. "ratings" => "",
  134. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  135. "audioChannels" => "",
  136. "audioCodec" => "",
  137. "videoCodec" => "",
  138. "size" => "",
  139. "genres" => "",
  140. );
  141. array_push($gotCalendar, array(
  142. "id" => "Sick-" . $number . "-Miss-" . $i,
  143. "title" => $seriesName,
  144. "start" => $episodeAirDate,
  145. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  146. "imagetype" => "tv " . $downloaded,
  147. "imagetypeFilter" => "tv",
  148. "downloadFilter" => $downloaded,
  149. "bgColor" => str_replace('text', 'bg', $downloaded),
  150. "details" => $details,
  151. ));
  152. }
  153. foreach ($array['data']['today'] as $child) {
  154. $i++;
  155. $seriesName = $child['show_name'];
  156. $seriesID = $child['tvdbid'];
  157. $episodeID = $child['tvdbid'];
  158. $episodeAirDate = $child['airdate'];
  159. $episodeAirDateTime = explode(" ", $child['airs']);
  160. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  161. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  162. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  163. if (new DateTime() < new DateTime($episodeAirDate)) {
  164. $unaired = true;
  165. }
  166. $downloaded = "0";
  167. if ($downloaded == "0" && isset($unaired)) {
  168. $downloaded = "text-info";
  169. } elseif ($downloaded == "1") {
  170. $downloaded = "text-success";
  171. } else {
  172. $downloaded = "text-danger";
  173. }
  174. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  175. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  176. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  177. $fanart = "/plugins/images/cache/no-np.png";
  178. if (file_exists($cacheFile)) {
  179. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  180. unset($cacheFile);
  181. }
  182. $details = array(
  183. "seasonCount" => "",
  184. "status" => $child['show_status'],
  185. "topTitle" => $seriesName,
  186. "bottomTitle" => $bottomTitle,
  187. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  188. "runtime" => "",
  189. "image" => $fanart,
  190. "ratings" => "",
  191. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  192. "audioChannels" => "",
  193. "audioCodec" => "",
  194. "videoCodec" => "",
  195. "size" => "",
  196. "genres" => "",
  197. );
  198. array_push($gotCalendar, array(
  199. "id" => "Sick-" . $number . "-Today-" . $i,
  200. "title" => $seriesName,
  201. "start" => $episodeAirDate,
  202. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  203. "imagetype" => "tv " . $downloaded,
  204. "imagetypeFilter" => "tv",
  205. "downloadFilter" => $downloaded,
  206. "bgColor" => str_replace('text', 'bg', $downloaded),
  207. "details" => $details,
  208. ));
  209. }
  210. foreach ($array['data']['soon'] as $child) {
  211. $i++;
  212. $seriesName = $child['show_name'];
  213. $seriesID = $child['tvdbid'];
  214. $episodeID = $child['tvdbid'];
  215. $episodeAirDate = $child['airdate'];
  216. $episodeAirDateTime = explode(" ", $child['airs']);
  217. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  218. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  219. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  220. if (new DateTime() < new DateTime($episodeAirDate)) {
  221. $unaired = true;
  222. }
  223. $downloaded = "0";
  224. if ($downloaded == "0" && isset($unaired)) {
  225. $downloaded = "text-info";
  226. } elseif ($downloaded == "1") {
  227. $downloaded = "text-success";
  228. } else {
  229. $downloaded = "text-danger";
  230. }
  231. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  232. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  233. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  234. $fanart = "/plugins/images/cache/no-np.png";
  235. if (file_exists($cacheFile)) {
  236. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  237. unset($cacheFile);
  238. }
  239. $details = array(
  240. "seasonCount" => "",
  241. "status" => $child['show_status'],
  242. "topTitle" => $seriesName,
  243. "bottomTitle" => $bottomTitle,
  244. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  245. "runtime" => "",
  246. "image" => $fanart,
  247. "ratings" => "",
  248. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  249. "audioChannels" => "",
  250. "audioCodec" => "",
  251. "videoCodec" => "",
  252. "size" => "",
  253. "genres" => "",
  254. );
  255. array_push($gotCalendar, array(
  256. "id" => "Sick-" . $number . "-Soon-" . $i,
  257. "title" => $seriesName,
  258. "start" => $episodeAirDate,
  259. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  260. "imagetype" => "tv " . $downloaded,
  261. "imagetypeFilter" => "tv",
  262. "downloadFilter" => $downloaded,
  263. "bgColor" => str_replace('text', 'bg', $downloaded),
  264. "details" => $details,
  265. ));
  266. }
  267. foreach ($array['data']['later'] as $child) {
  268. $i++;
  269. $seriesName = $child['show_name'];
  270. $seriesID = $child['tvdbid'];
  271. $episodeID = $child['tvdbid'];
  272. $episodeAirDate = $child['airdate'];
  273. $episodeAirDateTime = explode(" ", $child['airs']);
  274. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  275. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  276. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  277. if (new DateTime() < new DateTime($episodeAirDate)) {
  278. $unaired = true;
  279. }
  280. $downloaded = "0";
  281. if ($downloaded == "0" && isset($unaired)) {
  282. $downloaded = "text-info";
  283. } elseif ($downloaded == "1") {
  284. $downloaded = "text-success";
  285. } else {
  286. $downloaded = "text-danger";
  287. }
  288. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  289. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  290. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  291. $fanart = "/plugins/images/cache/no-np.png";
  292. if (file_exists($cacheFile)) {
  293. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  294. unset($cacheFile);
  295. }
  296. $details = array(
  297. "seasonCount" => "",
  298. "status" => $child['show_status'],
  299. "topTitle" => $seriesName,
  300. "bottomTitle" => $bottomTitle,
  301. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  302. "runtime" => "",
  303. "image" => $fanart,
  304. "ratings" => "",
  305. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  306. "audioChannels" => "",
  307. "audioCodec" => "",
  308. "videoCodec" => "",
  309. "size" => "",
  310. "genres" => "",
  311. );
  312. array_push($gotCalendar, array(
  313. "id" => "Sick-" . $number . "-Later-" . $i,
  314. "title" => $seriesName,
  315. "start" => $episodeAirDate,
  316. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  317. "imagetype" => "tv " . $downloaded,
  318. "imagetypeFilter" => "tv",
  319. "downloadFilter" => $downloaded,
  320. "bgColor" => str_replace('text', 'bg', $downloaded),
  321. "details" => $details,
  322. ));
  323. }
  324. if ($i != 0) {
  325. return $gotCalendar;
  326. }
  327. return false;
  328. }
  329. public function formatSickrageCalendarHistory($array, $number)
  330. {
  331. $array = json_decode($array, true);
  332. $gotCalendar = array();
  333. $i = 0;
  334. foreach ($array['data'] as $child) {
  335. $i++;
  336. $seriesName = $child['show_name'];
  337. $seriesID = $child['tvdbid'];
  338. $episodeID = $child['tvdbid'];
  339. $episodeAirDate = $child['date'];
  340. $downloaded = "text-success";
  341. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']);
  342. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  343. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  344. $fanart = "/plugins/images/cache/no-np.png";
  345. if (file_exists($cacheFile)) {
  346. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  347. unset($cacheFile);
  348. }
  349. $details = array(
  350. "seasonCount" => "",
  351. "status" => $child['status'],
  352. "topTitle" => $seriesName,
  353. "bottomTitle" => $bottomTitle,
  354. "overview" => '',
  355. "runtime" => isset($child['series']['runtime']) ? $child['series']['runtime'] : 30,
  356. "image" => $fanart,
  357. "ratings" => isset($child['series']['ratings']['value']) ? $child['series']['ratings']['value'] : "unknown",
  358. "videoQuality" => isset($child["quality"]) ? $child['quality'] : "unknown",
  359. "audioChannels" => "",
  360. "audioCodec" => "",
  361. "videoCodec" => "",
  362. "size" => "",
  363. "genres" => "",
  364. );
  365. array_push($gotCalendar, array(
  366. "id" => "Sick-" . $number . "-History-" . $i,
  367. "title" => $seriesName,
  368. "start" => $episodeAirDate,
  369. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  370. "imagetype" => "tv " . $downloaded,
  371. "imagetypeFilter" => "tv",
  372. "downloadFilter" => $downloaded,
  373. "bgColor" => str_replace('text', 'bg', $downloaded),
  374. "details" => $details,
  375. ));
  376. }
  377. if ($i != 0) {
  378. return $gotCalendar;
  379. }
  380. return false;
  381. }
  382. }