sickrage.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 sickrageHomepagePermissions($key = null)
  150. {
  151. $permissions = [
  152. 'calendar' => [
  153. 'enabled' => [
  154. 'homepageSickrageEnabled'
  155. ],
  156. 'auth' => [
  157. 'homepageSickrageAuth'
  158. ],
  159. 'not_empty' => [
  160. 'sickrageURL',
  161. 'sickrageToken'
  162. ]
  163. ]
  164. ];
  165. if (array_key_exists($key, $permissions)) {
  166. return $permissions[$key];
  167. } elseif ($key == 'all') {
  168. return $permissions;
  169. } else {
  170. return [];
  171. }
  172. }
  173. public function getSickRageCalendar($startDate = null, $endDate = null)
  174. {
  175. if (!$this->homepageItemPermissions($this->sickrageHomepagePermissions('calendar'), true)) {
  176. return false;
  177. }
  178. $calendarItems = array();
  179. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  180. foreach ($list as $key => $value) {
  181. try {
  182. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  183. $sickrageFuture = $this->formatSickrageCalendarWanted($downloader->future(), $key);
  184. $sickrageHistory = $this->formatSickrageCalendarHistory($downloader->history("100", "downloaded"), $key);
  185. if (!empty($sickrageFuture)) {
  186. $calendarItems = array_merge($calendarItems, $sickrageFuture);
  187. }
  188. if (!empty($sickrageHistory)) {
  189. $calendarItems = array_merge($calendarItems, $sickrageHistory);
  190. }
  191. } catch (Exception $e) {
  192. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  193. }
  194. }
  195. $this->setAPIResponse('success', null, 200, $calendarItems);
  196. return $calendarItems;
  197. }
  198. public function formatSickrageCalendarWanted($array, $number)
  199. {
  200. $array = json_decode($array, true);
  201. $gotCalendar = array();
  202. $i = 0;
  203. foreach ($array['data']['missed'] as $child) {
  204. $i++;
  205. $seriesName = $child['show_name'];
  206. $seriesID = $child['tvdbid'];
  207. $episodeID = $child['tvdbid'];
  208. $episodeAirDate = $child['airdate'];
  209. $episodeAirDateTime = explode(" ", $child['airs']);
  210. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  211. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  212. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  213. if (new DateTime() < new DateTime($episodeAirDate)) {
  214. $unaired = true;
  215. }
  216. $downloaded = "0";
  217. if ($downloaded == "0" && isset($unaired)) {
  218. $downloaded = "text-info";
  219. } elseif ($downloaded == "1") {
  220. $downloaded = "text-success";
  221. } else {
  222. $downloaded = "text-danger";
  223. }
  224. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  225. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  226. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  227. $fanart = "/plugins/images/cache/no-np.png";
  228. if (file_exists($cacheFile)) {
  229. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  230. unset($cacheFile);
  231. }
  232. $details = array(
  233. "seasonCount" => "",
  234. "status" => $child['show_status'],
  235. "topTitle" => $seriesName,
  236. "bottomTitle" => $bottomTitle,
  237. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  238. "runtime" => "",
  239. "image" => $fanart,
  240. "ratings" => "",
  241. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  242. "audioChannels" => "",
  243. "audioCodec" => "",
  244. "videoCodec" => "",
  245. "size" => "",
  246. "genres" => "",
  247. );
  248. array_push($gotCalendar, array(
  249. "id" => "Sick-" . $number . "-Miss-" . $i,
  250. "title" => $seriesName,
  251. "start" => $episodeAirDate,
  252. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  253. "imagetype" => "tv " . $downloaded,
  254. "imagetypeFilter" => "tv",
  255. "downloadFilter" => $downloaded,
  256. "bgColor" => str_replace('text', 'bg', $downloaded),
  257. "details" => $details,
  258. ));
  259. }
  260. foreach ($array['data']['today'] as $child) {
  261. $i++;
  262. $seriesName = $child['show_name'];
  263. $seriesID = $child['tvdbid'];
  264. $episodeID = $child['tvdbid'];
  265. $episodeAirDate = $child['airdate'];
  266. $episodeAirDateTime = explode(" ", $child['airs']);
  267. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  268. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  269. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  270. if (new DateTime() < new DateTime($episodeAirDate)) {
  271. $unaired = true;
  272. }
  273. $downloaded = "0";
  274. if ($downloaded == "0" && isset($unaired)) {
  275. $downloaded = "text-info";
  276. } elseif ($downloaded == "1") {
  277. $downloaded = "text-success";
  278. } else {
  279. $downloaded = "text-danger";
  280. }
  281. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  282. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  283. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  284. $fanart = "/plugins/images/cache/no-np.png";
  285. if (file_exists($cacheFile)) {
  286. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  287. unset($cacheFile);
  288. }
  289. $details = array(
  290. "seasonCount" => "",
  291. "status" => $child['show_status'],
  292. "topTitle" => $seriesName,
  293. "bottomTitle" => $bottomTitle,
  294. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  295. "runtime" => "",
  296. "image" => $fanart,
  297. "ratings" => "",
  298. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  299. "audioChannels" => "",
  300. "audioCodec" => "",
  301. "videoCodec" => "",
  302. "size" => "",
  303. "genres" => "",
  304. );
  305. array_push($gotCalendar, array(
  306. "id" => "Sick-" . $number . "-Today-" . $i,
  307. "title" => $seriesName,
  308. "start" => $episodeAirDate,
  309. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  310. "imagetype" => "tv " . $downloaded,
  311. "imagetypeFilter" => "tv",
  312. "downloadFilter" => $downloaded,
  313. "bgColor" => str_replace('text', 'bg', $downloaded),
  314. "details" => $details,
  315. ));
  316. }
  317. foreach ($array['data']['soon'] as $child) {
  318. $i++;
  319. $seriesName = $child['show_name'];
  320. $seriesID = $child['tvdbid'];
  321. $episodeID = $child['tvdbid'];
  322. $episodeAirDate = $child['airdate'];
  323. $episodeAirDateTime = explode(" ", $child['airs']);
  324. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  325. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  326. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  327. if (new DateTime() < new DateTime($episodeAirDate)) {
  328. $unaired = true;
  329. }
  330. $downloaded = "0";
  331. if ($downloaded == "0" && isset($unaired)) {
  332. $downloaded = "text-info";
  333. } elseif ($downloaded == "1") {
  334. $downloaded = "text-success";
  335. } else {
  336. $downloaded = "text-danger";
  337. }
  338. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  339. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  340. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  341. $fanart = "/plugins/images/cache/no-np.png";
  342. if (file_exists($cacheFile)) {
  343. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  344. unset($cacheFile);
  345. }
  346. $details = array(
  347. "seasonCount" => "",
  348. "status" => $child['show_status'],
  349. "topTitle" => $seriesName,
  350. "bottomTitle" => $bottomTitle,
  351. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  352. "runtime" => "",
  353. "image" => $fanart,
  354. "ratings" => "",
  355. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  356. "audioChannels" => "",
  357. "audioCodec" => "",
  358. "videoCodec" => "",
  359. "size" => "",
  360. "genres" => "",
  361. );
  362. array_push($gotCalendar, array(
  363. "id" => "Sick-" . $number . "-Soon-" . $i,
  364. "title" => $seriesName,
  365. "start" => $episodeAirDate,
  366. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  367. "imagetype" => "tv " . $downloaded,
  368. "imagetypeFilter" => "tv",
  369. "downloadFilter" => $downloaded,
  370. "bgColor" => str_replace('text', 'bg', $downloaded),
  371. "details" => $details,
  372. ));
  373. }
  374. foreach ($array['data']['later'] as $child) {
  375. $i++;
  376. $seriesName = $child['show_name'];
  377. $seriesID = $child['tvdbid'];
  378. $episodeID = $child['tvdbid'];
  379. $episodeAirDate = $child['airdate'];
  380. $episodeAirDateTime = explode(" ", $child['airs']);
  381. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  382. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  383. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  384. if (new DateTime() < new DateTime($episodeAirDate)) {
  385. $unaired = true;
  386. }
  387. $downloaded = "0";
  388. if ($downloaded == "0" && isset($unaired)) {
  389. $downloaded = "text-info";
  390. } elseif ($downloaded == "1") {
  391. $downloaded = "text-success";
  392. } else {
  393. $downloaded = "text-danger";
  394. }
  395. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  396. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  397. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  398. $fanart = "/plugins/images/cache/no-np.png";
  399. if (file_exists($cacheFile)) {
  400. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  401. unset($cacheFile);
  402. }
  403. $details = array(
  404. "seasonCount" => "",
  405. "status" => $child['show_status'],
  406. "topTitle" => $seriesName,
  407. "bottomTitle" => $bottomTitle,
  408. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  409. "runtime" => "",
  410. "image" => $fanart,
  411. "ratings" => "",
  412. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  413. "audioChannels" => "",
  414. "audioCodec" => "",
  415. "videoCodec" => "",
  416. "size" => "",
  417. "genres" => "",
  418. );
  419. array_push($gotCalendar, array(
  420. "id" => "Sick-" . $number . "-Later-" . $i,
  421. "title" => $seriesName,
  422. "start" => $episodeAirDate,
  423. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  424. "imagetype" => "tv " . $downloaded,
  425. "imagetypeFilter" => "tv",
  426. "downloadFilter" => $downloaded,
  427. "bgColor" => str_replace('text', 'bg', $downloaded),
  428. "details" => $details,
  429. ));
  430. }
  431. if ($i != 0) {
  432. return $gotCalendar;
  433. }
  434. return false;
  435. }
  436. public function formatSickrageCalendarHistory($array, $number)
  437. {
  438. $array = json_decode($array, true);
  439. $gotCalendar = array();
  440. $i = 0;
  441. foreach ($array['data'] as $child) {
  442. $i++;
  443. $seriesName = $child['show_name'];
  444. $seriesID = $child['tvdbid'];
  445. $episodeID = $child['tvdbid'];
  446. $episodeAirDate = $child['date'];
  447. $downloaded = "text-success";
  448. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']);
  449. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  450. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  451. $fanart = "/plugins/images/cache/no-np.png";
  452. if (file_exists($cacheFile)) {
  453. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  454. unset($cacheFile);
  455. }
  456. $details = array(
  457. "seasonCount" => "",
  458. "status" => $child['status'],
  459. "topTitle" => $seriesName,
  460. "bottomTitle" => $bottomTitle,
  461. "overview" => '',
  462. "runtime" => isset($child['series']['runtime']) ? $child['series']['runtime'] : 30,
  463. "image" => $fanart,
  464. "ratings" => isset($child['series']['ratings']['value']) ? $child['series']['ratings']['value'] : "unknown",
  465. "videoQuality" => isset($child["quality"]) ? $child['quality'] : "unknown",
  466. "audioChannels" => "",
  467. "audioCodec" => "",
  468. "videoCodec" => "",
  469. "size" => "",
  470. "genres" => "",
  471. );
  472. array_push($gotCalendar, array(
  473. "id" => "Sick-" . $number . "-History-" . $i,
  474. "title" => $seriesName,
  475. "start" => $episodeAirDate,
  476. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  477. "imagetype" => "tv " . $downloaded,
  478. "imagetypeFilter" => "tv",
  479. "downloadFilter" => $downloaded,
  480. "bgColor" => str_replace('text', 'bg', $downloaded),
  481. "details" => $details,
  482. ));
  483. }
  484. if ($i != 0) {
  485. return $gotCalendar;
  486. }
  487. return false;
  488. }
  489. }