lidarr.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. trait LidarrHomepageItem
  3. {
  4. public function lidarrSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Lidarr',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/lidarr.png',
  10. 'category' => 'PMR',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageLidarrEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageLidarrEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageLidarrAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageLidarrAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'lidarrURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['lidarrURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'lidarrToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['lidarrToken']
  41. )
  42. ),
  43. 'API SOCKS' => array(
  44. array(
  45. 'type' => 'html',
  46. 'override' => 12,
  47. 'label' => '',
  48. 'html' => '
  49. <div class="panel panel-default">
  50. <div class="panel-wrapper collapse in">
  51. <div class="panel-body">
  52. <h3 lang="en">Lidarr SOCKS API Connection</h3>
  53. <p>Using this feature allows you to access the Lidarr API without having to reverse proxy it. Just access it from: </p>
  54. <code>' . $this->getServerPath() . 'api/v2/socks/lidarr/</code>
  55. </div>
  56. </div>
  57. </div>'
  58. ),
  59. array(
  60. 'type' => 'switch',
  61. 'name' => 'lidarrSocksEnabled',
  62. 'label' => 'Enable',
  63. 'value' => $this->config['lidarrSocksEnabled']
  64. ),
  65. array(
  66. 'type' => 'select',
  67. 'name' => 'lidarrSocksAuth',
  68. 'label' => 'Minimum Authentication',
  69. 'value' => $this->config['lidarrSocksAuth'],
  70. 'options' => $this->groupOptions
  71. ),
  72. ),
  73. 'Misc Options' => array(
  74. array(
  75. 'type' => 'number',
  76. 'name' => 'calendarStart',
  77. 'label' => '# of Days Before',
  78. 'value' => $this->config['calendarStart'],
  79. 'placeholder' => ''
  80. ),
  81. array(
  82. 'type' => 'number',
  83. 'name' => 'calendarEnd',
  84. 'label' => '# of Days After',
  85. 'value' => $this->config['calendarEnd'],
  86. 'placeholder' => ''
  87. ),
  88. array(
  89. 'type' => 'select',
  90. 'name' => 'calendarFirstDay',
  91. 'label' => 'Start Day',
  92. 'value' => $this->config['calendarFirstDay'],
  93. 'options' => $this->daysOptions()
  94. ),
  95. array(
  96. 'type' => 'select',
  97. 'name' => 'calendarDefault',
  98. 'label' => 'Default View',
  99. 'value' => $this->config['calendarDefault'],
  100. 'options' => $this->calendarDefaultOptions()
  101. ),
  102. array(
  103. 'type' => 'select',
  104. 'name' => 'calendarTimeFormat',
  105. 'label' => 'Time Format',
  106. 'value' => $this->config['calendarTimeFormat'],
  107. 'options' => $this->timeFormatOptions()
  108. ),
  109. array(
  110. 'type' => 'select',
  111. 'name' => 'calendarLocale',
  112. 'label' => 'Locale',
  113. 'value' => $this->config['calendarLocale'],
  114. 'options' => $this->calendarLocaleOptions()
  115. ),
  116. array(
  117. 'type' => 'select',
  118. 'name' => 'calendarLimit',
  119. 'label' => 'Items Per Day',
  120. 'value' => $this->config['calendarLimit'],
  121. 'options' => $this->limitOptions()
  122. ),
  123. array(
  124. 'type' => 'select',
  125. 'name' => 'calendarRefresh',
  126. 'label' => 'Refresh Seconds',
  127. 'value' => $this->config['calendarRefresh'],
  128. 'options' => $this->timeOptions()
  129. ),
  130. ),
  131. 'Test Connection' => array(
  132. array(
  133. 'type' => 'blank',
  134. 'label' => 'Please Save before Testing'
  135. ),
  136. array(
  137. 'type' => 'button',
  138. 'label' => '',
  139. 'icon' => 'fa fa-flask',
  140. 'class' => 'pull-right',
  141. 'text' => 'Test Connection',
  142. 'attr' => 'onclick="testAPIConnection(\'lidarr\')"'
  143. ),
  144. )
  145. )
  146. );
  147. }
  148. public function testConnectionLidarr()
  149. {
  150. if (empty($this->config['lidarrURL'])) {
  151. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  152. return false;
  153. }
  154. if (empty($this->config['lidarrToken'])) {
  155. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  156. return false;
  157. }
  158. $failed = false;
  159. $errors = '';
  160. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  161. foreach ($list as $key => $value) {
  162. try {
  163. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
  164. $results = $downloader->getSystemStatus();
  165. $downloadList = json_decode($results, true);
  166. if (is_array($downloadList) || is_object($downloadList)) {
  167. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  168. if (!is_array($queue)) {
  169. $ip = $value['url'];
  170. $errors .= $ip . ': ' . $queue;
  171. $failed = true;
  172. }
  173. } else {
  174. $ip = $value['url'];
  175. $errors .= $ip . ': Response was not JSON';
  176. $failed = true;
  177. }
  178. } catch (Exception $e) {
  179. $failed = true;
  180. $ip = $value['url'];
  181. $errors .= $ip . ': ' . $e->getMessage();
  182. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  183. }
  184. }
  185. if ($failed) {
  186. $this->setAPIResponse('error', $errors, 500);
  187. return false;
  188. } else {
  189. $this->setAPIResponse('success', null, 200);
  190. return true;
  191. }
  192. }
  193. public function getLidarrQueue()
  194. {
  195. if (!$this->config['homepageLidarrEnabled']) {
  196. $this->setAPIResponse('error', 'Lidarr homepage item is not enabled', 409);
  197. return false;
  198. }
  199. if (!$this->config['homepageLidarrQueueEnabled']) {
  200. $this->setAPIResponse('error', 'Lidarr homepage module is not enabled', 409);
  201. return false;
  202. }
  203. if (!$this->qualifyRequest($this->config['homepageLidarrAuth'])) {
  204. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  205. return false;
  206. }
  207. if (!$this->qualifyRequest($this->config['homepageLidarrQueueAuth'])) {
  208. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  209. return false;
  210. }
  211. if (empty($this->config['lidarrURL'])) {
  212. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  213. return false;
  214. }
  215. if (empty($this->config['lidarrToken'])) {
  216. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  217. return false;
  218. }
  219. $queueItems = array();
  220. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  221. foreach ($list as $key => $value) {
  222. try {
  223. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
  224. $results = $downloader->getQueue();
  225. $downloadList = json_decode($results, true);
  226. if (is_array($downloadList) || is_object($downloadList)) {
  227. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  228. } else {
  229. $queue = '';
  230. }
  231. if (!empty($queue)) {
  232. $queueItems = array_merge($queueItems, $queue);
  233. }
  234. } catch (Exception $e) {
  235. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  236. }
  237. }
  238. $api['content']['queueItems'] = $queueItems;
  239. $api['content']['historyItems'] = false;
  240. $api['content'] = isset($api['content']) ? $api['content'] : false;
  241. $this->setAPIResponse('success', null, 200, $api);
  242. return $api;;
  243. }
  244. public function getLidarrCalendar($startDate = null, $endDate = null)
  245. {
  246. $startDate = ($startDate) ?? $_GET['start'];
  247. $endDate = ($endDate) ?? $_GET['end'];
  248. if (!$this->config['homepageLidarrEnabled']) {
  249. $this->setAPIResponse('error', 'Lidarr homepage item is not enabled', 409);
  250. return false;
  251. }
  252. if (!$this->qualifyRequest($this->config['homepageLidarrAuth'])) {
  253. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  254. return false;
  255. }
  256. if (empty($this->config['lidarrURL'])) {
  257. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  258. return false;
  259. }
  260. if (empty($this->config['lidarrToken'])) {
  261. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  262. return false;
  263. }
  264. $calendarItems = array();
  265. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  266. foreach ($list as $key => $value) {
  267. try {
  268. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
  269. $results = $downloader->getCalendar($startDate, $endDate);
  270. $result = json_decode($results, true);
  271. if (is_array($result) || is_object($result)) {
  272. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  273. } else {
  274. $calendar = '';
  275. }
  276. } catch (Exception $e) {
  277. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  278. }
  279. if (!empty($calendar)) {
  280. $calendarItems = array_merge($calendarItems, $calendar);
  281. }
  282. }
  283. $this->setAPIResponse('success', null, 200, $calendarItems);
  284. return $calendarItems;
  285. }
  286. public function formatLidarrCalendar($array, $number)
  287. {
  288. $array = json_decode($array, true);
  289. $gotCalendar = array();
  290. $i = 0;
  291. foreach ($array as $child) {
  292. $i++;
  293. $albumName = $child['title'];
  294. $artistName = $child['artist']['artistName'];
  295. $albumID = '';
  296. $releaseDate = $child['releaseDate'];
  297. $releaseDate = strtotime($releaseDate);
  298. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  299. if (new DateTime() < new DateTime($releaseDate)) {
  300. $unaired = true;
  301. }
  302. if (isset($child['statistics']['percentOfTracks'])) {
  303. if ($child['statistics']['percentOfTracks'] == '100.0') {
  304. $downloaded = '1';
  305. } else {
  306. $downloaded = '0';
  307. }
  308. } else {
  309. $downloaded = '0';
  310. }
  311. if ($downloaded == "0" && isset($unaired)) {
  312. $downloaded = "text-info";
  313. } elseif ($downloaded == "1") {
  314. $downloaded = "text-success";
  315. } else {
  316. $downloaded = "text-danger";
  317. }
  318. $fanart = "/plugins/images/cache/no-np.png";
  319. foreach ($child['artist']['images'] as $image) {
  320. if ($image['coverType'] == "fanart") {
  321. $fanart = str_replace('http://', 'https://', $image['url']);
  322. }
  323. }
  324. $details = array(
  325. "seasonCount" => '',
  326. "status" => '',
  327. "topTitle" => $albumName,
  328. "bottomTitle" => $artistName,
  329. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  330. "runtime" => '',
  331. "image" => $fanart,
  332. "ratings" => $child['artist']['ratings']['value'],
  333. "videoQuality" => "unknown",
  334. "audioChannels" => "unknown",
  335. "audioCodec" => "unknown",
  336. "videoCodec" => "unknown",
  337. "size" => "unknown",
  338. "genres" => $child['genres'],
  339. );
  340. array_push($gotCalendar, array(
  341. "id" => "Lidarr-" . $number . "-" . $i,
  342. "title" => $artistName,
  343. "start" => $child['releaseDate'],
  344. "className" => "inline-popups bg-calendar calendar-item musicID--",
  345. "imagetype" => "music " . $downloaded,
  346. "imagetypeFilter" => "music",
  347. "downloadFilter" => $downloaded,
  348. "bgColor" => str_replace('text', 'bg', $downloaded),
  349. "details" => $details,
  350. "data" => $child
  351. ));
  352. }
  353. if ($i != 0) {
  354. return $gotCalendar;
  355. }
  356. return false;
  357. }
  358. }