radarr.php 16 KB

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