lidarr.php 11 KB

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