radarr.php 14 KB

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