radarr.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 = array(
  17. 'settings' => array(
  18. 'Enable' => array(
  19. array(
  20. 'type' => 'switch',
  21. 'name' => 'homepageRadarrEnabled',
  22. 'label' => 'Enable',
  23. 'value' => $this->config['homepageRadarrEnabled']
  24. ),
  25. array(
  26. 'type' => 'select',
  27. 'name' => 'homepageRadarrAuth',
  28. 'label' => 'Minimum Authentication',
  29. 'value' => $this->config['homepageRadarrAuth'],
  30. 'options' => $this->groupOptions
  31. )
  32. ),
  33. 'Connection' => array(
  34. array(
  35. 'type' => 'input',
  36. 'name' => 'radarrURL',
  37. 'label' => 'URL',
  38. 'value' => $this->config['radarrURL'],
  39. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  40. 'placeholder' => 'http(s)://hostname:port'
  41. ),
  42. array(
  43. 'type' => 'password-alt',
  44. 'name' => 'radarrToken',
  45. 'label' => 'Token',
  46. 'value' => $this->config['radarrToken']
  47. )
  48. ),
  49. 'API SOCKS' => array(
  50. array(
  51. 'type' => 'html',
  52. 'override' => 12,
  53. 'label' => '',
  54. 'html' => '
  55. <div class="panel panel-default">
  56. <div class="panel-wrapper collapse in">
  57. <div class="panel-body">' . $this->socksHeadingHTML('radarr') . '</div>
  58. </div>
  59. </div>'
  60. ),
  61. array(
  62. 'type' => 'switch',
  63. 'name' => 'radarrSocksEnabled',
  64. 'label' => 'Enable',
  65. 'value' => $this->config['radarrSocksEnabled']
  66. ),
  67. array(
  68. 'type' => 'select',
  69. 'name' => 'radarrSocksAuth',
  70. 'label' => 'Minimum Authentication',
  71. 'value' => $this->config['radarrSocksAuth'],
  72. 'options' => $this->groupOptions
  73. ),
  74. ),
  75. 'Queue' => array(
  76. array(
  77. 'type' => 'switch',
  78. 'name' => 'homepageRadarrQueueEnabled',
  79. 'label' => 'Enable',
  80. 'value' => $this->config['homepageRadarrQueueEnabled']
  81. ),
  82. array(
  83. 'type' => 'select',
  84. 'name' => 'homepageRadarrQueueAuth',
  85. 'label' => 'Minimum Authentication',
  86. 'value' => $this->config['homepageRadarrQueueAuth'],
  87. 'options' => $this->groupOptions
  88. ),
  89. array(
  90. 'type' => 'switch',
  91. 'name' => 'homepageRadarrQueueCombine',
  92. 'label' => 'Add to Combined Downloader',
  93. 'value' => $this->config['homepageRadarrQueueCombine']
  94. ),
  95. array(
  96. 'type' => 'select',
  97. 'name' => 'homepageRadarrQueueRefresh',
  98. 'label' => 'Refresh Seconds',
  99. 'value' => $this->config['homepageRadarrQueueRefresh'],
  100. 'options' => $this->timeOptions()
  101. ),
  102. ),
  103. 'Calendar' => array(
  104. array(
  105. 'type' => 'number',
  106. 'name' => 'calendarStart',
  107. 'label' => '# of Days Before',
  108. 'value' => $this->config['calendarStart'],
  109. 'placeholder' => ''
  110. ),
  111. array(
  112. 'type' => 'number',
  113. 'name' => 'calendarEnd',
  114. 'label' => '# of Days After',
  115. 'value' => $this->config['calendarEnd'],
  116. 'placeholder' => ''
  117. ),
  118. array(
  119. 'type' => 'select',
  120. 'name' => 'calendarFirstDay',
  121. 'label' => 'Start Day',
  122. 'value' => $this->config['calendarFirstDay'],
  123. 'options' => $this->daysOptions()
  124. ),
  125. array(
  126. 'type' => 'select',
  127. 'name' => 'calendarDefault',
  128. 'label' => 'Default View',
  129. 'value' => $this->config['calendarDefault'],
  130. 'options' => $this->calendarDefaultOptions()
  131. ),
  132. array(
  133. 'type' => 'select',
  134. 'name' => 'calendarTimeFormat',
  135. 'label' => 'Time Format',
  136. 'value' => $this->config['calendarTimeFormat'],
  137. 'options' => $this->timeFormatOptions()
  138. ),
  139. array(
  140. 'type' => 'select',
  141. 'name' => 'calendarLocale',
  142. 'label' => 'Locale',
  143. 'value' => $this->config['calendarLocale'],
  144. 'options' => $this->calendarLocaleOptions()
  145. ),
  146. array(
  147. 'type' => 'select',
  148. 'name' => 'calendarLimit',
  149. 'label' => 'Items Per Day',
  150. 'value' => $this->config['calendarLimit'],
  151. 'options' => $this->limitOptions()
  152. ),
  153. array(
  154. 'type' => 'select',
  155. 'name' => 'calendarRefresh',
  156. 'label' => 'Refresh Seconds',
  157. 'value' => $this->config['calendarRefresh'],
  158. 'options' => $this->timeOptions()
  159. ),
  160. array(
  161. 'type' => 'switch',
  162. 'name' => 'radarrUnmonitored',
  163. 'label' => 'Show Unmonitored',
  164. 'value' => $this->config['radarrUnmonitored']
  165. ),
  166. array(
  167. 'type' => 'switch',
  168. 'name' => 'radarrPhysicalRelease',
  169. 'label' => 'Show Physical Release',
  170. 'value' => $this->config['radarrPhysicalRelease']
  171. ),
  172. array(
  173. 'type' => 'switch',
  174. 'name' => 'radarrDigitalRelease',
  175. 'label' => 'Show Digital Release',
  176. 'value' => $this->config['radarrDigitalRelease']
  177. ),
  178. array(
  179. 'type' => 'switch',
  180. 'name' => 'radarrCinemaRelease',
  181. 'label' => 'Show Cinema Releases',
  182. 'value' => $this->config['radarrCinemaRelease']
  183. )
  184. ),
  185. 'Test Connection' => array(
  186. array(
  187. 'type' => 'blank',
  188. 'label' => 'Please Save before Testing'
  189. ),
  190. array(
  191. 'type' => 'button',
  192. 'label' => '',
  193. 'icon' => 'fa fa-flask',
  194. 'class' => 'pull-right',
  195. 'text' => 'Test Connection',
  196. 'attr' => 'onclick="testAPIConnection(\'radarr\')"'
  197. ),
  198. )
  199. )
  200. );
  201. return array_merge($homepageInformation, $homepageSettings);
  202. }
  203. public function testConnectionRadarr()
  204. {
  205. if (empty($this->config['radarrURL'])) {
  206. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  207. return false;
  208. }
  209. if (empty($this->config['radarrToken'])) {
  210. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  211. return false;
  212. }
  213. $failed = false;
  214. $errors = '';
  215. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  216. foreach ($list as $key => $value) {
  217. try {
  218. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
  219. $results = $downloader->getRootFolder();
  220. $downloadList = json_decode($results, true);
  221. if (is_array($downloadList) || is_object($downloadList)) {
  222. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  223. if (!is_array($queue)) {
  224. $ip = $value['url'];
  225. $errors .= $ip . ': ' . $queue;
  226. $failed = true;
  227. }
  228. } else {
  229. $ip = $value['url'];
  230. $errors .= $ip . ': Response was not JSON';
  231. $failed = true;
  232. }
  233. } catch (Exception $e) {
  234. $failed = true;
  235. $ip = $value['url'];
  236. $errors .= $ip . ': ' . $e->getMessage();
  237. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  238. }
  239. }
  240. if ($failed) {
  241. $this->setAPIResponse('error', $errors, 500);
  242. return false;
  243. } else {
  244. $this->setAPIResponse('success', null, 200);
  245. return true;
  246. }
  247. }
  248. public function radarrHomepagePermissions($key = null)
  249. {
  250. $permissions = [
  251. 'calendar' => [
  252. 'enabled' => [
  253. 'homepageRadarrEnabled'
  254. ],
  255. 'auth' => [
  256. 'homepageRadarrAuth'
  257. ],
  258. 'not_empty' => [
  259. 'radarrURL',
  260. 'radarrToken'
  261. ]
  262. ],
  263. 'queue' => [
  264. 'enabled' => [
  265. 'homepageRadarrEnabled',
  266. 'homepageRadarrQueueEnabled'
  267. ],
  268. 'auth' => [
  269. 'homepageRadarrAuth',
  270. 'homepageRadarrQueueAuth'
  271. ],
  272. 'not_empty' => [
  273. 'radarrURL',
  274. 'radarrToken'
  275. ]
  276. ]
  277. ];
  278. if (array_key_exists($key, $permissions)) {
  279. return $permissions[$key];
  280. } elseif ($key == 'all') {
  281. return $permissions;
  282. } else {
  283. return [];
  284. }
  285. }
  286. public function homepageOrderRadarrQueue()
  287. {
  288. if ($this->homepageItemPermissions($this->radarrHomepagePermissions('queue'))) {
  289. $loadingBox = ($this->config['homepageRadarrQueueCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  290. $builder = ($this->config['homepageRadarrQueueCombine']) ? 'buildDownloaderCombined(\'radarr\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("radarr"));';
  291. return '
  292. <div id="' . __FUNCTION__ . '">
  293. ' . $loadingBox . '
  294. <script>
  295. // homepageOrderRadarrQueue
  296. ' . $builder . '
  297. homepageDownloader("radarr", "' . $this->config['homepageRadarrQueueRefresh'] . '");
  298. // End homepageOrderRadarrQueue
  299. </script>
  300. </div>
  301. ';
  302. }
  303. }
  304. public function getRadarrQueue()
  305. {
  306. if (!$this->homepageItemPermissions($this->radarrHomepagePermissions('queue'), true)) {
  307. return false;
  308. }
  309. $queueItems = array();
  310. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  311. foreach ($list as $key => $value) {
  312. try {
  313. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
  314. $results = $downloader->getQueue();
  315. $downloadList = json_decode($results, true);
  316. if (is_array($downloadList) || is_object($downloadList)) {
  317. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  318. } else {
  319. $queue = '';
  320. }
  321. if (!empty($queue)) {
  322. $queueItems = array_merge($queueItems, $queue);
  323. }
  324. } catch (Exception $e) {
  325. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  326. }
  327. }
  328. $api['content']['queueItems'] = $queueItems;
  329. $api['content']['historyItems'] = false;
  330. $api['content'] = isset($api['content']) ? $api['content'] : false;
  331. $this->setAPIResponse('success', null, 200, $api);
  332. return $api;;
  333. }
  334. public function getRadarrCalendar($startDate = null, $endDate = null)
  335. {
  336. $startDate = ($startDate) ?? $_GET['start'];
  337. $endDate = ($endDate) ?? $_GET['end'];
  338. if (!$this->homepageItemPermissions($this->radarrHomepagePermissions('calendar'), true)) {
  339. return false;
  340. }
  341. $calendarItems = array();
  342. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  343. foreach ($list as $key => $value) {
  344. try {
  345. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
  346. $results = $downloader->getCalendar($startDate, $endDate, $this->config['radarrUnmonitored']);
  347. $result = json_decode($results, true);
  348. if (is_array($result) || is_object($result)) {
  349. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatRadarrCalendar($results, $key, $value['url']);
  350. } else {
  351. $calendar = '';
  352. }
  353. } catch (Exception $e) {
  354. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  355. }
  356. if (!empty($calendar)) {
  357. $calendarItems = array_merge($calendarItems, $calendar);
  358. }
  359. }
  360. $this->setAPIResponse('success', null, 200, $calendarItems);
  361. return $calendarItems;
  362. }
  363. public function formatRadarrCalendar($array, $number, $url)
  364. {
  365. $url = rtrim($url, '/'); //remove trailing slash
  366. $url = $url . '/api';
  367. $array = json_decode($array, true);
  368. $gotCalendar = array();
  369. $i = 0;
  370. foreach ($array as $child) {
  371. for ($j = 0; $j < 3; $j++) {
  372. $type = [];
  373. if ($j == 0 && $this->config['radarrPhysicalRelease'] && isset($child['physicalRelease'])) {
  374. $releaseDate = $child['physicalRelease'];
  375. array_push($type, "physical");
  376. if (isset($child['digitalRelease']) && $child['physicalRelease'] == $child['digitalRelease']) {
  377. array_push($type, "digital");
  378. $j++;
  379. }
  380. if (isset($child['inCinemas']) && $child['physicalRelease'] == $child['inCinemas']) {
  381. array_push($type, "cinema");
  382. $j += 2;
  383. }
  384. } elseif ($j == 1 && $this->config['radarrDigitalRelease'] && isset($child['digitalRelease'])) {
  385. $releaseDate = $child['digitalRelease'];
  386. array_push($type, "digital");
  387. if (isset($child['inCinemas']) && $child['digitalRelease'] == $child['inCinemas']) {
  388. array_push($type, "cinema");
  389. $j++;
  390. }
  391. } elseif ($j == 2 && $this->config['radarrCinemaRelease'] && isset($child['inCinemas'])) {
  392. $releaseDate = $child['inCinemas'];
  393. array_push($type, "cinema");
  394. } else {
  395. continue;
  396. }
  397. $i++;
  398. $movieName = $child['title'];
  399. $movieID = $child['tmdbId'];
  400. if (!isset($movieID)) {
  401. $movieID = "";
  402. }
  403. $releaseDate = strtotime($releaseDate);
  404. $releaseDate = date("Y-m-d", $releaseDate);
  405. if (new DateTime() < new DateTime($releaseDate)) {
  406. $notReleased = "true";
  407. } else {
  408. $notReleased = "false";
  409. }
  410. $downloaded = $child['hasFile'];
  411. if ($downloaded == "0" && $notReleased == "true") {
  412. $downloaded = "text-info";
  413. } elseif ($downloaded == "1") {
  414. $downloaded = "text-success";
  415. } else {
  416. $downloaded = "text-danger";
  417. }
  418. $banner = "/plugins/images/cache/no-np.png";
  419. foreach ($child['images'] as $image) {
  420. if ($image['coverType'] == "banner" || $image['coverType'] == "fanart") {
  421. if (strpos($image['url'], '://') === false) {
  422. $imageUrl = $image['url'];
  423. $urlParts = explode("/", $url);
  424. $imageParts = explode("/", $image['url']);
  425. if ($imageParts[1] == end($urlParts)) {
  426. unset($imageParts[1]);
  427. $imageUrl = implode("/", $imageParts);
  428. }
  429. $banner = $url . $imageUrl . '?apikey=' . $this->config['radarrToken'];
  430. } else {
  431. $banner = $image['url'];
  432. }
  433. }
  434. }
  435. if ($banner !== "/plugins/images/cache/no-np.png" || (strpos($banner, 'apikey') !== false)) {
  436. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  437. $imageURL = $banner;
  438. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  439. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  440. if (!file_exists($cacheFile)) {
  441. $this->cacheImage($imageURL, $movieID);
  442. unset($imageURL);
  443. unset($cacheFile);
  444. }
  445. }
  446. $alternativeTitles = "";
  447. if (!empty($child['alternativeTitles'])) {
  448. foreach ($child['alternativeTitles'] as $alternative) {
  449. $alternativeTitles .= $alternative['title'] . ', ';
  450. }
  451. } elseif (!empty($child['alternateTitles'])) { //v3 API
  452. foreach ($child['alternateTitles'] as $alternative) {
  453. $alternativeTitles .= $alternative['title'] . ', ';
  454. }
  455. }
  456. $alternativeTitles = empty($alternativeTitles) ? "" : substr($alternativeTitles, 0, -2);
  457. $details = array(
  458. "topTitle" => $movieName,
  459. "bottomTitle" => $alternativeTitles,
  460. "status" => $child['status'],
  461. "overview" => $child['overview'],
  462. "runtime" => $child['runtime'],
  463. "image" => $banner,
  464. "ratings" => $child['ratings']['value'],
  465. "videoQuality" => $child["hasFile"] ? @$child['movieFile']['quality']['quality']['name'] : "unknown",
  466. "audioChannels" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioChannels'] : "unknown",
  467. "audioCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioFormat'] : "unknown",
  468. "videoCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['videoCodec'] : "unknown",
  469. "size" => $child["hasFile"] ? @$child['movieFile']['size'] : "unknown",
  470. "genres" => $child['genres'],
  471. "year" => isset($child['year']) ? $child['year'] : '',
  472. "studio" => isset($child['studio']) ? $child['studio'] : '',
  473. );
  474. array_push($gotCalendar, array(
  475. "id" => "Radarr-" . $number . "-" . $i,
  476. "title" => $movieName,
  477. "start" => $releaseDate,
  478. "className" => "inline-popups bg-calendar movieID--" . $movieID,
  479. "imagetype" => "film " . $downloaded,
  480. "imagetypeFilter" => "film",
  481. "downloadFilter" => $downloaded,
  482. "releaseType" => $type,
  483. "bgColor" => str_replace('text', 'bg', $downloaded),
  484. "details" => $details
  485. ));
  486. }
  487. }
  488. if ($i != 0) {
  489. return $gotCalendar;
  490. }
  491. return false;
  492. }
  493. }