lidarr.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. trait LidarrHomepageItem
  3. {
  4. public function testConnectionLidarr()
  5. {
  6. if (empty($this->config['lidarrURL'])) {
  7. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['lidarrToken'])) {
  11. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  12. return false;
  13. }
  14. $failed = false;
  15. $errors = '';
  16. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  17. foreach ($list as $key => $value) {
  18. try {
  19. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
  20. $results = $downloader->getSystemStatus();
  21. $downloadList = json_decode($results, true);
  22. if (is_array($downloadList) || is_object($downloadList)) {
  23. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  24. if (!is_array($queue)) {
  25. $ip = $value['url'];
  26. $errors .= $ip . ': ' . $queue;
  27. $failed = true;
  28. }
  29. } else {
  30. $ip = $value['url'];
  31. $errors .= $ip . ': Response was not JSON';
  32. $failed = true;
  33. }
  34. } catch (Exception $e) {
  35. $failed = true;
  36. $ip = $value['url'];
  37. $errors .= $ip . ': ' . $e->getMessage();
  38. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  39. }
  40. }
  41. if ($failed) {
  42. $this->setAPIResponse('error', $errors, 500);
  43. return false;
  44. } else {
  45. $this->setAPIResponse('success', null, 200);
  46. return true;
  47. }
  48. }
  49. public function getLidarrQueue()
  50. {
  51. if (!$this->config['homepageRadarrEnabled']) {
  52. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  53. return false;
  54. }
  55. if (!$this->config['homepageRadarrQueueEnabled']) {
  56. $this->setAPIResponse('error', 'Radarr homepage module is not enabled', 409);
  57. return false;
  58. }
  59. if (!$this->qualifyRequest($this->config['homepageRadarrAuth'])) {
  60. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  61. return false;
  62. }
  63. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  64. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  65. return false;
  66. }
  67. if (empty($this->config['radarrURL'])) {
  68. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  69. return false;
  70. }
  71. if (empty($this->config['radarrToken'])) {
  72. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  73. return false;
  74. }
  75. $queueItems = array();
  76. $list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
  77. foreach ($list as $key => $value) {
  78. try {
  79. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  80. $results = $downloader->getQueue();
  81. $downloadList = json_decode($results, true);
  82. if (is_array($downloadList) || is_object($downloadList)) {
  83. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  84. } else {
  85. $queue = '';
  86. }
  87. if (!empty($queue)) {
  88. $queueItems = array_merge($queueItems, $queue);
  89. }
  90. } catch (Exception $e) {
  91. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  92. }
  93. }
  94. $api['content']['queueItems'] = $queueItems;
  95. $api['content']['historyItems'] = false;
  96. $api['content'] = isset($api['content']) ? $api['content'] : false;
  97. $this->setAPIResponse('success', null, 200, $api);
  98. return $api;;
  99. }
  100. public function getLidarrCalendar($startDate = null, $endDate = null)
  101. {
  102. $startDate = ($startDate) ?? $_GET['start'];
  103. $endDate = ($endDate) ?? $_GET['end'];
  104. if (!$this->config['homepageLidarrEnabled']) {
  105. $this->setAPIResponse('error', 'Radarr homepage item is not enabled', 409);
  106. return false;
  107. }
  108. if (!$this->qualifyRequest($this->config['homepageLidarrAuth'])) {
  109. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  110. return false;
  111. }
  112. if (!$this->qualifyRequest($this->config['homepageRadarrQueueAuth'])) {
  113. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  114. return false;
  115. }
  116. if (empty($this->config['lidarrURL'])) {
  117. $this->setAPIResponse('error', 'Lidarr URL is not defined', 422);
  118. return false;
  119. }
  120. if (empty($this->config['lidarrToken'])) {
  121. $this->setAPIResponse('error', 'Lidarr Token is not defined', 422);
  122. return false;
  123. }
  124. $calendarItems = array();
  125. $list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
  126. foreach ($list as $key => $value) {
  127. try {
  128. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
  129. $results = $downloader->getCalendar($startDate, $endDate, );
  130. $result = json_decode($results, true);
  131. if (is_array($result) || is_object($result)) {
  132. $calendar = (array_key_exists('error', $result)) ? '' : $this->formatLidarrCalendar($results, $key);
  133. } else {
  134. $calendar = '';
  135. }
  136. } catch (Exception $e) {
  137. $this->writeLog('error', 'Lidarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  138. }
  139. if (!empty($calendar)) {
  140. $calendarItems = array_merge($calendarItems, $calendar);
  141. }
  142. }
  143. $this->setAPIResponse('success', null, 200, $calendarItems);
  144. return $calendarItems;
  145. }
  146. public function formatLidarrCalendar($array, $number)
  147. {
  148. $array = json_decode($array, true);
  149. $gotCalendar = array();
  150. $i = 0;
  151. foreach ($array as $child) {
  152. $i++;
  153. $albumName = $child['title'];
  154. $artistName = $child['artist']['artistName'];
  155. $albumID = '';
  156. $releaseDate = $child['releaseDate'];
  157. $releaseDate = strtotime($releaseDate);
  158. $releaseDate = date("Y-m-d H:i:s", $releaseDate);
  159. if (new DateTime() < new DateTime($releaseDate)) {
  160. $unaired = true;
  161. }
  162. if (isset($child['statistics']['percentOfTracks'])) {
  163. if ($child['statistics']['percentOfTracks'] == '100.0') {
  164. $downloaded = '1';
  165. } else {
  166. $downloaded = '0';
  167. }
  168. } else {
  169. $downloaded = '0';
  170. }
  171. if ($downloaded == "0" && isset($unaired)) {
  172. $downloaded = "text-info";
  173. } elseif ($downloaded == "1") {
  174. $downloaded = "text-success";
  175. } else {
  176. $downloaded = "text-danger";
  177. }
  178. $fanart = "/plugins/images/cache/no-np.png";
  179. foreach ($child['artist']['images'] as $image) {
  180. if ($image['coverType'] == "fanart") {
  181. $fanart = str_replace('http://', 'https://', $image['url']);
  182. }
  183. }
  184. $details = array(
  185. "seasonCount" => '',
  186. "status" => '',
  187. "topTitle" => $albumName,
  188. "bottomTitle" => $artistName,
  189. "overview" => isset($child['artist']['overview']) ? $child['artist']['overview'] : '',
  190. "runtime" => '',
  191. "image" => $fanart,
  192. "ratings" => $child['artist']['ratings']['value'],
  193. "videoQuality" => "unknown",
  194. "audioChannels" => "unknown",
  195. "audioCodec" => "unknown",
  196. "videoCodec" => "unknown",
  197. "size" => "unknown",
  198. "genres" => $child['genres'],
  199. );
  200. array_push($gotCalendar, array(
  201. "id" => "Lidarr-" . $number . "-" . $i,
  202. "title" => $artistName,
  203. "start" => $child['releaseDate'],
  204. "className" => "inline-popups bg-calendar calendar-item musicID--",
  205. "imagetype" => "music " . $downloaded,
  206. "imagetypeFilter" => "music",
  207. "downloadFilter" => $downloaded,
  208. "bgColor" => str_replace('text', 'bg', $downloaded),
  209. "details" => $details,
  210. "data" => $child
  211. ));
  212. }
  213. if ($i != 0) {
  214. return $gotCalendar;
  215. }
  216. return false;
  217. }
  218. }