lidarr.php 9.0 KB

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