lidarr.php 8.7 KB

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