sonarr.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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('switch', 'sonarrUnmonitored', ['label' => 'Show Unmonitored']),
  55. ],
  56. 'Test Connection' => [
  57. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  58. $this->settingsOption('test', 'sonarr'),
  59. ]
  60. ]
  61. ];
  62. return array_merge($homepageInformation, $homepageSettings);
  63. }
  64. public function testConnectionSonarr()
  65. {
  66. if (empty($this->config['sonarrURL'])) {
  67. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  68. return false;
  69. }
  70. if (empty($this->config['sonarrToken'])) {
  71. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  72. return false;
  73. }
  74. $failed = false;
  75. $errors = '';
  76. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  77. foreach ($list as $key => $value) {
  78. try {
  79. $options = $this->requestOptions($value['url'], null, $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  80. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  81. $results = $downloader->getRootFolder();
  82. $downloadList = json_decode($results, true);
  83. if (is_array($downloadList) || is_object($downloadList)) {
  84. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  85. if (!is_array($queue)) {
  86. $ip = $value['url'];
  87. $errors .= $ip . ': ' . $queue;
  88. $failed = true;
  89. }
  90. } else {
  91. $ip = $value['url'];
  92. $errors .= $ip . ': Response was not JSON';
  93. $failed = true;
  94. }
  95. } catch (Exception $e) {
  96. $failed = true;
  97. $ip = $value['url'];
  98. $errors .= $ip . ': ' . $e->getMessage();
  99. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  100. }
  101. }
  102. if ($failed) {
  103. $this->setAPIResponse('error', $errors, 500);
  104. return false;
  105. } else {
  106. $this->setAPIResponse('success', null, 200);
  107. return true;
  108. }
  109. }
  110. public function sonarrHomepagePermissions($key = null)
  111. {
  112. $permissions = [
  113. 'calendar' => [
  114. 'enabled' => [
  115. 'homepageSonarrEnabled'
  116. ],
  117. 'auth' => [
  118. 'homepageSonarrAuth'
  119. ],
  120. 'not_empty' => [
  121. 'sonarrURL',
  122. 'sonarrToken'
  123. ]
  124. ],
  125. 'queue' => [
  126. 'enabled' => [
  127. 'homepageSonarrEnabled',
  128. 'homepageSonarrQueueEnabled'
  129. ],
  130. 'auth' => [
  131. 'homepageSonarrAuth',
  132. 'homepageSonarrQueueAuth'
  133. ],
  134. 'not_empty' => [
  135. 'sonarrURL',
  136. 'sonarrToken'
  137. ]
  138. ]
  139. ];
  140. return $this->homepageCheckKeyPermissions($key, $permissions);
  141. }
  142. public function homepageOrderSonarrQueue()
  143. {
  144. if ($this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'))) {
  145. $loadingBox = ($this->config['homepageSonarrQueueCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  146. $builder = ($this->config['homepageSonarrQueueCombine']) ? 'buildDownloaderCombined(\'sonarr\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sonarr"));';
  147. return '
  148. <div id="' . __FUNCTION__ . '">
  149. ' . $loadingBox . '
  150. <script>
  151. // homepageOrderSonarrQueue
  152. ' . $builder . '
  153. homepageDownloader("sonarr", "' . $this->config['homepageSonarrQueueRefresh'] . '");
  154. // End homepageOrderSonarrQueue
  155. </script>
  156. </div>
  157. ';
  158. }
  159. }
  160. public function getSonarrQueue()
  161. {
  162. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'), true)) {
  163. return false;
  164. }
  165. $queueItems = array();
  166. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  167. foreach ($list as $key => $value) {
  168. try {
  169. $options = $this->requestOptions($value['url'], $this->config['homepageSonarrQueueRefresh'], $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  170. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  171. $results = $downloader->getQueue();
  172. $downloadList = json_decode($results, true);
  173. if (is_array($downloadList) || is_object($downloadList)) {
  174. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  175. } else {
  176. $queue = '';
  177. }
  178. if (!empty($queue)) {
  179. $queueItems = array_merge($queueItems, $queue);
  180. }
  181. } catch (Exception $e) {
  182. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  183. }
  184. }
  185. $api['content']['queueItems'] = $queueItems;
  186. $api['content']['historyItems'] = false;
  187. $api['content'] = isset($api['content']) ? $api['content'] : false;
  188. $this->setAPIResponse('success', null, 200, $api);
  189. return $api;;
  190. }
  191. public function getSonarrCalendar($startDate = null, $endDate = null)
  192. {
  193. $startDate = ($startDate) ?? $_GET['start'] ?? date('Y-m-d', strtotime('-' . $this->config['calendarStart'] . ' days'));
  194. $endDate = ($endDate) ?? $_GET['end'] ?? date('Y-m-d', strtotime('+' . $this->config['calendarEnd'] . ' days'));
  195. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('calendar'), true)) {
  196. return false;
  197. }
  198. if ($this->demo) {
  199. return $this->demoData('sonarr/calendar.json');
  200. }
  201. $calendarItems = array();
  202. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  203. foreach ($list as $key => $value) {
  204. try {
  205. $options = $this->requestOptions($value['url'], null, $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  206. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  207. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  208. $result = json_decode($sonarr, true);
  209. if (is_array($result) || is_object($result)) {
  210. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  211. } else {
  212. $sonarrCalendar = '';
  213. }
  214. } catch (Exception $e) {
  215. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  216. }
  217. if (!empty($sonarrCalendar)) {
  218. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  219. }
  220. }
  221. $this->setAPIResponse('success', null, 200, $calendarItems);
  222. return $calendarItems;
  223. }
  224. public function formatSonarrCalendar($array, $number)
  225. {
  226. $array = json_decode($array, true);
  227. $gotCalendar = array();
  228. $i = 0;
  229. foreach ($array as $child) {
  230. $i++;
  231. $seriesName = $child['series']['title'];
  232. $seriesID = $child['series']['tvdbId'];
  233. $episodeID = $child['series']['tvdbId'];
  234. $monitored = $child['monitored'];
  235. if (!isset($episodeID)) {
  236. $episodeID = "";
  237. }
  238. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  239. $episodeAirDate = $child['airDateUtc'];
  240. $episodeAirDate = strtotime($episodeAirDate);
  241. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  242. if (new DateTime() < new DateTime($episodeAirDate)) {
  243. $unaired = true;
  244. }
  245. if ($child['episodeNumber'] == "1") {
  246. $episodePremier = "true";
  247. } else {
  248. $episodePremier = "false";
  249. $date = new DateTime($episodeAirDate);
  250. $date->add(new DateInterval("PT1S"));
  251. $date->format(DateTime::ATOM);
  252. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  253. }
  254. $downloaded = $child['hasFile'];
  255. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  256. $downloaded = "text-primary animated flash";
  257. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  258. $downloaded = "text-dark";
  259. } elseif ($downloaded == "0" && isset($unaired)) {
  260. $downloaded = "text-info";
  261. } elseif ($downloaded == "1") {
  262. $downloaded = "text-success";
  263. } else {
  264. $downloaded = "text-danger";
  265. }
  266. $fanart = "/plugins/images/cache/no-np.png";
  267. foreach ($child['series']['images'] as $image) {
  268. if ($image['coverType'] == "fanart") {
  269. $fanart = $image['url'];
  270. }
  271. }
  272. if ($fanart !== "/plugins/images/cache/no-np.png" || (strpos($fanart, '://') === false)) {
  273. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  274. $imageURL = $fanart;
  275. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  276. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  277. if (!file_exists($cacheFile)) {
  278. $this->cacheImage($imageURL, $seriesID);
  279. unset($imageURL);
  280. unset($cacheFile);
  281. }
  282. }
  283. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  284. $details = array(
  285. "seasonCount" => $child['series']['seasonCount'],
  286. "status" => $child['series']['status'],
  287. "topTitle" => $seriesName,
  288. "bottomTitle" => $bottomTitle,
  289. "overview" => isset($child['overview']) ? $child['overview'] : '',
  290. "runtime" => $child['series']['runtime'],
  291. "image" => $fanart,
  292. "ratings" => $child['series']['ratings']['value'],
  293. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  294. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  295. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  296. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  297. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  298. "genres" => $child['series']['genres'],
  299. );
  300. array_push($gotCalendar, array(
  301. "id" => "Sonarr-" . $number . "-" . $i,
  302. "title" => $seriesName,
  303. "start" => $child['airDateUtc'],
  304. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  305. "imagetype" => "tv " . $downloaded,
  306. "imagetypeFilter" => "tv",
  307. "downloadFilter" => $downloaded,
  308. "bgColor" => str_replace('text', 'bg', $downloaded),
  309. "details" => $details
  310. ));
  311. }
  312. if ($i != 0) {
  313. return $gotCalendar;
  314. }
  315. return false;
  316. }
  317. }