radarr.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. trait RadarrHomepageItem
  3. {
  4. public function radarrSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Radarr',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/radarr.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', 'homepageRadarrEnabled'),
  21. $this->settingsOption('auth', 'homepageRadarrAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('multiple-url', 'radarrURL'),
  25. $this->settingsOption('multiple-token', 'radarrToken'),
  26. $this->settingsOption('disable-cert-check', 'radarrDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'radarrUseCustomCertificate'),
  28. ],
  29. 'API SOCKS' => [
  30. $this->settingsOption('socks', 'radarr'),
  31. $this->settingsOption('blank'),
  32. $this->settingsOption('enable', 'radarrSocksEnabled'),
  33. $this->settingsOption('auth', 'radarrSocksAuth'),
  34. ],
  35. 'Queue' => [
  36. $this->settingsOption('enable', 'homepageRadarrQueueEnabled'),
  37. $this->settingsOption('auth', 'homepageRadarrQueueAuth'),
  38. $this->settingsOption('combine', 'homepageRadarrQueueCombine'),
  39. $this->settingsOption('refresh', 'homepageRadarrQueueRefresh'),
  40. ],
  41. 'Calendar' => [
  42. $this->settingsOption('calendar-start', 'calendarStart'),
  43. $this->settingsOption('calendar-end', 'calendarEnd'),
  44. $this->settingsOption('calendar-starting-day', 'calendarFirstDay'),
  45. $this->settingsOption('calendar-default-view', 'calendarDefault'),
  46. $this->settingsOption('calendar-time-format', 'calendarTimeFormat'),
  47. $this->settingsOption('calendar-locale', 'calendarLocale'),
  48. $this->settingsOption('calendar-limit', 'calendarLimit'),
  49. $this->settingsOption('refresh', 'calendarRefresh'),
  50. $this->settingsOption('calendar-link-url', 'radarrCalendarLink'),
  51. $this->settingsOption('blank', null),
  52. $this->settingsOption('switch', 'radarrUnmonitored', ['label' => 'Show Unmonitored']),
  53. $this->settingsOption('switch', 'radarrPhysicalRelease', ['label' => 'Show Physical Releases']),
  54. $this->settingsOption('switch', 'radarrDigitalRelease', ['label' => 'Show Digital Releases']),
  55. $this->settingsOption('switch', 'radarrCinemaRelease', ['label' => 'Show Cinema Releases']),
  56. ],
  57. 'Test Connection' => [
  58. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  59. $this->settingsOption('test', 'radarr'),
  60. ]
  61. ]
  62. ];
  63. return array_merge($homepageInformation, $homepageSettings);
  64. }
  65. public function testConnectionRadarr()
  66. {
  67. if (empty($this->config['radarrURL'])) {
  68. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  69. return false;
  70. }
  71. if (empty($this->config['radarrToken'])) {
  72. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  73. return false;
  74. }
  75. $failed = false;
  76. $errors = '';
  77. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  78. foreach ($list as $key => $value) {
  79. try {
  80. $options = $this->requestOptions($value['url'], null, $this->config['radarrDisableCertCheck'], $this->config['radarrUseCustomCertificate']);
  81. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr', null, null, $options);
  82. $results = $downloader->getRootFolder();
  83. $downloadList = json_decode($results, true);
  84. if (is_array($downloadList) || is_object($downloadList)) {
  85. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  86. if (!is_array($queue)) {
  87. $ip = $value['url'];
  88. $errors .= $ip . ': ' . $queue;
  89. $failed = true;
  90. }
  91. } else {
  92. $ip = $value['url'];
  93. $errors .= $ip . ': Response was not JSON';
  94. $failed = true;
  95. }
  96. } catch (Exception $e) {
  97. $failed = true;
  98. $ip = $value['url'];
  99. $errors .= $ip . ': ' . $e->getMessage();
  100. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  101. }
  102. }
  103. if ($failed) {
  104. $this->setAPIResponse('error', $errors, 500);
  105. return false;
  106. } else {
  107. $this->setAPIResponse('success', null, 200);
  108. return true;
  109. }
  110. }
  111. public function radarrHomepagePermissions($key = null)
  112. {
  113. $permissions = [
  114. 'calendar' => [
  115. 'enabled' => [
  116. 'homepageRadarrEnabled'
  117. ],
  118. 'auth' => [
  119. 'homepageRadarrAuth'
  120. ],
  121. 'not_empty' => [
  122. 'radarrURL',
  123. 'radarrToken'
  124. ]
  125. ],
  126. 'queue' => [
  127. 'enabled' => [
  128. 'homepageRadarrEnabled',
  129. 'homepageRadarrQueueEnabled'
  130. ],
  131. 'auth' => [
  132. 'homepageRadarrAuth',
  133. 'homepageRadarrQueueAuth'
  134. ],
  135. 'not_empty' => [
  136. 'radarrURL',
  137. 'radarrToken'
  138. ]
  139. ]
  140. ];
  141. return $this->homepageCheckKeyPermissions($key, $permissions);
  142. }
  143. public function homepageOrderRadarrQueue()
  144. {
  145. if ($this->homepageItemPermissions($this->radarrHomepagePermissions('queue'))) {
  146. $loadingBox = ($this->config['homepageRadarrQueueCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  147. $builder = ($this->config['homepageRadarrQueueCombine']) ? 'buildDownloaderCombined(\'radarr\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("radarr"));';
  148. return '
  149. <div id="' . __FUNCTION__ . '">
  150. ' . $loadingBox . '
  151. <script>
  152. // homepageOrderRadarrQueue
  153. ' . $builder . '
  154. homepageDownloader("radarr", "' . $this->config['homepageRadarrQueueRefresh'] . '");
  155. // End homepageOrderRadarrQueue
  156. </script>
  157. </div>
  158. ';
  159. }
  160. }
  161. public function getRadarrQueue()
  162. {
  163. if (!$this->homepageItemPermissions($this->radarrHomepagePermissions('queue'), true)) {
  164. return false;
  165. }
  166. $queueItems = array();
  167. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  168. foreach ($list as $key => $value) {
  169. try {
  170. $options = $this->requestOptions($value['url'], $this->config['homepageRadarrQueueRefresh'], $this->config['radarrDisableCertCheck'], $this->config['radarrUseCustomCertificate']);
  171. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr', null, null, $options);
  172. $results = $downloader->getQueue();
  173. $downloadList = json_decode($results, true);
  174. if (is_array($downloadList) || is_object($downloadList)) {
  175. $queue = (array_key_exists('error', $downloadList)) ? [] : $downloadList;
  176. $queue = $queue['records'] ?? $queue;
  177. } else {
  178. $queue = [];
  179. }
  180. if (!empty($queue)) {
  181. $queueItems = array_merge($queueItems, $queue);
  182. }
  183. } catch (Exception $e) {
  184. $this->logger->error($e);
  185. }
  186. }
  187. $api['content']['queueItems'] = $queueItems;
  188. $api['content']['historyItems'] = false;
  189. $api['content'] = $api['content'] ?? false;
  190. $this->setAPIResponse('success', null, 200, $api);
  191. return $api;
  192. }
  193. public function getRadarrCalendar($startDate = null, $endDate = null)
  194. {
  195. $startDate = ($startDate) ?? $_GET['start'] ?? date('Y-m-d', strtotime('-' . $this->config['calendarStart'] . ' days'));
  196. $endDate = ($endDate) ?? $_GET['end'] ?? date('Y-m-d', strtotime('+' . $this->config['calendarEnd'] . ' days'));
  197. if (!$this->homepageItemPermissions($this->radarrHomepagePermissions('calendar'), true)) {
  198. return false;
  199. }
  200. if ($this->demo) {
  201. return $this->demoData('radarr/calendar.json');
  202. }
  203. $calendarItems = array();
  204. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  205. foreach ($list as $key => $value) {
  206. try {
  207. $options = $this->requestOptions($value['url'], $this->config['homepageRadarrQueueRefresh'], $this->config['radarrDisableCertCheck'], $this->config['radarrUseCustomCertificate']);
  208. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr', null, null, $options);
  209. $results = $downloader->getCalendar($startDate, $endDate, $this->config['radarrUnmonitored']);
  210. $result = json_decode($results, true);
  211. if (is_array($result) || is_object($result)) {
  212. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatRadarrCalendar($results, $key, $value['url']);
  213. } else {
  214. $calendar = '';
  215. }
  216. } catch (Exception $e) {
  217. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  218. }
  219. if (!empty($calendar)) {
  220. $calendarItems = array_merge($calendarItems, $calendar);
  221. }
  222. }
  223. $this->setAPIResponse('success', null, 200, $calendarItems);
  224. return $calendarItems;
  225. }
  226. public function formatRadarrCalendar($array, $number, $url)
  227. {
  228. $url = rtrim($url, '/'); //remove trailing slash
  229. $url = $url . '/api';
  230. $array = json_decode($array, true);
  231. $gotCalendar = array();
  232. $i = 0;
  233. foreach ($array as $child) {
  234. for ($j = 0; $j < 3; $j++) {
  235. $type = [];
  236. if ($j == 0 && $this->config['radarrPhysicalRelease'] && isset($child['physicalRelease'])) {
  237. $releaseDate = $child['physicalRelease'];
  238. array_push($type, "physical");
  239. if (isset($child['digitalRelease']) && $child['physicalRelease'] == $child['digitalRelease']) {
  240. array_push($type, "digital");
  241. $j++;
  242. }
  243. if (isset($child['inCinemas']) && $child['physicalRelease'] == $child['inCinemas']) {
  244. array_push($type, "cinema");
  245. $j += 2;
  246. }
  247. } elseif ($j == 1 && $this->config['radarrDigitalRelease'] && isset($child['digitalRelease'])) {
  248. $releaseDate = $child['digitalRelease'];
  249. array_push($type, "digital");
  250. if (isset($child['inCinemas']) && $child['digitalRelease'] == $child['inCinemas']) {
  251. array_push($type, "cinema");
  252. $j++;
  253. }
  254. } elseif ($j == 2 && $this->config['radarrCinemaRelease'] && isset($child['inCinemas'])) {
  255. $releaseDate = $child['inCinemas'];
  256. array_push($type, "cinema");
  257. } else {
  258. continue;
  259. }
  260. $i++;
  261. $movieName = $child['title'];
  262. $movieID = $child['tmdbId'];
  263. if (!isset($movieID)) {
  264. $movieID = "";
  265. }
  266. $releaseDate = strtotime($releaseDate);
  267. $releaseDate = date("Y-m-d", $releaseDate);
  268. if (new DateTime() < new DateTime($releaseDate)) {
  269. $notReleased = "true";
  270. } else {
  271. $notReleased = "false";
  272. }
  273. $downloaded = $child['hasFile'];
  274. if ($downloaded == "0" && $notReleased == "true") {
  275. $downloaded = "text-info";
  276. } elseif ($downloaded == "1") {
  277. $downloaded = "text-success";
  278. } else {
  279. $downloaded = "text-danger";
  280. }
  281. $banner = "/plugins/images/homepage/no-np.png";
  282. foreach ($child['images'] as $image) {
  283. if ($image['coverType'] == "banner" || $image['coverType'] == "fanart") {
  284. if (strpos($image['url'], '://') === false) {
  285. $imageUrl = $image['url'];
  286. $urlParts = explode("/", $url);
  287. $imageParts = explode("/", $image['url']);
  288. if ($imageParts[1] == end($urlParts)) {
  289. unset($imageParts[1]);
  290. $imageUrl = implode("/", $imageParts);
  291. }
  292. $banner = $url . $imageUrl . '?apikey=' . $this->config['radarrToken'];
  293. } else {
  294. $banner = $image['url'];
  295. }
  296. }
  297. }
  298. if ($banner !== "/plugins/images/homepage/no-np.png" || (strpos($banner, 'apikey') !== false)) {
  299. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  300. $imageURL = $banner;
  301. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  302. $banner = 'data/cache/' . $movieID . '.jpg';
  303. if (!file_exists($cacheFile)) {
  304. $this->cacheImage($imageURL, $movieID);
  305. unset($imageURL);
  306. unset($cacheFile);
  307. }
  308. }
  309. $alternativeTitles = "";
  310. if (!empty($child['alternativeTitles'])) {
  311. foreach ($child['alternativeTitles'] as $alternative) {
  312. $alternativeTitles .= $alternative['title'] . ', ';
  313. }
  314. } elseif (!empty($child['alternateTitles'])) { //v3 API
  315. foreach ($child['alternateTitles'] as $alternative) {
  316. $alternativeTitles .= $alternative['title'] . ', ';
  317. }
  318. }
  319. $alternativeTitles = empty($alternativeTitles) ? "" : substr($alternativeTitles, 0, -2);
  320. $href = $this->config['radarrCalendarLink'] ?? '';
  321. if (empty($href) && !empty($this->config['radarrURL'])) {
  322. $href_arr = explode(',', $this->config['radarrURL']);
  323. $href = reset($href_arr);
  324. }
  325. if (!empty($href)) {
  326. $href = $href . '/movie/' . $movieID;
  327. $href = str_replace("//movie/", "/movie/", $href);
  328. }
  329. $details = array(
  330. "topTitle" => $movieName,
  331. "bottomTitle" => $alternativeTitles,
  332. "status" => $child['status'],
  333. "overview" => $child['overview'],
  334. "runtime" => $child['runtime'],
  335. "image" => $banner,
  336. "ratings" => $child['ratings']['value'] ?? 0,
  337. "videoQuality" => $child["hasFile"] ? @$child['movieFile']['quality']['quality']['name'] : "unknown",
  338. "audioChannels" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioChannels'] : "unknown",
  339. "audioCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioFormat'] : "unknown",
  340. "videoCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['videoCodec'] : "unknown",
  341. "size" => $child["hasFile"] ? @$child['movieFile']['size'] : "unknown",
  342. "genres" => $child['genres'],
  343. "year" => $child['year'] ?? '',
  344. "studio" => $child['studio'] ?? '',
  345. "href" => strtolower($href),
  346. "icon" => "/plugins/images/tabs/radarr.png",
  347. );
  348. array_push($gotCalendar, array(
  349. "id" => "Radarr-" . $number . "-" . $i,
  350. "title" => $movieName,
  351. "start" => $releaseDate,
  352. "className" => "inline-popups bg-calendar movieID--" . $movieID,
  353. "imagetype" => "film " . $downloaded,
  354. "imagetypeFilter" => "film",
  355. "downloadFilter" => $downloaded,
  356. "releaseType" => $type,
  357. "bgColor" => str_replace('text', 'bg', $downloaded),
  358. "details" => $details
  359. ));
  360. }
  361. }
  362. if ($i != 0) {
  363. return $gotCalendar;
  364. }
  365. return false;
  366. }
  367. }