sonarr.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. trait SonarrHomepageItem
  3. {
  4. public function sonarrSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Sonarr',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sonarr.png',
  10. 'category' => 'PVR',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'docs' => $this->docs('features/homepage/sonarr-homepage-item'),
  18. 'debug' => true,
  19. 'settings' => [
  20. 'About' => [
  21. $this->settingsOption('about', 'Sonarr', ['about' => 'This item allows access to Sonarr\'s calendar data and aggregates it to Organizr\'s calendar. Along with that you also have the Downloader function that allow access to Sonarr\'s queue. The last item that is included is the API SOCKS function which acts as a middleman between API\'s which is useful if you are not port forwarding or reverse proxying Sonarr.']),
  22. ],
  23. 'Enable' => [
  24. $this->settingsOption('enable', 'homepageSonarrEnabled'),
  25. $this->settingsOption('auth', 'homepageSonarrAuth'),
  26. ],
  27. 'Connection' => [
  28. $this->settingsOption('multiple-url', 'sonarrURL'),
  29. $this->settingsOption('multiple-token', 'sonarrToken'),
  30. $this->settingsOption('disable-cert-check', 'sonarrDisableCertCheck'),
  31. $this->settingsOption('use-custom-certificate', 'sonarrUseCustomCertificate'),
  32. ],
  33. 'API SOCKS' => [
  34. $this->settingsOption('socks', 'sonarr'),
  35. $this->settingsOption('blank'),
  36. $this->settingsOption('enable', 'sonarrSocksEnabled'),
  37. $this->settingsOption('auth', 'sonarrSocksAuth'),
  38. ],
  39. 'Queue' => [
  40. $this->settingsOption('enable', 'homepageSonarrQueueEnabled'),
  41. $this->settingsOption('auth', 'homepageSonarrQueueAuth'),
  42. $this->settingsOption('combine', 'homepageSonarrQueueCombine'),
  43. $this->settingsOption('refresh', 'homepageSonarrQueueRefresh'),
  44. ],
  45. 'Calendar' => [
  46. $this->settingsOption('calendar-start', 'calendarStart'),
  47. $this->settingsOption('calendar-end', 'calendarEnd'),
  48. $this->settingsOption('calendar-starting-day', 'calendarFirstDay'),
  49. $this->settingsOption('calendar-default-view', 'calendarDefault'),
  50. $this->settingsOption('calendar-time-format', 'calendarTimeFormat'),
  51. $this->settingsOption('calendar-locale', 'calendarLocale'),
  52. $this->settingsOption('calendar-limit', 'calendarLimit'),
  53. $this->settingsOption('refresh', 'calendarRefresh'),
  54. $this->settingsOption('calendar-link-url', 'sonarrCalendarLink'),
  55. $this->settingsOption('switch', 'sonarrUnmonitored', ['label' => 'Show Unmonitored']),
  56. ],
  57. 'Test Connection' => [
  58. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  59. $this->settingsOption('test', 'sonarr'),
  60. ]
  61. ]
  62. ];
  63. return array_merge($homepageInformation, $homepageSettings);
  64. }
  65. public function testConnectionSonarr()
  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. $failed = false;
  76. $errors = '';
  77. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  78. foreach ($list as $key => $value) {
  79. try {
  80. $options = $this->requestOptions($value['url'], null, $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  81. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  82. $results = $downloader->getRootFolder();
  83. $downloadList = json_decode($results, true);
  84. if (is_array($downloadList) || is_object($downloadList)) {
  85. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  86. if (!is_array($queue)) {
  87. $ip = $value['url'];
  88. $errors .= $ip . ': ' . $queue;
  89. $failed = true;
  90. }
  91. } else {
  92. $ip = $value['url'];
  93. $errors .= $ip . ': Response was not JSON';
  94. $failed = true;
  95. }
  96. } catch (Exception $e) {
  97. $failed = true;
  98. $ip = $value['url'];
  99. $errors .= $ip . ': ' . $e->getMessage();
  100. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  101. }
  102. }
  103. if ($failed) {
  104. $this->setAPIResponse('error', $errors, 500);
  105. return false;
  106. } else {
  107. $this->setAPIResponse('success', null, 200);
  108. return true;
  109. }
  110. }
  111. public function sonarrHomepagePermissions($key = null)
  112. {
  113. $permissions = [
  114. 'calendar' => [
  115. 'enabled' => [
  116. 'homepageSonarrEnabled'
  117. ],
  118. 'auth' => [
  119. 'homepageSonarrAuth'
  120. ],
  121. 'not_empty' => [
  122. 'sonarrURL',
  123. 'sonarrToken'
  124. ]
  125. ],
  126. 'queue' => [
  127. 'enabled' => [
  128. 'homepageSonarrEnabled',
  129. 'homepageSonarrQueueEnabled'
  130. ],
  131. 'auth' => [
  132. 'homepageSonarrAuth',
  133. 'homepageSonarrQueueAuth'
  134. ],
  135. 'not_empty' => [
  136. 'sonarrURL',
  137. 'sonarrToken'
  138. ]
  139. ]
  140. ];
  141. return $this->homepageCheckKeyPermissions($key, $permissions);
  142. }
  143. public function homepageOrderSonarrQueue()
  144. {
  145. if ($this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'))) {
  146. $loadingBox = ($this->config['homepageSonarrQueueCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  147. $builder = ($this->config['homepageSonarrQueueCombine']) ? 'buildDownloaderCombined(\'sonarr\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sonarr"));';
  148. return '
  149. <div id="' . __FUNCTION__ . '">
  150. ' . $loadingBox . '
  151. <script>
  152. // homepageOrderSonarrQueue
  153. ' . $builder . '
  154. homepageDownloader("sonarr", "' . $this->config['homepageSonarrQueueRefresh'] . '");
  155. // End homepageOrderSonarrQueue
  156. </script>
  157. </div>
  158. ';
  159. }
  160. }
  161. public function getSonarrQueue()
  162. {
  163. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'), true)) {
  164. return false;
  165. }
  166. $queueItems = array();
  167. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  168. foreach ($list as $key => $value) {
  169. try {
  170. $options = $this->requestOptions($value['url'], $this->config['homepageSonarrQueueRefresh'], $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  171. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  172. $results = $downloader->getQueue();
  173. $downloadList = json_decode($results, true);
  174. if (is_array($downloadList) || is_object($downloadList)) {
  175. $queue = (array_key_exists('error', $downloadList)) ? [] : $downloadList;
  176. $queue = $queue['records'] ?? $queue;
  177. } else {
  178. $queue = [];
  179. }
  180. if (!empty($queue)) {
  181. $queueItems = array_merge($queueItems, $queue);
  182. }
  183. } catch (Exception $e) {
  184. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  185. }
  186. }
  187. $api['content']['queueItems'] = $queueItems;
  188. $api['content']['historyItems'] = false;
  189. $api['content'] = $api['content'] ?? false;
  190. $this->setAPIResponse('success', null, 200, $api);
  191. return $api;
  192. }
  193. public function getSonarrCalendar($startDate = null, $endDate = null)
  194. {
  195. $startDate = ($startDate) ?? $_GET['start'] ?? date('Y-m-d', strtotime('-' . $this->config['calendarStart'] . ' days'));
  196. $endDate = ($endDate) ?? $_GET['end'] ?? date('Y-m-d', strtotime('+' . $this->config['calendarEnd'] . ' days'));
  197. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('calendar'), true)) {
  198. return false;
  199. }
  200. if ($this->demo) {
  201. return $this->demoData('sonarr/calendar.json');
  202. }
  203. $calendarItems = array();
  204. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  205. foreach ($list as $key => $value) {
  206. try {
  207. $options = $this->requestOptions($value['url'], null, $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  208. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  209. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  210. $result = json_decode($sonarr, true);
  211. if (is_array($result) || is_object($result)) {
  212. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  213. } else {
  214. $sonarrCalendar = '';
  215. }
  216. } catch (Exception $e) {
  217. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  218. }
  219. if (!empty($sonarrCalendar)) {
  220. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  221. }
  222. }
  223. $this->setAPIResponse('success', null, 200, $calendarItems);
  224. return $calendarItems;
  225. }
  226. public function formatSonarrCalendar($array, $number)
  227. {
  228. $array = json_decode($array, true);
  229. $gotCalendar = array();
  230. $i = 0;
  231. foreach ($array as $child) {
  232. $i++;
  233. $seriesName = $child['series']['title'];
  234. $seriesID = $child['series']['tvdbId'];
  235. $episodeID = $child['series']['tvdbId'];
  236. $monitored = $child['monitored'];
  237. if (!isset($episodeID)) {
  238. $episodeID = "";
  239. }
  240. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  241. $episodeAirDate = $child['airDateUtc'];
  242. $episodeAirDate = strtotime($episodeAirDate);
  243. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  244. if (new DateTime() < new DateTime($episodeAirDate)) {
  245. $unaired = true;
  246. }
  247. if ($child['episodeNumber'] == "1") {
  248. $episodePremier = "true";
  249. } else {
  250. $episodePremier = "false";
  251. $date = new DateTime($episodeAirDate);
  252. $date->add(new DateInterval("PT1S"));
  253. $date->format(DateTime::ATOM);
  254. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  255. }
  256. $downloaded = $child['hasFile'];
  257. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  258. $downloaded = "text-primary animated flash";
  259. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  260. $downloaded = "text-dark";
  261. } elseif ($downloaded == "0" && isset($unaired)) {
  262. $downloaded = "text-info";
  263. } elseif ($downloaded == "1") {
  264. $downloaded = "text-success";
  265. } else {
  266. $downloaded = "text-danger";
  267. }
  268. $fanart = "/plugins/images/homepage/no-np.png";
  269. foreach ($child['series']['images'] as $image) {
  270. if ($image['coverType'] == "fanart") {
  271. $fanart = $image['url'];
  272. }
  273. }
  274. if ($fanart !== "/plugins/images/homepage/no-np.png" || (strpos($fanart, '://') === false)) {
  275. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  276. $imageURL = $fanart;
  277. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  278. $fanart = 'data/cache/' . $seriesID . '.jpg';
  279. if (!file_exists($cacheFile)) {
  280. $this->cacheImage($imageURL, $seriesID);
  281. unset($imageURL);
  282. unset($cacheFile);
  283. }
  284. }
  285. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  286. $href = $this->config['sonarrCalendarLink'] ?? '';
  287. if (empty($href) && !empty($this->config['sonarrURL'])) {
  288. $href_arr = explode(',', $this->config['sonarrURL']);
  289. $href = reset($href_arr);
  290. }
  291. if (!empty($href)) {
  292. $href = $href . '/series/' . preg_replace('/[^A-Za-z0-9. -]/', '', preg_replace('/[[:space:]]+/', '-', $seriesName));
  293. $href = str_replace("//series/", "/series/", $href);
  294. }
  295. $details = array(
  296. "seasonCount" => $child['series']['seasonCount'] ?? isset($child['series']['seasons']) ? count($child['series']['seasons']) : 0,
  297. "status" => $child['series']['status'],
  298. "topTitle" => $seriesName,
  299. "bottomTitle" => $bottomTitle,
  300. "overview" => $child['overview'] ?? '',
  301. "runtime" => $child['series']['runtime'],
  302. "image" => $fanart,
  303. "ratings" => $child['series']['ratings']['value'],
  304. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  305. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  306. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  307. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  308. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  309. "genres" => $child['series']['genres'],
  310. "href" => strtolower($href),
  311. "icon" => "/plugins/images/tabs/sonarr.png",
  312. );
  313. array_push($gotCalendar, array(
  314. "id" => "Sonarr-" . $number . "-" . $i,
  315. "title" => $seriesName,
  316. "start" => $child['airDateUtc'],
  317. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  318. "imagetype" => "tv " . $downloaded,
  319. "imagetypeFilter" => "tv",
  320. "downloadFilter" => $downloaded,
  321. "bgColor" => str_replace('text', 'bg', $downloaded),
  322. "details" => $details
  323. ));
  324. }
  325. if ($i != 0) {
  326. return $gotCalendar;
  327. }
  328. return false;
  329. }
  330. }