lidarr.php 11 KB

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