lidarr.php 11 KB

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