sickrage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. trait SickRageHomepageItem
  3. {
  4. public function sickrageSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'SickRage',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sickrage.png',
  10. 'category' => 'PVR',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageSickrageEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageSickrageEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageSickrageAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageSickrageAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'sickrageURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['sickrageURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'sickrageToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['sickrageToken']
  41. )
  42. ),
  43. 'Misc Options' => array(
  44. array(
  45. 'type' => 'select',
  46. 'name' => 'calendarFirstDay',
  47. 'label' => 'Start Day',
  48. 'value' => $this->config['calendarFirstDay'],
  49. 'options' => $this->daysOptions()
  50. ),
  51. array(
  52. 'type' => 'select',
  53. 'name' => 'calendarDefault',
  54. 'label' => 'Default View',
  55. 'value' => $this->config['calendarDefault'],
  56. 'options' => $this->calendarDefaultOptions()
  57. ),
  58. array(
  59. 'type' => 'select',
  60. 'name' => 'calendarTimeFormat',
  61. 'label' => 'Time Format',
  62. 'value' => $this->config['calendarTimeFormat'],
  63. 'options' => $this->timeFormatOptions()
  64. ),
  65. array(
  66. 'type' => 'select',
  67. 'name' => 'calendarLocale',
  68. 'label' => 'Locale',
  69. 'value' => $this->config['calendarLocale'],
  70. 'options' => $this->calendarLocaleOptions()
  71. ),
  72. array(
  73. 'type' => 'select',
  74. 'name' => 'calendarLimit',
  75. 'label' => 'Items Per Day',
  76. 'value' => $this->config['calendarLimit'],
  77. 'options' => $this->limitOptions()
  78. ),
  79. array(
  80. 'type' => 'select',
  81. 'name' => 'calendarRefresh',
  82. 'label' => 'Refresh Seconds',
  83. 'value' => $this->config['calendarRefresh'],
  84. 'options' => $this->timeOptions()
  85. )
  86. ),
  87. 'Test Connection' => array(
  88. array(
  89. 'type' => 'blank',
  90. 'label' => 'Please Save before Testing'
  91. ),
  92. array(
  93. 'type' => 'button',
  94. 'label' => '',
  95. 'icon' => 'fa fa-flask',
  96. 'class' => 'pull-right',
  97. 'text' => 'Test Connection',
  98. 'attr' => 'onclick="testAPIConnection(\'sickrage\')"'
  99. ),
  100. )
  101. )
  102. );
  103. }
  104. public function testConnectionSickRage()
  105. {
  106. if (empty($this->config['sickrageURL'])) {
  107. $this->setAPIResponse('error', 'SickRage URL is not defined', 422);
  108. return false;
  109. }
  110. if (empty($this->config['sickrageToken'])) {
  111. $this->setAPIResponse('error', 'SickRage Token is not defined', 422);
  112. return false;
  113. }
  114. $failed = false;
  115. $errors = '';
  116. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  117. foreach ($list as $key => $value) {
  118. try {
  119. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  120. $results = $downloader->sb();
  121. $downloadList = json_decode($results, true);
  122. if (is_array($downloadList) || is_object($downloadList)) {
  123. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  124. if (!is_array($queue)) {
  125. $ip = $value['url'];
  126. $errors .= $ip . ': ' . $queue;
  127. $failed = true;
  128. }
  129. } else {
  130. $ip = $value['url'];
  131. $errors .= $ip . ': Response was not JSON';
  132. $failed = true;
  133. }
  134. } catch (Exception $e) {
  135. $failed = true;
  136. $ip = $value['url'];
  137. $errors .= $ip . ': ' . $e->getMessage();
  138. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  139. }
  140. }
  141. if ($failed) {
  142. $this->setAPIResponse('error', $errors, 500);
  143. return false;
  144. } else {
  145. $this->setAPIResponse('success', null, 200);
  146. return true;
  147. }
  148. }
  149. public function getSickRageCalendar($startDate = null, $endDate = null)
  150. {
  151. if (!$this->config['homepageSickrageEnabled']) {
  152. $this->setAPIResponse('error', 'SickRage homepage item is not enabled', 409);
  153. return false;
  154. }
  155. if (!$this->qualifyRequest($this->config['homepageSickrageAuth'])) {
  156. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  157. return false;
  158. }
  159. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  160. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  161. return false;
  162. }
  163. if (empty($this->config['sickrageURL'])) {
  164. $this->setAPIResponse('error', 'SickRage URL is not defined', 422);
  165. return false;
  166. }
  167. if (empty($this->config['sickrageToken'])) {
  168. $this->setAPIResponse('error', 'SickRage Token is not defined', 422);
  169. return false;
  170. }
  171. $calendarItems = array();
  172. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  173. foreach ($list as $key => $value) {
  174. try {
  175. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  176. $sickrageFuture = $this->formatSickrageCalendarWanted($downloader->future(), $key);
  177. $sickrageHistory = $this->formatSickrageCalendarHistory($downloader->history("100", "downloaded"), $key);
  178. if (!empty($sickrageFuture)) {
  179. $calendarItems = array_merge($calendarItems, $sickrageFuture);
  180. }
  181. if (!empty($sickrageHistory)) {
  182. $calendarItems = array_merge($calendarItems, $sickrageHistory);
  183. }
  184. } catch (Exception $e) {
  185. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  186. }
  187. }
  188. $this->setAPIResponse('success', null, 200, $calendarItems);
  189. return $calendarItems;
  190. }
  191. public function formatSickrageCalendarWanted($array, $number)
  192. {
  193. $array = json_decode($array, true);
  194. $gotCalendar = array();
  195. $i = 0;
  196. foreach ($array['data']['missed'] as $child) {
  197. $i++;
  198. $seriesName = $child['show_name'];
  199. $seriesID = $child['tvdbid'];
  200. $episodeID = $child['tvdbid'];
  201. $episodeAirDate = $child['airdate'];
  202. $episodeAirDateTime = explode(" ", $child['airs']);
  203. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  204. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  205. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  206. if (new DateTime() < new DateTime($episodeAirDate)) {
  207. $unaired = true;
  208. }
  209. $downloaded = "0";
  210. if ($downloaded == "0" && isset($unaired)) {
  211. $downloaded = "text-info";
  212. } elseif ($downloaded == "1") {
  213. $downloaded = "text-success";
  214. } else {
  215. $downloaded = "text-danger";
  216. }
  217. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  218. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  219. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  220. $fanart = "/plugins/images/cache/no-np.png";
  221. if (file_exists($cacheFile)) {
  222. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  223. unset($cacheFile);
  224. }
  225. $details = array(
  226. "seasonCount" => "",
  227. "status" => $child['show_status'],
  228. "topTitle" => $seriesName,
  229. "bottomTitle" => $bottomTitle,
  230. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  231. "runtime" => "",
  232. "image" => $fanart,
  233. "ratings" => "",
  234. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  235. "audioChannels" => "",
  236. "audioCodec" => "",
  237. "videoCodec" => "",
  238. "size" => "",
  239. "genres" => "",
  240. );
  241. array_push($gotCalendar, array(
  242. "id" => "Sick-" . $number . "-Miss-" . $i,
  243. "title" => $seriesName,
  244. "start" => $episodeAirDate,
  245. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  246. "imagetype" => "tv " . $downloaded,
  247. "imagetypeFilter" => "tv",
  248. "downloadFilter" => $downloaded,
  249. "bgColor" => str_replace('text', 'bg', $downloaded),
  250. "details" => $details,
  251. ));
  252. }
  253. foreach ($array['data']['today'] as $child) {
  254. $i++;
  255. $seriesName = $child['show_name'];
  256. $seriesID = $child['tvdbid'];
  257. $episodeID = $child['tvdbid'];
  258. $episodeAirDate = $child['airdate'];
  259. $episodeAirDateTime = explode(" ", $child['airs']);
  260. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  261. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  262. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  263. if (new DateTime() < new DateTime($episodeAirDate)) {
  264. $unaired = true;
  265. }
  266. $downloaded = "0";
  267. if ($downloaded == "0" && isset($unaired)) {
  268. $downloaded = "text-info";
  269. } elseif ($downloaded == "1") {
  270. $downloaded = "text-success";
  271. } else {
  272. $downloaded = "text-danger";
  273. }
  274. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  275. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  276. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  277. $fanart = "/plugins/images/cache/no-np.png";
  278. if (file_exists($cacheFile)) {
  279. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  280. unset($cacheFile);
  281. }
  282. $details = array(
  283. "seasonCount" => "",
  284. "status" => $child['show_status'],
  285. "topTitle" => $seriesName,
  286. "bottomTitle" => $bottomTitle,
  287. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  288. "runtime" => "",
  289. "image" => $fanart,
  290. "ratings" => "",
  291. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  292. "audioChannels" => "",
  293. "audioCodec" => "",
  294. "videoCodec" => "",
  295. "size" => "",
  296. "genres" => "",
  297. );
  298. array_push($gotCalendar, array(
  299. "id" => "Sick-" . $number . "-Today-" . $i,
  300. "title" => $seriesName,
  301. "start" => $episodeAirDate,
  302. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  303. "imagetype" => "tv " . $downloaded,
  304. "imagetypeFilter" => "tv",
  305. "downloadFilter" => $downloaded,
  306. "bgColor" => str_replace('text', 'bg', $downloaded),
  307. "details" => $details,
  308. ));
  309. }
  310. foreach ($array['data']['soon'] as $child) {
  311. $i++;
  312. $seriesName = $child['show_name'];
  313. $seriesID = $child['tvdbid'];
  314. $episodeID = $child['tvdbid'];
  315. $episodeAirDate = $child['airdate'];
  316. $episodeAirDateTime = explode(" ", $child['airs']);
  317. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  318. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  319. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  320. if (new DateTime() < new DateTime($episodeAirDate)) {
  321. $unaired = true;
  322. }
  323. $downloaded = "0";
  324. if ($downloaded == "0" && isset($unaired)) {
  325. $downloaded = "text-info";
  326. } elseif ($downloaded == "1") {
  327. $downloaded = "text-success";
  328. } else {
  329. $downloaded = "text-danger";
  330. }
  331. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  332. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  333. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  334. $fanart = "/plugins/images/cache/no-np.png";
  335. if (file_exists($cacheFile)) {
  336. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  337. unset($cacheFile);
  338. }
  339. $details = array(
  340. "seasonCount" => "",
  341. "status" => $child['show_status'],
  342. "topTitle" => $seriesName,
  343. "bottomTitle" => $bottomTitle,
  344. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  345. "runtime" => "",
  346. "image" => $fanart,
  347. "ratings" => "",
  348. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  349. "audioChannels" => "",
  350. "audioCodec" => "",
  351. "videoCodec" => "",
  352. "size" => "",
  353. "genres" => "",
  354. );
  355. array_push($gotCalendar, array(
  356. "id" => "Sick-" . $number . "-Soon-" . $i,
  357. "title" => $seriesName,
  358. "start" => $episodeAirDate,
  359. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  360. "imagetype" => "tv " . $downloaded,
  361. "imagetypeFilter" => "tv",
  362. "downloadFilter" => $downloaded,
  363. "bgColor" => str_replace('text', 'bg', $downloaded),
  364. "details" => $details,
  365. ));
  366. }
  367. foreach ($array['data']['later'] as $child) {
  368. $i++;
  369. $seriesName = $child['show_name'];
  370. $seriesID = $child['tvdbid'];
  371. $episodeID = $child['tvdbid'];
  372. $episodeAirDate = $child['airdate'];
  373. $episodeAirDateTime = explode(" ", $child['airs']);
  374. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  375. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  376. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  377. if (new DateTime() < new DateTime($episodeAirDate)) {
  378. $unaired = true;
  379. }
  380. $downloaded = "0";
  381. if ($downloaded == "0" && isset($unaired)) {
  382. $downloaded = "text-info";
  383. } elseif ($downloaded == "1") {
  384. $downloaded = "text-success";
  385. } else {
  386. $downloaded = "text-danger";
  387. }
  388. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  389. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  390. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  391. $fanart = "/plugins/images/cache/no-np.png";
  392. if (file_exists($cacheFile)) {
  393. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  394. unset($cacheFile);
  395. }
  396. $details = array(
  397. "seasonCount" => "",
  398. "status" => $child['show_status'],
  399. "topTitle" => $seriesName,
  400. "bottomTitle" => $bottomTitle,
  401. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  402. "runtime" => "",
  403. "image" => $fanart,
  404. "ratings" => "",
  405. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  406. "audioChannels" => "",
  407. "audioCodec" => "",
  408. "videoCodec" => "",
  409. "size" => "",
  410. "genres" => "",
  411. );
  412. array_push($gotCalendar, array(
  413. "id" => "Sick-" . $number . "-Later-" . $i,
  414. "title" => $seriesName,
  415. "start" => $episodeAirDate,
  416. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  417. "imagetype" => "tv " . $downloaded,
  418. "imagetypeFilter" => "tv",
  419. "downloadFilter" => $downloaded,
  420. "bgColor" => str_replace('text', 'bg', $downloaded),
  421. "details" => $details,
  422. ));
  423. }
  424. if ($i != 0) {
  425. return $gotCalendar;
  426. }
  427. return false;
  428. }
  429. public function formatSickrageCalendarHistory($array, $number)
  430. {
  431. $array = json_decode($array, true);
  432. $gotCalendar = array();
  433. $i = 0;
  434. foreach ($array['data'] as $child) {
  435. $i++;
  436. $seriesName = $child['show_name'];
  437. $seriesID = $child['tvdbid'];
  438. $episodeID = $child['tvdbid'];
  439. $episodeAirDate = $child['date'];
  440. $downloaded = "text-success";
  441. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']);
  442. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  443. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  444. $fanart = "/plugins/images/cache/no-np.png";
  445. if (file_exists($cacheFile)) {
  446. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  447. unset($cacheFile);
  448. }
  449. $details = array(
  450. "seasonCount" => "",
  451. "status" => $child['status'],
  452. "topTitle" => $seriesName,
  453. "bottomTitle" => $bottomTitle,
  454. "overview" => '',
  455. "runtime" => isset($child['series']['runtime']) ? $child['series']['runtime'] : 30,
  456. "image" => $fanart,
  457. "ratings" => isset($child['series']['ratings']['value']) ? $child['series']['ratings']['value'] : "unknown",
  458. "videoQuality" => isset($child["quality"]) ? $child['quality'] : "unknown",
  459. "audioChannels" => "",
  460. "audioCodec" => "",
  461. "videoCodec" => "",
  462. "size" => "",
  463. "genres" => "",
  464. );
  465. array_push($gotCalendar, array(
  466. "id" => "Sick-" . $number . "-History-" . $i,
  467. "title" => $seriesName,
  468. "start" => $episodeAirDate,
  469. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  470. "imagetype" => "tv " . $downloaded,
  471. "imagetypeFilter" => "tv",
  472. "downloadFilter" => $downloaded,
  473. "bgColor" => str_replace('text', 'bg', $downloaded),
  474. "details" => $details,
  475. ));
  476. }
  477. if ($i != 0) {
  478. return $gotCalendar;
  479. }
  480. return false;
  481. }
  482. }