sickrage.php 17 KB

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