sickrage.php 15 KB

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