radarr.php 13 KB

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