lidarr.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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'], 'lidarr');
  164. $results = $downloader->getRootFolder();
  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 lidarrHomepagePermissions($key = null)
  194. {
  195. $permissions = [
  196. 'calendar' => [
  197. 'enabled' => [
  198. 'homepageLidarrEnabled'
  199. ],
  200. 'auth' => [
  201. 'homepageLidarrAuth'
  202. ],
  203. 'not_empty' => [
  204. 'lidarrURL',
  205. 'lidarrToken'
  206. ]
  207. ],
  208. 'queue' => [
  209. 'enabled' => [
  210. 'homepageLidarrEnabled',
  211. 'homepageLidarrQueueEnabled'
  212. ],
  213. 'auth' => [
  214. 'homepageLidarrAuth',
  215. 'homepageLidarrQueueAuth'
  216. ],
  217. 'not_empty' => [
  218. 'lidarrURL',
  219. 'lidarrToken'
  220. ]
  221. ]
  222. ];
  223. if (array_key_exists($key, $permissions)) {
  224. return $permissions[$key];
  225. } elseif ($key == 'all') {
  226. return $permissions;
  227. } else {
  228. return [];
  229. }
  230. }
  231. public function getLidarrQueue()
  232. {
  233. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('queue'), true)) {
  234. return false;
  235. }
  236. $queueItems = array();
  237. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  238. foreach ($list as $key => $value) {
  239. try {
  240. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr');
  241. $results = $downloader->getQueue();
  242. $downloadList = json_decode($results, true);
  243. if (is_array($downloadList) || is_object($downloadList)) {
  244. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  245. } else {
  246. $queue = '';
  247. }
  248. if (!empty($queue)) {
  249. $queueItems = array_merge($queueItems, $queue);
  250. }
  251. } catch (Exception $e) {
  252. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  253. }
  254. }
  255. $api['content']['queueItems'] = $queueItems;
  256. $api['content']['historyItems'] = false;
  257. $api['content'] = isset($api['content']) ? $api['content'] : false;
  258. $this->setAPIResponse('success', null, 200, $api);
  259. return $api;;
  260. }
  261. public function getLidarrCalendar($startDate = null, $endDate = null)
  262. {
  263. $startDate = ($startDate) ?? $_GET['start'];
  264. $endDate = ($endDate) ?? $_GET['end'];
  265. if (!$this->homepageItemPermissions($this->lidarrHomepagePermissions('calendar'), true)) {
  266. return false;
  267. }
  268. $calendarItems = array();
  269. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  270. foreach ($list as $key => $value) {
  271. try {
  272. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr');
  273. $results = $downloader->getCalendar($startDate, $endDate);
  274. $result = json_decode($results, true);
  275. if (is_array($result) || is_object($result)) {
  276. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  277. } else {
  278. $calendar = '';
  279. }
  280. } catch (Exception $e) {
  281. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  282. }
  283. if (!empty($calendar)) {
  284. $calendarItems = array_merge($calendarItems, $calendar);
  285. }
  286. }
  287. $this->setAPIResponse('success', null, 200, $calendarItems);
  288. return $calendarItems;
  289. }
  290. public function formatLidarrCalendar($array, $number)
  291. {
  292. $array = json_decode($array, true);
  293. $gotCalendar = array();
  294. $i = 0;
  295. foreach ($array as $child) {
  296. $i++;
  297. $albumName = $child['title'];
  298. $artistName = $child['artist']['artistName'];
  299. $albumID = '';
  300. $releaseDate = $child['releaseDate'];
  301. $releaseDate = strtotime($releaseDate);
  302. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  303. if (new DateTime() < new DateTime($releaseDate)) {
  304. $unaired = true;
  305. }
  306. if (isset($child['statistics']['percentOfTracks'])) {
  307. if ($child['statistics']['percentOfTracks'] == '100.0') {
  308. $downloaded = '1';
  309. } else {
  310. $downloaded = '0';
  311. }
  312. } else {
  313. $downloaded = '0';
  314. }
  315. if ($downloaded == "0" && isset($unaired)) {
  316. $downloaded = "text-info";
  317. } elseif ($downloaded == "1") {
  318. $downloaded = "text-success";
  319. } else {
  320. $downloaded = "text-danger";
  321. }
  322. $fanart = "/plugins/images/cache/no-np.png";
  323. foreach ($child['artist']['images'] as $image) {
  324. if ($image['coverType'] == "fanart") {
  325. $fanart = str_replace('http://', 'https://', $image['url']);
  326. }
  327. }
  328. $details = array(
  329. "seasonCount" => '',
  330. "status" => '',
  331. "topTitle" => $albumName,
  332. "bottomTitle" => $artistName,
  333. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  334. "runtime" => '',
  335. "image" => $fanart,
  336. "ratings" => $child['artist']['ratings']['value'],
  337. "videoQuality" => "unknown",
  338. "audioChannels" => "unknown",
  339. "audioCodec" => "unknown",
  340. "videoCodec" => "unknown",
  341. "size" => "unknown",
  342. "genres" => $child['genres'],
  343. );
  344. array_push($gotCalendar, array(
  345. "id" => "Lidarr-" . $number . "-" . $i,
  346. "title" => $artistName,
  347. "start" => $child['releaseDate'],
  348. "className" => "inline-popups bg-calendar calendar-item musicID--",
  349. "imagetype" => "music " . $downloaded,
  350. "imagetypeFilter" => "music",
  351. "downloadFilter" => $downloaded,
  352. "bgColor" => str_replace('text', 'bg', $downloaded),
  353. "details" => $details,
  354. "data" => $child
  355. ));
  356. }
  357. if ($i != 0) {
  358. return $gotCalendar;
  359. }
  360. return false;
  361. }
  362. }