lidarr.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. if (array_key_exists($key, $permissions)) {
  130. return $permissions[$key];
  131. } elseif ($key == 'all') {
  132. return $permissions;
  133. } else {
  134. return [];
  135. }
  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->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  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'];
  171. $endDate = ($endDate) ?? $_GET['end'];
  172. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('calendar'), true)) {
  173. return false;
  174. }
  175. $calendarItems = array();
  176. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  177. foreach ($list as $key => $value) {
  178. try {
  179. $options = $this->requestOptions($value['url'], null, $this->config['lidarrDisableCertCheck'], $this->config['lidarrUseCustomCertificate']);
  180. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr', null, null, $options);
  181. $results = $downloader->getCalendar($startDate, $endDate);
  182. $result = json_decode($results, true);
  183. if (is_array($result) || is_object($result)) {
  184. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  185. } else {
  186. $calendar = '';
  187. }
  188. } catch (Exception $e) {
  189. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  190. }
  191. if (!empty($calendar)) {
  192. $calendarItems = array_merge($calendarItems, $calendar);
  193. }
  194. }
  195. $this->setAPIResponse('success', null, 200, $calendarItems);
  196. return $calendarItems;
  197. }
  198. public function formatLidarrCalendar($array, $number)
  199. {
  200. $array = json_decode($array, true);
  201. $gotCalendar = array();
  202. $i = 0;
  203. foreach ($array as $child) {
  204. $i++;
  205. $albumName = $child['title'];
  206. $artistName = $child['artist']['artistName'];
  207. $albumID = '';
  208. $releaseDate = $child['releaseDate'];
  209. $releaseDate = strtotime($releaseDate);
  210. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  211. if (new DateTime() < new DateTime($releaseDate)) {
  212. $unaired = true;
  213. }
  214. if (isset($child['statistics']['percentOfTracks'])) {
  215. if ($child['statistics']['percentOfTracks'] == '100.0') {
  216. $downloaded = '1';
  217. } else {
  218. $downloaded = '0';
  219. }
  220. } else {
  221. $downloaded = '0';
  222. }
  223. if ($downloaded == "0" && isset($unaired)) {
  224. $downloaded = "text-info";
  225. } elseif ($downloaded == "1") {
  226. $downloaded = "text-success";
  227. } else {
  228. $downloaded = "text-danger";
  229. }
  230. $fanart = "/plugins/images/cache/no-np.png";
  231. foreach ($child['artist']['images'] as $image) {
  232. if ($image['coverType'] == "fanart") {
  233. $fanart = str_replace('http://', 'https://', $image['url']);
  234. }
  235. }
  236. $details = array(
  237. "seasonCount" => '',
  238. "status" => '',
  239. "topTitle" => $albumName,
  240. "bottomTitle" => $artistName,
  241. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  242. "runtime" => '',
  243. "image" => $fanart,
  244. "ratings" => $child['artist']['ratings']['value'],
  245. "videoQuality" => "unknown",
  246. "audioChannels" => "unknown",
  247. "audioCodec" => "unknown",
  248. "videoCodec" => "unknown",
  249. "size" => "unknown",
  250. "genres" => $child['genres'],
  251. );
  252. array_push($gotCalendar, array(
  253. "id" => "Lidarr-" . $number . "-" . $i,
  254. "title" => $artistName,
  255. "start" => $child['releaseDate'],
  256. "className" => "inline-popups bg-calendar calendar-item musicID--",
  257. "imagetype" => "music " . $downloaded,
  258. "imagetypeFilter" => "music",
  259. "downloadFilter" => $downloaded,
  260. "bgColor" => str_replace('text', 'bg', $downloaded),
  261. "details" => $details,
  262. "data" => $child
  263. ));
  264. }
  265. if ($i != 0) {
  266. return $gotCalendar;
  267. }
  268. return false;
  269. }
  270. }