radarr.php 16 KB

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