radarr.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. ),
  159. 'Test Connection' => array(
  160. array(
  161. 'type' => 'blank',
  162. 'label' => 'Please Save before Testing'
  163. ),
  164. array(
  165. 'type' => 'button',
  166. 'label' => '',
  167. 'icon' => 'fa fa-flask',
  168. 'class' => 'pull-right',
  169. 'text' => 'Test Connection',
  170. 'attr' => 'onclick="testAPIConnection(\'radarr\')"'
  171. ),
  172. )
  173. )
  174. );
  175. }
  176. public function testConnectionRadarr()
  177. {
  178. if (empty($this->config['radarrURL'])) {
  179. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  180. return false;
  181. }
  182. if (empty($this->config['radarrToken'])) {
  183. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  184. return false;
  185. }
  186. $failed = false;
  187. $errors = '';
  188. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  189. foreach ($list as $key => $value) {
  190. try {
  191. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  192. $results = $downloader->getSystemStatus();
  193. $downloadList = json_decode($results, true);
  194. if (is_array($downloadList) || is_object($downloadList)) {
  195. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  196. if (!is_array($queue)) {
  197. $ip = $value['url'];
  198. $errors .= $ip . ': ' . $queue;
  199. $failed = true;
  200. }
  201. } else {
  202. $ip = $value['url'];
  203. $errors .= $ip . ': Response was not JSON';
  204. $failed = true;
  205. }
  206. } catch (Exception $e) {
  207. $failed = true;
  208. $ip = $value['url'];
  209. $errors .= $ip . ': ' . $e->getMessage();
  210. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  211. }
  212. }
  213. if ($failed) {
  214. $this->setAPIResponse('error', $errors, 500);
  215. return false;
  216. } else {
  217. $this->setAPIResponse('success', null, 200);
  218. return true;
  219. }
  220. }
  221. public function getRadarrQueue()
  222. {
  223. if (!$this->config['homepageRadarrEnabled']) {
  224. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  225. return false;
  226. }
  227. if (!$this->config['homepageRadarrQueueEnabled']) {
  228. $this->setAPIResponse('error', 'Radarr homepage module is not enabled', 409);
  229. return false;
  230. }
  231. if (!$this->qualifyRequest($this->config['homepageRadarrAuth'])) {
  232. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  233. return false;
  234. }
  235. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  236. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  237. return false;
  238. }
  239. if (empty($this->config['radarrURL'])) {
  240. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  241. return false;
  242. }
  243. if (empty($this->config['radarrToken'])) {
  244. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  245. return false;
  246. }
  247. $queueItems = array();
  248. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  249. foreach ($list as $key => $value) {
  250. try {
  251. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  252. $results = $downloader->getQueue();
  253. $downloadList = json_decode($results, true);
  254. if (is_array($downloadList) || is_object($downloadList)) {
  255. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  256. } else {
  257. $queue = '';
  258. }
  259. if (!empty($queue)) {
  260. $queueItems = array_merge($queueItems, $queue);
  261. }
  262. } catch (Exception $e) {
  263. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  264. }
  265. }
  266. $api['content']['queueItems'] = $queueItems;
  267. $api['content']['historyItems'] = false;
  268. $api['content'] = isset($api['content']) ? $api['content'] : false;
  269. $this->setAPIResponse('success', null, 200, $api);
  270. return $api;;
  271. }
  272. public function getRadarrCalendar($startDate = null, $endDate = null)
  273. {
  274. $startDate = ($startDate) ?? $_GET['start'];
  275. $endDate = ($endDate) ?? $_GET['end'];
  276. if (!$this->config['homepageRadarrEnabled']) {
  277. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  278. return false;
  279. }
  280. if (!$this->qualifyRequest($this->config['homepageRadarrAuth'])) {
  281. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  282. return false;
  283. }
  284. if (empty($this->config['radarrURL'])) {
  285. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  286. return false;
  287. }
  288. if (empty($this->config['radarrToken'])) {
  289. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  290. return false;
  291. }
  292. $calendarItems = array();
  293. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  294. foreach ($list as $key => $value) {
  295. try {
  296. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  297. $results = $downloader->getCalendar($startDate, $endDate);
  298. $result = json_decode($results, true);
  299. if (is_array($result) || is_object($result)) {
  300. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatRadarrCalendar($results, $key, $value['url']);
  301. } else {
  302. $calendar = '';
  303. }
  304. } catch (Exception $e) {
  305. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  306. }
  307. if (!empty($calendar)) {
  308. $calendarItems = array_merge($calendarItems, $calendar);
  309. }
  310. }
  311. $this->setAPIResponse('success', null, 200, $calendarItems);
  312. return $calendarItems;
  313. }
  314. public function formatRadarrCalendar($array, $number, $url)
  315. {
  316. $url = rtrim($url, '/'); //remove trailing slash
  317. $url = $url . '/api';
  318. $array = json_decode($array, true);
  319. $gotCalendar = array();
  320. $i = 0;
  321. foreach ($array as $child) {
  322. if (isset($child['physicalRelease'])) {
  323. $i++;
  324. $movieName = $child['title'];
  325. $movieID = $child['tmdbId'];
  326. if (!isset($movieID)) {
  327. $movieID = "";
  328. }
  329. $physicalRelease = $child['physicalRelease'];
  330. $physicalRelease = strtotime($physicalRelease);
  331. $physicalRelease = date("Y-m-d", $physicalRelease);
  332. if (new DateTime() < new DateTime($physicalRelease)) {
  333. $notReleased = "true";
  334. } else {
  335. $notReleased = "false";
  336. }
  337. $downloaded = $child['hasFile'];
  338. if ($downloaded == "0" && $notReleased == "true") {
  339. $downloaded = "text-info";
  340. } elseif ($downloaded == "1") {
  341. $downloaded = "text-success";
  342. } else {
  343. $downloaded = "text-danger";
  344. }
  345. $banner = "/plugins/images/cache/no-np.png";
  346. foreach ($child['images'] as $image) {
  347. if ($image['coverType'] == "banner" || $image['coverType'] == "fanart") {
  348. if (strpos($image['url'], '://') === false) {
  349. $imageUrl = $image['url'];
  350. $urlParts = explode("/", $url);
  351. $imageParts = explode("/", $image['url']);
  352. if ($imageParts[1] == end($urlParts)) {
  353. unset($imageParts[1]);
  354. $imageUrl = implode("/", $imageParts);
  355. }
  356. $banner = $url . $imageUrl . '?apikey=' . $this->config['radarrToken'];
  357. } else {
  358. $banner = $image['url'];
  359. }
  360. }
  361. }
  362. if ($banner !== "/plugins/images/cache/no-np.png" || (strpos($banner, 'apikey') !== false)) {
  363. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  364. $imageURL = $banner;
  365. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  366. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  367. if (!file_exists($cacheFile)) {
  368. $this->cacheImage($imageURL, $movieID);
  369. unset($imageURL);
  370. unset($cacheFile);
  371. }
  372. }
  373. $alternativeTitles = "";
  374. foreach ($child['alternativeTitles'] as $alternative) {
  375. $alternativeTitles .= $alternative['title'] . ', ';
  376. }
  377. $alternativeTitles = empty($child['alternativeTitles']) ? "" : substr($alternativeTitles, 0, -2);
  378. $details = array(
  379. "topTitle" => $movieName,
  380. "bottomTitle" => $alternativeTitles,
  381. "status" => $child['status'],
  382. "overview" => $child['overview'],
  383. "runtime" => $child['runtime'],
  384. "image" => $banner,
  385. "ratings" => $child['ratings']['value'],
  386. "videoQuality" => $child["hasFile"] ? @$child['movieFile']['quality']['quality']['name'] : "unknown",
  387. "audioChannels" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioChannels'] : "unknown",
  388. "audioCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioFormat'] : "unknown",
  389. "videoCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['videoCodec'] : "unknown",
  390. "size" => $child["hasFile"] ? @$child['movieFile']['size'] : "unknown",
  391. "genres" => $child['genres'],
  392. "year" => isset($child['year']) ? $child['year'] : '',
  393. "studio" => isset($child['studio']) ? $child['studio'] : '',
  394. );
  395. array_push($gotCalendar, array(
  396. "id" => "Radarr-" . $number . "-" . $i,
  397. "title" => $movieName,
  398. "start" => $physicalRelease,
  399. "className" => "inline-popups bg-calendar movieID--" . $movieID,
  400. "imagetype" => "film " . $downloaded,
  401. "imagetypeFilter" => "film",
  402. "downloadFilter" => $downloaded,
  403. "bgColor" => str_replace('text', 'bg', $downloaded),
  404. "details" => $details
  405. ));
  406. }
  407. }
  408. if ($i != 0) {
  409. return $gotCalendar;
  410. }
  411. return false;
  412. }
  413. }