sickrage.php 17 KB

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