radarr.php 16 KB

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