lidarr.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. 'Misc Options' => [
  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('calendar-link-url', 'lidarrCalendarLink'),
  45. ],
  46. 'Test Connection' => [
  47. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  48. $this->settingsOption('test', 'lidarr'),
  49. ]
  50. ]
  51. ];
  52. return array_merge($homepageInformation, $homepageSettings);
  53. }
  54. public function testConnectionLidarr()
  55. {
  56. if (empty($this->config['lidarrURL'])) {
  57. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  58. return false;
  59. }
  60. if (empty($this->config['lidarrToken'])) {
  61. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  62. return false;
  63. }
  64. $failed = false;
  65. $errors = '';
  66. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  67. foreach ($list as $key => $value) {
  68. try {
  69. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  70. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null . null, $options);
  71. $results = $downloader->getRootFolder();
  72. $downloadList = json_decode($results, true);
  73. if (is_array($downloadList) || is_object($downloadList)) {
  74. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  75. if (!is_array($queue)) {
  76. $ip = $value['url'];
  77. $errors .= $ip . ': ' . $queue;
  78. $failed = true;
  79. }
  80. } else {
  81. $ip = $value['url'];
  82. $errors .= $ip . ': Response was not JSON';
  83. $failed = true;
  84. }
  85. } catch (Exception $e) {
  86. $failed = true;
  87. $ip = $value['url'];
  88. $errors .= $ip . ': ' . $e->getMessage();
  89. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  90. }
  91. }
  92. if ($failed) {
  93. $this->setAPIResponse('error', $errors, 500);
  94. return false;
  95. } else {
  96. $this->setAPIResponse('success', null, 200);
  97. return true;
  98. }
  99. }
  100. public function lidarrHomepagePermissions($key = null)
  101. {
  102. $permissions = [
  103. 'calendar' => [
  104. 'enabled' => [
  105. 'homepageLidarrEnabled'
  106. ],
  107. 'auth' => [
  108. 'homepageLidarrAuth'
  109. ],
  110. 'not_empty' => [
  111. 'lidarrURL',
  112. 'lidarrToken'
  113. ]
  114. ],
  115. 'queue' => [
  116. 'enabled' => [
  117. 'homepageLidarrEnabled',
  118. 'homepageLidarrQueueEnabled'
  119. ],
  120. 'auth' => [
  121. 'homepageLidarrAuth',
  122. 'homepageLidarrQueueAuth'
  123. ],
  124. 'not_empty' => [
  125. 'lidarrURL',
  126. 'lidarrToken'
  127. ]
  128. ]
  129. ];
  130. return $this->homepageCheckKeyPermissions($key, $permissions);
  131. }
  132. public function getLidarrQueue()
  133. {
  134. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('queue'), true)) {
  135. return false;
  136. }
  137. $queueItems = array();
  138. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  139. foreach ($list as $key => $value) {
  140. try {
  141. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  142. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null, null, $options);
  143. $results = $downloader->getQueue();
  144. $downloadList = json_decode($results, true);
  145. if (is_array($downloadList) || is_object($downloadList)) {
  146. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  147. } else {
  148. $queue = '';
  149. }
  150. if (!empty($queue)) {
  151. $queueItems = array_merge($queueItems, $queue);
  152. }
  153. } catch (Exception $e) {
  154. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  155. }
  156. }
  157. $api['content']['queueItems'] = $queueItems;
  158. $api['content']['historyItems'] = false;
  159. $api['content'] = isset($api['content']) ? $api['content'] : false;
  160. $this->setAPIResponse('success', null, 200, $api);
  161. return $api;;
  162. }
  163. public function getLidarrCalendar($startDate = null, $endDate = null)
  164. {
  165. $startDate = ($startDate) ?? $_GET['start'] ?? date('Y-m-d', strtotime('-' . $this->config['calendarStart'] . ' days'));
  166. $endDate = ($endDate) ?? $_GET['end'] ?? date('Y-m-d', strtotime('+' . $this->config['calendarEnd'] . ' days'));
  167. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('calendar'), true)) {
  168. return false;
  169. }
  170. if ($this->demo) {
  171. return $this->demoData('lidarr/calendar.json');
  172. }
  173. $calendarItems = array();
  174. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  175. foreach ($list as $key => $value) {
  176. try {
  177. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  178. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null, null, $options);
  179. $results = $downloader->getCalendar($startDate, $endDate);
  180. $result = json_decode($results, true);
  181. if (is_array($result) || is_object($result)) {
  182. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  183. } else {
  184. $calendar = '';
  185. }
  186. } catch (Exception $e) {
  187. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  188. }
  189. if (!empty($calendar)) {
  190. $calendarItems = array_merge($calendarItems, $calendar);
  191. }
  192. }
  193. $this->setAPIResponse('success', null, 200, $calendarItems);
  194. return $calendarItems;
  195. }
  196. public function formatLidarrCalendar($array, $number)
  197. {
  198. $array = json_decode($array, true);
  199. $gotCalendar = array();
  200. $i = 0;
  201. foreach ($array as $child) {
  202. $i++;
  203. $albumName = $child['title'];
  204. $artistName = $child['artist']['artistName'];
  205. $albumID = '';
  206. $releaseDate = $child['releaseDate'];
  207. $releaseDate = strtotime($releaseDate);
  208. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  209. if (new DateTime() < new DateTime($releaseDate)) {
  210. $unaired = true;
  211. }
  212. if (isset($child['statistics']['percentOfTracks'])) {
  213. if ($child['statistics']['percentOfTracks'] == '100.0') {
  214. $downloaded = '1';
  215. } else {
  216. $downloaded = '0';
  217. }
  218. } else {
  219. $downloaded = '0';
  220. }
  221. if ($downloaded == "0" && isset($unaired)) {
  222. $downloaded = "text-info";
  223. } elseif ($downloaded == "1") {
  224. $downloaded = "text-success";
  225. } else {
  226. $downloaded = "text-danger";
  227. }
  228. $fanart = "/plugins/images/homepage/no-np.png";
  229. foreach ($child['artist']['images'] as $image) {
  230. if ($image['coverType'] == "fanart") {
  231. $fanart = str_replace('http://', 'https://', $image['url']);
  232. }
  233. }
  234. $href = $this->config['lidarrCalendarLink'] ?? '';
  235. if (empty($href) && !empty($this->config['lidarrURL'])) {
  236. $href_arr = explode(',', $this->config['lidarrURL']);
  237. $href = reset($href_arr);
  238. }
  239. if (!empty($href)) {
  240. $href = $href . '/artist/' . $child['artist']['foreignArtistId'];
  241. $href = str_replace("//artist/", "/artist/", $href);
  242. }
  243. $details = array(
  244. "seasonCount" => '',
  245. "status" => '',
  246. "topTitle" => $albumName,
  247. "bottomTitle" => $artistName,
  248. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  249. "runtime" => '',
  250. "image" => $fanart,
  251. "ratings" => $child['artist']['ratings']['value'],
  252. "videoQuality" => "unknown",
  253. "audioChannels" => "unknown",
  254. "audioCodec" => "unknown",
  255. "videoCodec" => "unknown",
  256. "size" => "unknown",
  257. "genres" => $child['genres'],
  258. "href" => strtolower($href),
  259. "icon" => "/plugins/images/tabs/lidarr.png",
  260. );
  261. array_push($gotCalendar, array(
  262. "id" => "Lidarr-" . $number . "-" . $i,
  263. "title" => $artistName,
  264. "start" => $child['releaseDate'],
  265. "className" => "inline-popups bg-calendar calendar-item musicID--",
  266. "imagetype" => "music " . $downloaded,
  267. "imagetypeFilter" => "music",
  268. "downloadFilter" => $downloaded,
  269. "bgColor" => str_replace('text', 'bg', $downloaded),
  270. "details" => $details,
  271. "data" => $child
  272. ));
  273. }
  274. if ($i != 0) {
  275. return $gotCalendar;
  276. }
  277. return false;
  278. }
  279. }