sonarr.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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']);
  198. $results = $downloader->getSystemStatus();
  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 getSonarrQueue()
  228. {
  229. if (!$this->config['homepageSonarrEnabled']) {
  230. $this->setAPIResponse('error', 'Sonarr homepage item is not enabled', 409);
  231. return false;
  232. }
  233. if (!$this->config['homepageSonarrQueueEnabled']) {
  234. $this->setAPIResponse('error', 'Sonarr homepage module is not enabled', 409);
  235. return false;
  236. }
  237. if (!$this->qualifyRequest($this->config['homepageSonarrAuth'])) {
  238. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  239. return false;
  240. }
  241. if (!$this->qualifyRequest($this->config['homepageSonarrQueueAuth'])) {
  242. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  243. return false;
  244. }
  245. if (empty($this->config['sonarrURL'])) {
  246. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  247. return false;
  248. }
  249. if (empty($this->config['sonarrToken'])) {
  250. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  251. return false;
  252. }
  253. $queueItems = array();
  254. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  255. foreach ($list as $key => $value) {
  256. try {
  257. $downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  258. $results = $downloader->getQueue();
  259. $downloadList = json_decode($results, true);
  260. if (is_array($downloadList) || is_object($downloadList)) {
  261. $queue = (array_key_exists('error', $downloadList)) ? '' : $downloadList;
  262. } else {
  263. $queue = '';
  264. }
  265. if (!empty($queue)) {
  266. $queueItems = array_merge($queueItems, $queue);
  267. }
  268. } catch (Exception $e) {
  269. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  270. }
  271. }
  272. $api['content']['queueItems'] = $queueItems;
  273. $api['content']['historyItems'] = false;
  274. $api['content'] = isset($api['content']) ? $api['content'] : false;
  275. $this->setAPIResponse('success', null, 200, $api);
  276. return $api;;
  277. }
  278. public function getSonarrCalendar($startDate = null, $endDate = null)
  279. {
  280. $startDate = ($startDate) ?? $_GET['start'];
  281. $endDate = ($endDate) ?? $_GET['end'];
  282. if (!$this->config['homepageSonarrEnabled']) {
  283. $this->setAPIResponse('error', 'Sonarr homepage item is not enabled', 409);
  284. return false;
  285. }
  286. if (!$this->qualifyRequest($this->config['homepageSonarrAuth'])) {
  287. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  288. return false;
  289. }
  290. if (empty($this->config['sonarrURL'])) {
  291. $this->setAPIResponse('error', 'Sonarr URL is not defined', 422);
  292. return false;
  293. }
  294. if (empty($this->config['sonarrToken'])) {
  295. $this->setAPIResponse('error', 'Sonarr Token is not defined', 422);
  296. return false;
  297. }
  298. $calendarItems = array();
  299. $list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
  300. foreach ($list as $key => $value) {
  301. try {
  302. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  303. $sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
  304. $result = json_decode($sonarr, true);
  305. if (is_array($result) || is_object($result)) {
  306. $sonarrCalendar = (array_key_exists('error', $result)) ? '' : $this->formatSonarrCalendar($sonarr, $key);
  307. } else {
  308. $sonarrCalendar = '';
  309. }
  310. } catch (Exception $e) {
  311. $this->writeLog('error', 'Sonarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  312. }
  313. if (!empty($sonarrCalendar)) {
  314. $calendarItems = array_merge($calendarItems, $sonarrCalendar);
  315. }
  316. }
  317. $this->setAPIResponse('success', null, 200, $calendarItems);
  318. return $calendarItems;
  319. }
  320. public function formatSonarrCalendar($array, $number)
  321. {
  322. $array = json_decode($array, true);
  323. $gotCalendar = array();
  324. $i = 0;
  325. foreach ($array as $child) {
  326. $i++;
  327. $seriesName = $child['series']['title'];
  328. $seriesID = $child['series']['tvdbId'];
  329. $episodeID = $child['series']['tvdbId'];
  330. $monitored = $child['monitored'];
  331. if (!isset($episodeID)) {
  332. $episodeID = "";
  333. }
  334. //$episodeName = htmlentities($child['title'], ENT_QUOTES);
  335. $episodeAirDate = $child['airDateUtc'];
  336. $episodeAirDate = strtotime($episodeAirDate);
  337. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  338. if (new DateTime() < new DateTime($episodeAirDate)) {
  339. $unaired = true;
  340. }
  341. if ($child['episodeNumber'] == "1") {
  342. $episodePremier = "true";
  343. } else {
  344. $episodePremier = "false";
  345. $date = new DateTime($episodeAirDate);
  346. $date->add(new DateInterval("PT1S"));
  347. $date->format(DateTime::ATOM);
  348. $child['airDateUtc'] = gmdate('Y-m-d\TH:i:s\Z', strtotime($date->format(DateTime::ATOM)));
  349. }
  350. $downloaded = $child['hasFile'];
  351. if ($downloaded == "0" && isset($unaired) && $episodePremier == "true") {
  352. $downloaded = "text-primary animated flash";
  353. } elseif ($downloaded == "0" && isset($unaired) && $monitored == "0") {
  354. $downloaded = "text-dark";
  355. } elseif ($downloaded == "0" && isset($unaired)) {
  356. $downloaded = "text-info";
  357. } elseif ($downloaded == "1") {
  358. $downloaded = "text-success";
  359. } else {
  360. $downloaded = "text-danger";
  361. }
  362. $fanart = "/plugins/images/cache/no-np.png";
  363. foreach ($child['series']['images'] as $image) {
  364. if ($image['coverType'] == "fanart") {
  365. $fanart = $image['url'];
  366. }
  367. }
  368. if ($fanart !== "/plugins/images/cache/no-np.png" || (strpos($fanart, '://') === false)) {
  369. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  370. $imageURL = $fanart;
  371. $cacheFile = $cacheDirectory . $seriesID . '.jpg';
  372. $fanart = 'plugins/images/cache/' . $seriesID . '.jpg';
  373. if (!file_exists($cacheFile)) {
  374. $this->cacheImage($imageURL, $seriesID);
  375. unset($imageURL);
  376. unset($cacheFile);
  377. }
  378. }
  379. $bottomTitle = 'S' . sprintf("%02d", $child['seasonNumber']) . 'E' . sprintf("%02d", $child['episodeNumber']) . ' - ' . $child['title'];
  380. $details = array(
  381. "seasonCount" => $child['series']['seasonCount'],
  382. "status" => $child['series']['status'],
  383. "topTitle" => $seriesName,
  384. "bottomTitle" => $bottomTitle,
  385. "overview" => isset($child['overview']) ? $child['overview'] : '',
  386. "runtime" => $child['series']['runtime'],
  387. "image" => $fanart,
  388. "ratings" => $child['series']['ratings']['value'],
  389. "videoQuality" => $child["hasFile"] && isset($child['episodeFile']['quality']['quality']['name']) ? $child['episodeFile']['quality']['quality']['name'] : "unknown",
  390. "audioChannels" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioChannels'] : "unknown",
  391. "audioCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['audioCodec'] : "unknown",
  392. "videoCodec" => $child["hasFile"] && isset($child['episodeFile']['mediaInfo']) ? $child['episodeFile']['mediaInfo']['videoCodec'] : "unknown",
  393. "size" => $child["hasFile"] && isset($child['episodeFile']['size']) ? $child['episodeFile']['size'] : "unknown",
  394. "genres" => $child['series']['genres'],
  395. );
  396. array_push($gotCalendar, array(
  397. "id" => "Sonarr-" . $number . "-" . $i,
  398. "title" => $seriesName,
  399. "start" => $child['airDateUtc'],
  400. "className" => "inline-popups bg-calendar calendar-item tvID--" . $episodeID,
  401. "imagetype" => "tv " . $downloaded,
  402. "imagetypeFilter" => "tv",
  403. "downloadFilter" => $downloaded,
  404. "bgColor" => str_replace('text', 'bg', $downloaded),
  405. "details" => $details
  406. ));
  407. }
  408. if ($i != 0) {
  409. return $gotCalendar;
  410. }
  411. return false;
  412. }
  413. }