sonarr.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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' => 'https://docs.organizr.app/books/setup-features/page/sonarr',
  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'];
  194. $endDate = ($endDate) ?? $_GET['end'];
  195. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('calendar'), true)) {
  196. return false;
  197. }
  198. $calendarItems = array();
  199. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  200. foreach ($list as $key => $value) {
  201. try {
  202. $options = $this->requestOptions($value['url'], null, $this->config['sonarrDisableCertCheck'], $this->config['sonarrUseCustomCertificate']);
  203. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr', null, null, $options);
  204. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  205. $result = json_decode($sonarr, true);
  206. if (is_array($result) || is_object($result)) {
  207. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  208. } else {
  209. $sonarrCalendar = '';
  210. }
  211. } catch (Exception $e) {
  212. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  213. }
  214. if (!empty($sonarrCalendar)) {
  215. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  216. }
  217. }
  218. $this->setAPIResponse('success', null, 200, $calendarItems);
  219. return $calendarItems;
  220. }
  221. public function formatSonarrCalendar($array, $number)
  222. {
  223. $array = json_decode($array, true);
  224. $gotCalendar = array();
  225. $i = 0;
  226. foreach ($array as $child) {
  227. $i++;
  228. $seriesName = $child['series']['title'];
  229. $seriesID = $child['series']['tvdbId'];
  230. $episodeID = $child['series']['tvdbId'];
  231. $monitored = $child['monitored'];
  232. if (!isset($episodeID)) {
  233. $episodeID = "";
  234. }
  235. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  236. $episodeAirDate = $child['airDateUtc'];
  237. $episodeAirDate = strtotime($episodeAirDate);
  238. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  239. if (new DateTime() < new DateTime($episodeAirDate)) {
  240. $unaired = true;
  241. }
  242. if ($child['episodeNumber'] == "1") {
  243. $episodePremier = "true";
  244. } else {
  245. $episodePremier = "false";
  246. $date = new DateTime($episodeAirDate);
  247. $date->add(new DateInterval("PT1S"));
  248. $date->format(DateTime::ATOM);
  249. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  250. }
  251. $downloaded = $child['hasFile'];
  252. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  253. $downloaded = "text-primary animated flash";
  254. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  255. $downloaded = "text-dark";
  256. } elseif ($downloaded == "0" && isset($unaired)) {
  257. $downloaded = "text-info";
  258. } elseif ($downloaded == "1") {
  259. $downloaded = "text-success";
  260. } else {
  261. $downloaded = "text-danger";
  262. }
  263. $fanart = "/plugins/images/cache/no-np.png";
  264. foreach ($child['series']['images'] as $image) {
  265. if ($image['coverType'] == "fanart") {
  266. $fanart = $image['url'];
  267. }
  268. }
  269. if ($fanart !== "/plugins/images/cache/no-np.png" || (strpos($fanart, '://') === false)) {
  270. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  271. $imageURL = $fanart;
  272. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  273. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  274. if (!file_exists($cacheFile)) {
  275. $this->cacheImage($imageURL, $seriesID);
  276. unset($imageURL);
  277. unset($cacheFile);
  278. }
  279. }
  280. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  281. $details = array(
  282. "seasonCount" => $child['series']['seasonCount'],
  283. "status" => $child['series']['status'],
  284. "topTitle" => $seriesName,
  285. "bottomTitle" => $bottomTitle,
  286. "overview" => isset($child['overview']) ? $child['overview'] : '',
  287. "runtime" => $child['series']['runtime'],
  288. "image" => $fanart,
  289. "ratings" => $child['series']['ratings']['value'],
  290. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  291. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  292. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  293. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  294. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  295. "genres" => $child['series']['genres'],
  296. );
  297. array_push($gotCalendar, array(
  298. "id" => "Sonarr-" . $number . "-" . $i,
  299. "title" => $seriesName,
  300. "start" => $child['airDateUtc'],
  301. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  302. "imagetype" => "tv " . $downloaded,
  303. "imagetypeFilter" => "tv",
  304. "downloadFilter" => $downloaded,
  305. "bgColor" => str_replace('text', 'bg', $downloaded),
  306. "details" => $details
  307. ));
  308. }
  309. if ($i != 0) {
  310. return $gotCalendar;
  311. }
  312. return false;
  313. }
  314. }