sonarr.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. trait SonarrHomepageItem
  3. {
  4. public function sonarrSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Sonarr',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sonarr.png',
  10. 'category' => 'PVR',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageSonarrEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageSonarrEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageSonarrAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageSonarrAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'sonarrURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['sonarrURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'sonarrToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['sonarrToken']
  41. )
  42. ),
  43. 'API SOCKS' => array(
  44. array(
  45. 'type' => 'html',
  46. 'override' => 12,
  47. 'label' => '',
  48. 'html' => '
  49. <div class="panel panel-default">
  50. <div class="panel-wrapper collapse in">
  51. <div class="panel-body">
  52. <h3 lang="en">Sonarr SOCKS API Connection</h3>
  53. <p>Using this feature allows you to access the Sonarr API without having to reverse proxy it. Just access it from: </p>
  54. <code>' . $this->getServerPath() . 'api/v2/socks/sonarr/</code>
  55. </div>
  56. </div>
  57. </div>'
  58. ),
  59. array(
  60. 'type' => 'switch',
  61. 'name' => 'sonarrSocksEnabled',
  62. 'label' => 'Enable',
  63. 'value' => $this->config['sonarrSocksEnabled']
  64. ),
  65. array(
  66. 'type' => 'select',
  67. 'name' => 'sonarrSocksAuth',
  68. 'label' => 'Minimum Authentication',
  69. 'value' => $this->config['sonarrSocksAuth'],
  70. 'options' => $this->groupOptions
  71. ),
  72. ),
  73. 'Queue' => array(
  74. array(
  75. 'type' => 'switch',
  76. 'name' => 'homepageSonarrQueueEnabled',
  77. 'label' => 'Enable',
  78. 'value' => $this->config['homepageSonarrQueueEnabled']
  79. ),
  80. array(
  81. 'type' => 'select',
  82. 'name' => 'homepageSonarrQueueAuth',
  83. 'label' => 'Minimum Authentication',
  84. 'value' => $this->config['homepageSonarrQueueAuth'],
  85. 'options' => $this->groupOptions
  86. ),
  87. array(
  88. 'type' => 'switch',
  89. 'name' => 'homepageSonarrQueueCombine',
  90. 'label' => 'Add to Combined Downloader',
  91. 'value' => $this->config['homepageSonarrQueueCombine']
  92. ),
  93. array(
  94. 'type' => 'select',
  95. 'name' => 'homepageSonarrQueueRefresh',
  96. 'label' => 'Refresh Seconds',
  97. 'value' => $this->config['homepageSonarrQueueRefresh'],
  98. 'options' => $this->timeOptions()
  99. ),
  100. ),
  101. 'Calendar' => array(
  102. array(
  103. 'type' => 'number',
  104. 'name' => 'calendarStart',
  105. 'label' => '# of Days Before',
  106. 'value' => $this->config['calendarStart'],
  107. 'placeholder' => ''
  108. ),
  109. array(
  110. 'type' => 'number',
  111. 'name' => 'calendarEnd',
  112. 'label' => '# of Days After',
  113. 'value' => $this->config['calendarEnd'],
  114. 'placeholder' => ''
  115. ),
  116. array(
  117. 'type' => 'select',
  118. 'name' => 'calendarFirstDay',
  119. 'label' => 'Start Day',
  120. 'value' => $this->config['calendarFirstDay'],
  121. 'options' => $this->daysOptions()
  122. ),
  123. array(
  124. 'type' => 'select',
  125. 'name' => 'calendarDefault',
  126. 'label' => 'Default View',
  127. 'value' => $this->config['calendarDefault'],
  128. 'options' => $this->calendarDefaultOptions()
  129. ),
  130. array(
  131. 'type' => 'select',
  132. 'name' => 'calendarTimeFormat',
  133. 'label' => 'Time Format',
  134. 'value' => $this->config['calendarTimeFormat'],
  135. 'options' => $this->timeFormatOptions()
  136. ),
  137. array(
  138. 'type' => 'select',
  139. 'name' => 'calendarLocale',
  140. 'label' => 'Locale',
  141. 'value' => $this->config['calendarLocale'],
  142. 'options' => $this->calendarLocaleOptions()
  143. ),
  144. array(
  145. 'type' => 'select',
  146. 'name' => 'calendarLimit',
  147. 'label' => 'Items Per Day',
  148. 'value' => $this->config['calendarLimit'],
  149. 'options' => $this->limitOptions()
  150. ),
  151. array(
  152. 'type' => 'select',
  153. 'name' => 'calendarRefresh',
  154. 'label' => 'Refresh Seconds',
  155. 'value' => $this->config['calendarRefresh'],
  156. 'options' => $this->timeOptions()
  157. ),
  158. array(
  159. 'type' => 'switch',
  160. 'name' => 'sonarrUnmonitored',
  161. 'label' => 'Show Unmonitored',
  162. 'value' => $this->config['sonarrUnmonitored']
  163. )
  164. ),
  165. 'Test Connection' => array(
  166. array(
  167. 'type' => 'blank',
  168. 'label' => 'Please Save before Testing'
  169. ),
  170. array(
  171. 'type' => 'button',
  172. 'label' => '',
  173. 'icon' => 'fa fa-flask',
  174. 'class' => 'pull-right',
  175. 'text' => 'Test Connection',
  176. 'attr' => 'onclick="testAPIConnection(\'sonarr\')"'
  177. ),
  178. )
  179. )
  180. );
  181. }
  182. public function testConnectionSonarr()
  183. {
  184. if (empty($this->config['sonarrURL'])) {
  185. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  186. return false;
  187. }
  188. if (empty($this->config['sonarrToken'])) {
  189. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  190. return false;
  191. }
  192. $failed = false;
  193. $errors = '';
  194. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  195. foreach ($list as $key => $value) {
  196. try {
  197. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
  198. $results = $downloader->getRootFolder();
  199. $downloadList = json_decode($results, true);
  200. if (is_array($downloadList) || is_object($downloadList)) {
  201. $queue = (array_key_exists('error', $downloadList)) ? $downloadList['error']['msg'] : $downloadList;
  202. if (!is_array($queue)) {
  203. $ip = $value['url'];
  204. $errors .= $ip . ': ' . $queue;
  205. $failed = true;
  206. }
  207. } else {
  208. $ip = $value['url'];
  209. $errors .= $ip . ': Response was not JSON';
  210. $failed = true;
  211. }
  212. } catch (Exception $e) {
  213. $failed = true;
  214. $ip = $value['url'];
  215. $errors .= $ip . ': ' . $e->getMessage();
  216. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  217. }
  218. }
  219. if ($failed) {
  220. $this->setAPIResponse('error', $errors, 500);
  221. return false;
  222. } else {
  223. $this->setAPIResponse('success', null, 200);
  224. return true;
  225. }
  226. }
  227. public function sonarrHomepagePermissions($key = null)
  228. {
  229. $permissions = [
  230. 'calendar' => [
  231. 'enabled' => [
  232. 'homepageSonarrEnabled'
  233. ],
  234. 'auth' => [
  235. 'homepageSonarrAuth'
  236. ],
  237. 'not_empty' => [
  238. 'sonarrURL',
  239. 'sonarrToken'
  240. ]
  241. ],
  242. 'queue' => [
  243. 'enabled' => [
  244. 'homepageSonarrEnabled',
  245. 'homepageSonarrQueueEnabled'
  246. ],
  247. 'auth' => [
  248. 'homepageSonarrAuth',
  249. 'homepageSonarrQueueAuth'
  250. ],
  251. 'not_empty' => [
  252. 'sonarrURL',
  253. 'sonarrToken'
  254. ]
  255. ]
  256. ];
  257. if (array_key_exists($key, $permissions)) {
  258. return $permissions[$key];
  259. } elseif ($key == 'all') {
  260. return $permissions;
  261. } else {
  262. return [];
  263. }
  264. }
  265. public function homepageOrderSonarrQueue()
  266. {
  267. if ($this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'))) {
  268. $loadingBox = ($this->config['homepageSonarrQueueCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  269. $builder = ($this->config['homepageSonarrQueueCombine']) ? 'buildDownloaderCombined(\'sonarr\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sonarr"));';
  270. return '
  271. <div id="' . __FUNCTION__ . '">
  272. ' . $loadingBox . '
  273. <script>
  274. // homepageOrderSonarrQueue
  275. ' . $builder . '
  276. homepageDownloader("sonarr", "' . $this->config['homepageSonarrQueueRefresh'] . '");
  277. // End homepageOrderSonarrQueue
  278. </script>
  279. </div>
  280. ';
  281. }
  282. }
  283. public function getSonarrQueue()
  284. {
  285. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('queue'), true)) {
  286. return false;
  287. }
  288. $queueItems = array();
  289. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  290. foreach ($list as $key => $value) {
  291. try {
  292. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
  293. $results = $downloader->getQueue();
  294. $downloadList = json_decode($results, true);
  295. if (is_array($downloadList) || is_object($downloadList)) {
  296. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  297. } else {
  298. $queue = '';
  299. }
  300. if (!empty($queue)) {
  301. $queueItems = array_merge($queueItems, $queue);
  302. }
  303. } catch (Exception $e) {
  304. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  305. }
  306. }
  307. $api['content']['queueItems'] = $queueItems;
  308. $api['content']['historyItems'] = false;
  309. $api['content'] = isset($api['content']) ? $api['content'] : false;
  310. $this->setAPIResponse('success', null, 200, $api);
  311. return $api;;
  312. }
  313. public function getSonarrCalendar($startDate = null, $endDate = null)
  314. {
  315. $startDate = ($startDate) ?? $_GET['start'];
  316. $endDate = ($endDate) ?? $_GET['end'];
  317. if (!$this->homepageItemPermissions($this->sonarrHomepagePermissions('calendar'), true)) {
  318. return false;
  319. }
  320. $calendarItems = array();
  321. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  322. foreach ($list as $key => $value) {
  323. try {
  324. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
  325. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  326. $result = json_decode($sonarr, true);
  327. if (is_array($result) || is_object($result)) {
  328. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  329. } else {
  330. $sonarrCalendar = '';
  331. }
  332. } catch (Exception $e) {
  333. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  334. }
  335. if (!empty($sonarrCalendar)) {
  336. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  337. }
  338. }
  339. $this->setAPIResponse('success', null, 200, $calendarItems);
  340. return $calendarItems;
  341. }
  342. public function formatSonarrCalendar($array, $number)
  343. {
  344. $array = json_decode($array, true);
  345. $gotCalendar = array();
  346. $i = 0;
  347. foreach ($array as $child) {
  348. $i++;
  349. $seriesName = $child['series']['title'];
  350. $seriesID = $child['series']['tvdbId'];
  351. $episodeID = $child['series']['tvdbId'];
  352. $monitored = $child['monitored'];
  353. if (!isset($episodeID)) {
  354. $episodeID = "";
  355. }
  356. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  357. $episodeAirDate = $child['airDateUtc'];
  358. $episodeAirDate = strtotime($episodeAirDate);
  359. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  360. if (new DateTime() < new DateTime($episodeAirDate)) {
  361. $unaired = true;
  362. }
  363. if ($child['episodeNumber'] == "1") {
  364. $episodePremier = "true";
  365. } else {
  366. $episodePremier = "false";
  367. $date = new DateTime($episodeAirDate);
  368. $date->add(new DateInterval("PT1S"));
  369. $date->format(DateTime::ATOM);
  370. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  371. }
  372. $downloaded = $child['hasFile'];
  373. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  374. $downloaded = "text-primary animated flash";
  375. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  376. $downloaded = "text-dark";
  377. } elseif ($downloaded == "0" && isset($unaired)) {
  378. $downloaded = "text-info";
  379. } elseif ($downloaded == "1") {
  380. $downloaded = "text-success";
  381. } else {
  382. $downloaded = "text-danger";
  383. }
  384. $fanart = "/plugins/images/cache/no-np.png";
  385. foreach ($child['series']['images'] as $image) {
  386. if ($image['coverType'] == "fanart") {
  387. $fanart = $image['url'];
  388. }
  389. }
  390. if ($fanart !== "/plugins/images/cache/no-np.png" || (strpos($fanart, '://') === false)) {
  391. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  392. $imageURL = $fanart;
  393. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  394. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  395. if (!file_exists($cacheFile)) {
  396. $this->cacheImage($imageURL, $seriesID);
  397. unset($imageURL);
  398. unset($cacheFile);
  399. }
  400. }
  401. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  402. $details = array(
  403. "seasonCount" => $child['series']['seasonCount'],
  404. "status" => $child['series']['status'],
  405. "topTitle" => $seriesName,
  406. "bottomTitle" => $bottomTitle,
  407. "overview" => isset($child['overview']) ? $child['overview'] : '',
  408. "runtime" => $child['series']['runtime'],
  409. "image" => $fanart,
  410. "ratings" => $child['series']['ratings']['value'],
  411. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  412. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  413. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  414. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  415. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  416. "genres" => $child['series']['genres'],
  417. );
  418. array_push($gotCalendar, array(
  419. "id" => "Sonarr-" . $number . "-" . $i,
  420. "title" => $seriesName,
  421. "start" => $child['airDateUtc'],
  422. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  423. "imagetype" => "tv " . $downloaded,
  424. "imagetypeFilter" => "tv",
  425. "downloadFilter" => $downloaded,
  426. "bgColor" => str_replace('text', 'bg', $downloaded),
  427. "details" => $details
  428. ));
  429. }
  430. if ($i != 0) {
  431. return $gotCalendar;
  432. }
  433. return false;
  434. }
  435. }