sonarr.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. trait SonarrHomepageItem
  3. {
  4. public function testConnectionSonarr()
  5. {
  6. if (empty($this->config['sonarrURL'])) {
  7. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['sonarrToken'])) {
  11. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  12. return false;
  13. }
  14. $failed = false;
  15. $errors = '';
  16. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  17. foreach ($list as $key => $value) {
  18. try {
  19. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  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', 'Sonarr 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 getSonarrQueue()
  50. {
  51. if (!$this->config['homepageSonarrEnabled']) {
  52. $this->setAPIResponse('error', 'Sonarr homepage item is not enabled', 409);
  53. return false;
  54. }
  55. if (!$this->config['homepageSonarrQueueEnabled']) {
  56. $this->setAPIResponse('error', 'Sonarr homepage module is not enabled', 409);
  57. return false;
  58. }
  59. if (!$this->qualifyRequest($this->config['homepageSonarrAuth'])) {
  60. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  61. return false;
  62. }
  63. if (!$this->qualifyRequest($this->config['homepageSonarrQueueAuth'])) {
  64. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  65. return false;
  66. }
  67. if (empty($this->config['sonarrURL'])) {
  68. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  69. return false;
  70. }
  71. if (empty($this->config['sonarrToken'])) {
  72. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  73. return false;
  74. }
  75. $queueItems = array();
  76. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  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', 'Sonarr 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 getSonarrCalendar($startDate = null, $endDate = null)
  101. {
  102. $startDate = ($startDate) ?? $_GET['start'];
  103. $endDate = ($endDate) ?? $_GET['end'];
  104. if (!$this->config['homepageSonarrEnabled']) {
  105. $this->setAPIResponse('error', 'Sonarr homepage item is not enabled', 409);
  106. return false;
  107. }
  108. if (!$this->config['homepageSonarrQueueEnabled']) {
  109. $this->setAPIResponse('error', 'Sonarr homepage module is not enabled', 409);
  110. return false;
  111. }
  112. if (!$this->qualifyRequest($this->config['homepageSonarrAuth'])) {
  113. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  114. return false;
  115. }
  116. if (!$this->qualifyRequest($this->config['homepageSonarrQueueAuth'])) {
  117. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  118. return false;
  119. }
  120. if (empty($this->config['sonarrURL'])) {
  121. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  122. return false;
  123. }
  124. if (empty($this->config['sonarrToken'])) {
  125. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  126. return false;
  127. }
  128. $calendarItems = array();
  129. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  130. foreach ($list as $key => $value) {
  131. try {
  132. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  133. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  134. $result = json_decode($sonarr, true);
  135. if (is_array($result) || is_object($result)) {
  136. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  137. } else {
  138. $sonarrCalendar = '';
  139. }
  140. } catch (Exception $e) {
  141. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  142. }
  143. if (!empty($sonarrCalendar)) {
  144. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  145. }
  146. }
  147. $this->setAPIResponse('success', null, 200, $calendarItems);
  148. return $calendarItems;
  149. }
  150. public function formatSonarrCalendar($array, $number)
  151. {
  152. $array = json_decode($array, true);
  153. $gotCalendar = array();
  154. $i = 0;
  155. foreach ($array as $child) {
  156. $i++;
  157. $seriesName = $child['series']['title'];
  158. $seriesID = $child['series']['tvdbId'];
  159. $episodeID = $child['series']['tvdbId'];
  160. $monitored = $child['monitored'];
  161. if (!isset($episodeID)) {
  162. $episodeID = "";
  163. }
  164. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  165. $episodeAirDate = $child['airDateUtc'];
  166. $episodeAirDate = strtotime($episodeAirDate);
  167. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  168. if (new DateTime() < new DateTime($episodeAirDate)) {
  169. $unaired = true;
  170. }
  171. if ($child['episodeNumber'] == "1") {
  172. $episodePremier = "true";
  173. } else {
  174. $episodePremier = "false";
  175. $date = new DateTime($episodeAirDate);
  176. $date->add(new DateInterval("PT1S"));
  177. $date->format(DateTime::ATOM);
  178. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  179. }
  180. $downloaded = $child['hasFile'];
  181. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  182. $downloaded = "text-primary animated flash";
  183. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  184. $downloaded = "text-dark";
  185. } elseif ($downloaded == "0" && isset($unaired)) {
  186. $downloaded = "text-info";
  187. } elseif ($downloaded == "1") {
  188. $downloaded = "text-success";
  189. } else {
  190. $downloaded = "text-danger";
  191. }
  192. $fanart = "/plugins/images/cache/no-np.png";
  193. foreach ($child['series']['images'] as $image) {
  194. if ($image['coverType'] == "fanart") {
  195. $fanart = $image['url'];
  196. }
  197. }
  198. if ($fanart !== "/plugins/images/cache/no-np.png" || (strpos($fanart, '://') === false)) {
  199. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  200. $imageURL = $fanart;
  201. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  202. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  203. if (!file_exists($cacheFile)) {
  204. $this->cacheImage($imageURL, $seriesID);
  205. unset($imageURL);
  206. unset($cacheFile);
  207. }
  208. }
  209. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  210. $details = array(
  211. "seasonCount" => $child['series']['seasonCount'],
  212. "status" => $child['series']['status'],
  213. "topTitle" => $seriesName,
  214. "bottomTitle" => $bottomTitle,
  215. "overview" => isset($child['overview']) ? $child['overview'] : '',
  216. "runtime" => $child['series']['runtime'],
  217. "image" => $fanart,
  218. "ratings" => $child['series']['ratings']['value'],
  219. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  220. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  221. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  222. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  223. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  224. "genres" => $child['series']['genres'],
  225. );
  226. array_push($gotCalendar, array(
  227. "id" => "Sonarr-" . $number . "-" . $i,
  228. "title" => $seriesName,
  229. "start" => $child['airDateUtc'],
  230. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  231. "imagetype" => "tv " . $downloaded,
  232. "imagetypeFilter" => "tv",
  233. "downloadFilter" => $downloaded,
  234. "bgColor" => str_replace('text', 'bg', $downloaded),
  235. "details" => $details
  236. ));
  237. }
  238. if ($i != 0) {
  239. return $gotCalendar;
  240. }
  241. return false;
  242. }
  243. }