radarr.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. trait RadarrHomepageItem
  3. {
  4. public function testConnectionRadarr()
  5. {
  6. if (empty($this->config['radarrURL'])) {
  7. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['radarrToken'])) {
  11. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  12. return false;
  13. }
  14. $failed = false;
  15. $errors = '';
  16. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  17. foreach ($list as $key => $value) {
  18. try {
  19. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  20. $results = $downloader->getSystemStatus();
  21. $downloadList = json_decode($results, true);
  22. if (is_array($downloadList) || is_object($downloadList)) {
  23. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  24. if (!is_array($queue)) {
  25. $ip = $value['url'];
  26. $errors .= $ip . ': ' . $queue;
  27. $failed = true;
  28. }
  29. } else {
  30. $ip = $value['url'];
  31. $errors .= $ip . ': Response was not JSON';
  32. $failed = true;
  33. }
  34. } catch (Exception $e) {
  35. $failed = true;
  36. $ip = $value['url'];
  37. $errors .= $ip . ': ' . $e->getMessage();
  38. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  39. }
  40. }
  41. if ($failed) {
  42. $this->setAPIResponse('error', $errors, 500);
  43. return false;
  44. } else {
  45. $this->setAPIResponse('success', null, 200);
  46. return true;
  47. }
  48. }
  49. public function getRadarrQueue()
  50. {
  51. if (!$this->config['homepageRadarrEnabled']) {
  52. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  53. return false;
  54. }
  55. if (!$this->config['homepageRadarrQueueEnabled']) {
  56. $this->setAPIResponse('error', 'Radarr homepage module is not enabled', 409);
  57. return false;
  58. }
  59. if (!$this->qualifyRequest($this->config['homepageRadarrAuth'])) {
  60. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  61. return false;
  62. }
  63. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  64. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  65. return false;
  66. }
  67. if (empty($this->config['radarrURL'])) {
  68. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  69. return false;
  70. }
  71. if (empty($this->config['radarrToken'])) {
  72. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  73. return false;
  74. }
  75. $queueItems = array();
  76. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  77. foreach ($list as $key => $value) {
  78. try {
  79. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  80. $results = $downloader->getQueue();
  81. $downloadList = json_decode($results, true);
  82. if (is_array($downloadList) || is_object($downloadList)) {
  83. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  84. } else {
  85. $queue = '';
  86. }
  87. if (!empty($queue)) {
  88. $queueItems = array_merge($queueItems, $queue);
  89. }
  90. } catch (Exception $e) {
  91. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  92. }
  93. }
  94. $api['content']['queueItems'] = $queueItems;
  95. $api['content']['historyItems'] = false;
  96. $api['content'] = isset($api['content']) ? $api['content'] : false;
  97. $this->setAPIResponse('success', null, 200, $api);
  98. return $api;;
  99. }
  100. public function getRadarrCalendar($startDate = null, $endDate = null)
  101. {
  102. $startDate = ($startDate) ?? $_GET['start'];
  103. $endDate = ($endDate) ?? $_GET['end'];
  104. if (!$this->config['homepageRadarrEnabled']) {
  105. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  106. return false;
  107. }
  108. if (!$this->config['homepageRadarrQueueEnabled']) {
  109. $this->setAPIResponse('error', 'Radarr homepage module is not enabled', 409);
  110. return false;
  111. }
  112. if (!$this->qualifyRequest($this->config['homepageRadarrAuth'])) {
  113. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  114. return false;
  115. }
  116. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  117. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  118. return false;
  119. }
  120. if (empty($this->config['radarrURL'])) {
  121. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  122. return false;
  123. }
  124. if (empty($this->config['radarrToken'])) {
  125. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  126. return false;
  127. }
  128. $calendarItems = array();
  129. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  130. foreach ($list as $key => $value) {
  131. try {
  132. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  133. $results = $downloader->getCalendar($startDate, $endDate);
  134. $result = json_decode($results, true);
  135. if (is_array($result) || is_object($result)) {
  136. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatRadarrCalendar($results, $key, $value['url']);
  137. } else {
  138. $calendar = '';
  139. }
  140. } catch (Exception $e) {
  141. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  142. }
  143. if (!empty($calendar)) {
  144. $calendarItems = array_merge($calendarItems, $calendar);
  145. }
  146. }
  147. $this->setAPIResponse('success', null, 200, $calendarItems);
  148. return $calendarItems;
  149. }
  150. public function formatRadarrCalendar($array, $number, $url)
  151. {
  152. $url = rtrim($url, '/'); //remove trailing slash
  153. $url = $url . '/api';
  154. $array = json_decode($array, true);
  155. $gotCalendar = array();
  156. $i = 0;
  157. foreach ($array as $child) {
  158. if (isset($child['physicalRelease'])) {
  159. $i++;
  160. $movieName = $child['title'];
  161. $movieID = $child['tmdbId'];
  162. if (!isset($movieID)) {
  163. $movieID = "";
  164. }
  165. $physicalRelease = $child['physicalRelease'];
  166. $physicalRelease = strtotime($physicalRelease);
  167. $physicalRelease = date("Y-m-d", $physicalRelease);
  168. if (new DateTime() < new DateTime($physicalRelease)) {
  169. $notReleased = "true";
  170. } else {
  171. $notReleased = "false";
  172. }
  173. $downloaded = $child['hasFile'];
  174. if ($downloaded == "0" && $notReleased == "true") {
  175. $downloaded = "text-info";
  176. } elseif ($downloaded == "1") {
  177. $downloaded = "text-success";
  178. } else {
  179. $downloaded = "text-danger";
  180. }
  181. $banner = "/plugins/images/cache/no-np.png";
  182. foreach ($child['images'] as $image) {
  183. if ($image['coverType'] == "banner" || $image['coverType'] == "fanart") {
  184. if (strpos($image['url'], '://') === false) {
  185. $imageUrl = $image['url'];
  186. $urlParts = explode("/", $url);
  187. $imageParts = explode("/", $image['url']);
  188. if ($imageParts[1] == end($urlParts)) {
  189. unset($imageParts[1]);
  190. $imageUrl = implode("/", $imageParts);
  191. }
  192. $banner = $url . $imageUrl . '?apikey=' . $this->config['radarrToken'];
  193. } else {
  194. $banner = $image['url'];
  195. }
  196. }
  197. }
  198. if ($banner !== "/plugins/images/cache/no-np.png" || (strpos($banner, 'apikey') !== false)) {
  199. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  200. $imageURL = $banner;
  201. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  202. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  203. if (!file_exists($cacheFile)) {
  204. $this->cacheImage($imageURL, $movieID);
  205. unset($imageURL);
  206. unset($cacheFile);
  207. }
  208. }
  209. $alternativeTitles = "";
  210. foreach ($child['alternativeTitles'] as $alternative) {
  211. $alternativeTitles .= $alternative['title'] . ', ';
  212. }
  213. $alternativeTitles = empty($child['alternativeTitles']) ? "" : substr($alternativeTitles, 0, -2);
  214. $details = array(
  215. "topTitle" => $movieName,
  216. "bottomTitle" => $alternativeTitles,
  217. "status" => $child['status'],
  218. "overview" => $child['overview'],
  219. "runtime" => $child['runtime'],
  220. "image" => $banner,
  221. "ratings" => $child['ratings']['value'],
  222. "videoQuality" => $child["hasFile"] ? @$child['movieFile']['quality']['quality']['name'] : "unknown",
  223. "audioChannels" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioChannels'] : "unknown",
  224. "audioCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['audioFormat'] : "unknown",
  225. "videoCodec" => $child["hasFile"] ? @$child['movieFile']['mediaInfo']['videoCodec'] : "unknown",
  226. "size" => $child["hasFile"] ? @$child['movieFile']['size'] : "unknown",
  227. "genres" => $child['genres'],
  228. "year" => isset($child['year']) ? $child['year'] : '',
  229. "studio" => isset($child['studio']) ? $child['studio'] : '',
  230. );
  231. array_push($gotCalendar, array(
  232. "id" => "Radarr-" . $number . "-" . $i,
  233. "title" => $movieName,
  234. "start" => $physicalRelease,
  235. "className" => "inline-popups bg-calendar movieID--" . $movieID,
  236. "imagetype" => "film " . $downloaded,
  237. "imagetypeFilter" => "film",
  238. "downloadFilter" => $downloaded,
  239. "bgColor" => str_replace('text', 'bg', $downloaded),
  240. "details" => $details
  241. ));
  242. }
  243. }
  244. if ($i != 0) {
  245. return $gotCalendar;
  246. }
  247. return false;
  248. }
  249. }