sickrage.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  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. if (array_key_exists($key, $permissions)) {
  108. return $permissions[$key];
  109. } elseif ($key == 'all') {
  110. return $permissions;
  111. } else {
  112. return [];
  113. }
  114. }
  115. public function getSickRageCalendar($startDate = null, $endDate = null)
  116. {
  117. if (!$this->homepageItemPermissions($this->sickrageHomepagePermissions('calendar'), true)) {
  118. return false;
  119. }
  120. $calendarItems = array();
  121. $list = $this->csvHomepageUrlToken($this->config['sickrageURL'], $this->config['sickrageToken']);
  122. foreach ($list as $key => $value) {
  123. try {
  124. $options = $this->requestOptions($value['url'], null, $this->config['sickrageDisableCertCheck'], $this->config['sickrageUseCustomCertificate']);
  125. $downloader = new Kryptonit3\SickRage\SickRage($value['url'], $value['token'], null, null, $options);
  126. $sickrageFuture = $this->formatSickrageCalendarWanted($downloader->future(), $key);
  127. $sickrageHistory = $this->formatSickrageCalendarHistory($downloader->history("100", "downloaded"), $key);
  128. if (!empty($sickrageFuture)) {
  129. $calendarItems = array_merge($calendarItems, $sickrageFuture);
  130. }
  131. if (!empty($sickrageHistory)) {
  132. $calendarItems = array_merge($calendarItems, $sickrageHistory);
  133. }
  134. } catch (Exception $e) {
  135. $this->writeLog('error', 'SickRage Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  136. }
  137. }
  138. $this->setAPIResponse('success', null, 200, $calendarItems);
  139. return $calendarItems;
  140. }
  141. public function formatSickrageCalendarWanted($array, $number)
  142. {
  143. $array = json_decode($array, true);
  144. $gotCalendar = array();
  145. $i = 0;
  146. foreach ($array['data']['missed'] as $child) {
  147. $i++;
  148. $seriesName = $child['show_name'];
  149. $seriesID = $child['tvdbid'];
  150. $episodeID = $child['tvdbid'];
  151. $episodeAirDate = $child['airdate'];
  152. $episodeAirDateTime = explode(" ", $child['airs']);
  153. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1] . $episodeAirDateTime[2]));
  154. $episodeAirDate = strtotime($episodeAirDate . $episodeAirDateTime);
  155. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  156. if (new DateTime() < new DateTime($episodeAirDate)) {
  157. $unaired = true;
  158. }
  159. $downloaded = "0";
  160. if ($downloaded == "0" && isset($unaired)) {
  161. $downloaded = "text-info";
  162. } elseif ($downloaded == "1") {
  163. $downloaded = "text-success";
  164. } else {
  165. $downloaded = "text-danger";
  166. }
  167. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']) . ' - ' . $child['ep_name'];
  168. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  169. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  170. $fanart = "/plugins/images/cache/no-np.png";
  171. if (file_exists($cacheFile)) {
  172. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  173. unset($cacheFile);
  174. }
  175. $details = array(
  176. "seasonCount" => "",
  177. "status" => $child['show_status'],
  178. "topTitle" => $seriesName,
  179. "bottomTitle" => $bottomTitle,
  180. "overview" => isset($child['ep_plot']) ? $child['ep_plot'] : '',
  181. "runtime" => "",
  182. "image" => $fanart,
  183. "ratings" => "",
  184. "videoQuality" => isset($child["quality"]) ? $child["quality"] : "",
  185. "audioChannels" => "",
  186. "audioCodec" => "",
  187. "videoCodec" => "",
  188. "size" => "",
  189. "genres" => "",
  190. );
  191. array_push($gotCalendar, array(
  192. "id" => "Sick-" . $number . "-Miss-" . $i,
  193. "title" => $seriesName,
  194. "start" => $episodeAirDate,
  195. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  196. "imagetype" => "tv " . $downloaded,
  197. "imagetypeFilter" => "tv",
  198. "downloadFilter" => $downloaded,
  199. "bgColor" => str_replace('text', 'bg', $downloaded),
  200. "details" => $details,
  201. ));
  202. }
  203. foreach ($array['data']['today'] 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 . "-Today-" . $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']['soon'] 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 . "-Soon-" . $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']['later'] 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 . "-Later-" . $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. if ($i != 0) {
  375. return $gotCalendar;
  376. }
  377. return false;
  378. }
  379. public function formatSickrageCalendarHistory($array, $number)
  380. {
  381. $array = json_decode($array, true);
  382. $gotCalendar = array();
  383. $i = 0;
  384. foreach ($array['data'] as $child) {
  385. $i++;
  386. $seriesName = $child['show_name'];
  387. $seriesID = $child['tvdbid'];
  388. $episodeID = $child['tvdbid'];
  389. $episodeAirDate = $child['date'];
  390. $downloaded = "text-success";
  391. $bottomTitle = 'S' . sprintf("%02d", $child['season']) . 'E' . sprintf("%02d", $child['episode']);
  392. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  393. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  394. $fanart = "/plugins/images/cache/no-np.png";
  395. if (file_exists($cacheFile)) {
  396. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  397. unset($cacheFile);
  398. }
  399. $details = array(
  400. "seasonCount" => "",
  401. "status" => $child['status'],
  402. "topTitle" => $seriesName,
  403. "bottomTitle" => $bottomTitle,
  404. "overview" => '',
  405. "runtime" => isset($child['series']['runtime']) ? $child['series']['runtime'] : 30,
  406. "image" => $fanart,
  407. "ratings" => isset($child['series']['ratings']['value']) ? $child['series']['ratings']['value'] : "unknown",
  408. "videoQuality" => isset($child["quality"]) ? $child['quality'] : "unknown",
  409. "audioChannels" => "",
  410. "audioCodec" => "",
  411. "videoCodec" => "",
  412. "size" => "",
  413. "genres" => "",
  414. );
  415. array_push($gotCalendar, array(
  416. "id" => "Sick-" . $number . "-History-" . $i,
  417. "title" => $seriesName,
  418. "start" => $episodeAirDate,
  419. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  420. "imagetype" => "tv " . $downloaded,
  421. "imagetypeFilter" => "tv",
  422. "downloadFilter" => $downloaded,
  423. "bgColor" => str_replace('text', 'bg', $downloaded),
  424. "details" => $details,
  425. ));
  426. }
  427. if ($i != 0) {
  428. return $gotCalendar;
  429. }
  430. return false;
  431. }
  432. }