lidarr.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. trait LidarrHomepageItem
  3. {
  4. public function lidarrSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Lidarr',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/lidarr.png',
  10. 'category' => 'PMR',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageLidarrEnabled'),
  21. $this->settingsOption('auth', 'homepageLidarrAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('multiple-url', 'lidarrURL'),
  25. $this->settingsOption('multiple-token', 'lidarrToken'),
  26. $this->settingsOption('disable-cert-check', 'lidarrDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'lidarrUseCustomCertificate'),
  28. ],
  29. 'API SOCKS' => [
  30. $this->settingsOption('socks', 'lidarr'),
  31. $this->settingsOption('blank'),
  32. $this->settingsOption('enable', 'lidarrSocksEnabled'),
  33. $this->settingsOption('auth', 'lidarrSocksAuth'),
  34. ],
  35. 'Calendar' => [
  36. $this->settingsOption('calendar-start', 'calendarStart'),
  37. $this->settingsOption('calendar-end', 'calendarEnd'),
  38. $this->settingsOption('calendar-starting-day', 'calendarFirstDay'),
  39. $this->settingsOption('calendar-default-view', 'calendarDefault'),
  40. $this->settingsOption('calendar-time-format', 'calendarTimeFormat'),
  41. $this->settingsOption('calendar-locale', 'calendarLocale'),
  42. $this->settingsOption('calendar-limit', 'calendarLimit'),
  43. $this->settingsOption('refresh', 'calendarRefresh'),
  44. $this->settingsOption('blank', '', ['type' => 'html', 'html' => '<hr />']),
  45. $this->settingsOption('blank', '', ['type' => 'html', 'html' => '<hr />']),
  46. $this->settingsOption('enable', 'lidarrIcon', ['label' => 'Show Lidarr Icon']),
  47. $this->settingsOption('calendar-link-url', 'lidarrCalendarLink'),
  48. $this->settingsOption('blank'),
  49. $this->settingsOption('calendar-frame-target', 'lidarrFrameTarget')
  50. ],
  51. 'Test Connection' => [
  52. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  53. $this->settingsOption('test', 'lidarr'),
  54. ]
  55. ]
  56. ];
  57. return array_merge($homepageInformation, $homepageSettings);
  58. }
  59. public function testConnectionLidarr()
  60. {
  61. if (empty($this->config['lidarrURL'])) {
  62. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  63. return false;
  64. }
  65. if (empty($this->config['lidarrToken'])) {
  66. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  67. return false;
  68. }
  69. $failed = false;
  70. $errors = '';
  71. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  72. foreach ($list as $key => $value) {
  73. try {
  74. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  75. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null . null, $options);
  76. $results = $downloader->getRootFolder();
  77. $downloadList = json_decode($results, true);
  78. if (is_array($downloadList) || is_object($downloadList)) {
  79. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  80. if (!is_array($queue)) {
  81. $ip = $value['url'];
  82. $errors .= $ip . ': ' . $queue;
  83. $failed = true;
  84. }
  85. } else {
  86. $ip = $value['url'];
  87. $errors .= $ip . ': Response was not JSON';
  88. $failed = true;
  89. }
  90. } catch (Exception $e) {
  91. $failed = true;
  92. $ip = $value['url'];
  93. $errors .= $ip . ': ' . $e->getMessage();
  94. $this->setLoggerChannel('Lidarr')->error($e);
  95. }
  96. }
  97. if ($failed) {
  98. $this->setAPIResponse('error', $errors, 500);
  99. return false;
  100. } else {
  101. $this->setAPIResponse('success', null, 200);
  102. return true;
  103. }
  104. }
  105. public function lidarrHomepagePermissions($key = null)
  106. {
  107. $permissions = [
  108. 'calendar' => [
  109. 'enabled' => [
  110. 'homepageLidarrEnabled'
  111. ],
  112. 'auth' => [
  113. 'homepageLidarrAuth'
  114. ],
  115. 'not_empty' => [
  116. 'lidarrURL',
  117. 'lidarrToken'
  118. ]
  119. ],
  120. 'queue' => [
  121. 'enabled' => [
  122. 'homepageLidarrEnabled',
  123. 'homepageLidarrQueueEnabled'
  124. ],
  125. 'auth' => [
  126. 'homepageLidarrAuth',
  127. 'homepageLidarrQueueAuth'
  128. ],
  129. 'not_empty' => [
  130. 'lidarrURL',
  131. 'lidarrToken'
  132. ]
  133. ]
  134. ];
  135. return $this->homepageCheckKeyPermissions($key, $permissions);
  136. }
  137. public function getLidarrQueue()
  138. {
  139. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('queue'), true)) {
  140. return false;
  141. }
  142. $queueItems = array();
  143. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  144. foreach ($list as $key => $value) {
  145. try {
  146. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  147. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null, null, $options);
  148. $results = $downloader->getQueue();
  149. $downloadList = json_decode($results, true);
  150. if (is_array($downloadList) || is_object($downloadList)) {
  151. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  152. } else {
  153. $queue = '';
  154. }
  155. if (!empty($queue)) {
  156. $queueItems = array_merge($queueItems, $queue);
  157. }
  158. } catch (Exception $e) {
  159. $this->setLoggerChannel('Lidarr')->error($e);
  160. }
  161. }
  162. $api['content']['queueItems'] = $queueItems;
  163. $api['content']['historyItems'] = false;
  164. $api['content'] = isset($api['content']) ? $api['content'] : false;
  165. $this->setAPIResponse('success', null, 200, $api);
  166. return $api;;
  167. }
  168. public function getLidarrCalendar($startDate = null, $endDate = null)
  169. {
  170. $startDate = ($startDate) ?? $_GET['start'] ?? date('Y-m-d', strtotime('-' . $this->config['calendarStart'] . ' days'));
  171. $endDate = ($endDate) ?? $_GET['end'] ?? date('Y-m-d', strtotime('+' . $this->config['calendarEnd'] . ' days'));
  172. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('calendar'), true)) {
  173. return false;
  174. }
  175. if ($this->demo) {
  176. return $this->demoData('lidarr/calendar.json');
  177. }
  178. $calendarItems = array();
  179. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  180. foreach ($list as $key => $value) {
  181. try {
  182. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  183. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null, null, $options);
  184. $results = $downloader->getCalendar($startDate, $endDate);
  185. $result = json_decode($results, true);
  186. if (is_array($result) || is_object($result)) {
  187. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  188. } else {
  189. $calendar = '';
  190. }
  191. } catch (Exception $e) {
  192. $this->setLoggerChannel('Lidarr')->error($e);
  193. }
  194. if (!empty($calendar)) {
  195. $calendarItems = array_merge($calendarItems, $calendar);
  196. }
  197. }
  198. $this->setAPIResponse('success', null, 200, $calendarItems);
  199. return $calendarItems;
  200. }
  201. public function formatLidarrCalendar($array, $number)
  202. {
  203. $array = json_decode($array, true);
  204. $gotCalendar = array();
  205. $i = 0;
  206. foreach ($array as $child) {
  207. $i++;
  208. $albumName = $child['title'];
  209. $artistName = $child['artist']['artistName'];
  210. $albumID = '';
  211. $releaseDate = $child['releaseDate'];
  212. $releaseDate = strtotime($releaseDate);
  213. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  214. if (new DateTime() < new DateTime($releaseDate)) {
  215. $unaired = true;
  216. }
  217. if (isset($child['statistics']['percentOfTracks'])) {
  218. if ($child['statistics']['percentOfTracks'] == '100.0') {
  219. $downloaded = '1';
  220. } else {
  221. $downloaded = '0';
  222. }
  223. } else {
  224. $downloaded = '0';
  225. }
  226. if ($downloaded == "0" && isset($unaired)) {
  227. $downloaded = "text-info";
  228. } elseif ($downloaded == "1") {
  229. $downloaded = "text-success";
  230. } else {
  231. $downloaded = "text-danger";
  232. }
  233. $fanart = "/plugins/images/homepage/no-np.png";
  234. foreach ($child['artist']['images'] as $image) {
  235. if ($image['coverType'] == "fanart") {
  236. $fanart = str_replace('http://', 'https://', $image['url']);
  237. }
  238. }
  239. $href = $this->config['lidarrCalendarLink'] ?? '';
  240. if (empty($href) && !empty($this->config['lidarrURL'])) {
  241. $href_arr = explode(',', $this->config['lidarrURL']);
  242. $href = reset($href_arr);
  243. }
  244. if (!empty($href)) {
  245. $href = $href . '/artist/' . $child['artist']['foreignArtistId'];
  246. $href = str_replace("//artist/", "/artist/", $href);
  247. }
  248. $details = array(
  249. "seasonCount" => '',
  250. "status" => '',
  251. "topTitle" => $albumName,
  252. "bottomTitle" => $artistName,
  253. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  254. "runtime" => '',
  255. "image" => $fanart,
  256. "ratings" => $child['artist']['ratings']['value'],
  257. "videoQuality" => "unknown",
  258. "audioChannels" => "unknown",
  259. "audioCodec" => "unknown",
  260. "videoCodec" => "unknown",
  261. "size" => "unknown",
  262. "genres" => $child['genres'],
  263. "href" => strtolower($href),
  264. "icon" => "/plugins/images/tabs/lidarr.png",
  265. "frame" => $this->config['lidarrFrameTarget'],
  266. "showLink" => $this->config['lidarrIcon']
  267. );
  268. array_push($gotCalendar, array(
  269. "id" => "Lidarr-" . $number . "-" . $i,
  270. "title" => $artistName,
  271. "start" => $child['releaseDate'],
  272. "className" => "inline-popups bg-calendar calendar-item musicID--",
  273. "imagetype" => "music " . $downloaded,
  274. "imagetypeFilter" => "music",
  275. "downloadFilter" => $downloaded,
  276. "bgColor" => str_replace('text', 'bg', $downloaded),
  277. "details" => $details,
  278. "data" => $child
  279. ));
  280. }
  281. if ($i != 0) {
  282. return $gotCalendar;
  283. }
  284. return false;
  285. }
  286. }