Sonarr.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. namespace Kryptonit3\Sonarr;
  3. use GuzzleHttp\Client;
  4. class Sonarr
  5. {
  6. protected $url;
  7. protected $apiKey;
  8. protected $httpAuthUsername;
  9. protected $httpAuthPassword;
  10. public function __construct($url, $apiKey, $lidarr = false, $httpAuthUsername = null, $httpAuthPassword = null)
  11. {
  12. $this->url = rtrim($url, '/\\'); // Example: http://127.0.0.1:8989 (no trailing forward-backward slashes)
  13. $this->apiKey = $apiKey;
  14. $this->lidarr = $lidarr;
  15. $this->httpAuthUsername = $httpAuthUsername;
  16. $this->httpAuthPassword = $httpAuthPassword;
  17. }
  18. /**
  19. * Gets upcoming episodes, if start/end are not supplied episodes airing today and tomorrow will be returned
  20. * When supplying start and/or end date you must supply date in format yyyy-mm-dd
  21. * Example: $sonarr->getCalendar('2015-01-25', '2016-01-15');
  22. * 'start' and 'end' not required. You may supply, one or both.
  23. *
  24. * @param string|null $start
  25. * @param string|null $end
  26. * @return array|object|string
  27. */
  28. public function getCalendar($start = null, $end = null, $sonarrUnmonitored = 'false')
  29. {
  30. $uriData = [];
  31. if ( $start ) {
  32. if ( $this->validateDate($start) ) {
  33. $uriData['start'] = $start;
  34. } else {
  35. return json_encode(array(
  36. 'error' => array(
  37. 'msg' => 'Start date string was not recognized as a valid DateTime. Format must be yyyy-mm-dd.',
  38. 'code' => 400,
  39. ),
  40. ));
  41. exit();
  42. }
  43. }
  44. if ( $end ) {
  45. if ( $this->validateDate($end) ) {
  46. $uriData['end'] = $end;
  47. } else {
  48. return json_encode(array(
  49. 'error' => array(
  50. 'msg' => 'End date string was not recognized as a valid DateTime. Format must be yyyy-mm-dd.',
  51. 'code' => 400,
  52. ),
  53. ));
  54. exit();
  55. }
  56. }
  57. if ( $sonarrUnmonitored == 'true' ) {
  58. $uriData['unmonitored'] = 'true';
  59. }
  60. if ( $this->lidarr == true ) {
  61. $uriData['includeArtist'] = 'true';
  62. }
  63. $response = [
  64. 'uri' => 'calendar',
  65. 'type' => 'get',
  66. 'data' => $uriData
  67. ];
  68. return $this->processRequest($response);
  69. }
  70. /**
  71. * Queries the status of a previously started command, or all currently started commands.
  72. *
  73. * @param null $id Unique ID of the command
  74. * @return array|object|string
  75. */
  76. public function getCommand($id = null)
  77. {
  78. $uri = ($id) ? 'command/' . $id : 'command';
  79. $response = [
  80. 'uri' => $uri,
  81. 'type' => 'get',
  82. 'data' => []
  83. ];
  84. return $this->processRequest($response);
  85. }
  86. /**
  87. * Publish a new command for Sonarr to run.
  88. * These commands are executed asynchronously; use GET to retrieve the current status.
  89. *
  90. * Commands and their parameters can be found here:
  91. * https://github.com/Sonarr/Sonarr/wiki/Command#commands
  92. *
  93. * @param $name
  94. * @param array|null $params
  95. * @return string
  96. */
  97. public function postCommand($name, array $params = null)
  98. {
  99. $uri = 'command';
  100. $uriData = [
  101. 'name' => $name
  102. ];
  103. if (is_array($params)) {
  104. foreach($params as $key=>$value) {
  105. $uriData[$key] = $value;
  106. }
  107. }
  108. $response = [
  109. 'uri' => $uri,
  110. 'type' => 'post',
  111. 'data' => $uriData
  112. ];
  113. return $this->processRequest($response);
  114. }
  115. /**
  116. * Gets Diskspace
  117. *
  118. * @return array|object|string
  119. */
  120. public function getDiskspace()
  121. {
  122. $uri = 'diskspace';
  123. $response = [
  124. 'uri' => $uri,
  125. 'type' => 'get',
  126. 'data' => []
  127. ];
  128. return $this->processRequest($response);
  129. }
  130. /**
  131. * Returns all episodes for the given series
  132. *
  133. * @param $seriesId
  134. * @return array|object|string
  135. */
  136. public function getEpisodes($seriesId)
  137. {
  138. $uri = 'episode';
  139. $response = [
  140. 'uri' => $uri,
  141. 'type' => 'get',
  142. 'data' => [
  143. 'SeriesId' => $seriesId
  144. ]
  145. ];
  146. return $this->processRequest($response);
  147. }
  148. /**
  149. * Returns the episode with the matching id
  150. *
  151. * @param $id
  152. * @return string
  153. */
  154. public function getEpisode($id)
  155. {
  156. $uri = 'episode';
  157. $response = [
  158. 'uri' => $uri . '/' . $id,
  159. 'type' => 'get',
  160. 'data' => []
  161. ];
  162. return $this->processRequest($response);
  163. }
  164. /**
  165. * Update the given episodes, currently only monitored is changed, all other modifications are ignored.
  166. *
  167. * Required: All parameters; You should perform a getEpisode(id)
  168. * and submit the full body with the changes, as other values may be editable in the future.
  169. *
  170. * @param array $data
  171. * @return string
  172. */
  173. public function updateEpisode(array $data)
  174. {
  175. $uri = 'episode';
  176. $response = [
  177. 'uri' => $uri,
  178. 'type' => 'put',
  179. 'data' => $data
  180. ];
  181. return $this->processRequest($response);
  182. }
  183. /**
  184. * Returns all episode files for the given series
  185. *
  186. * @param $seriesId
  187. * @return array|object|string
  188. */
  189. public function getEpisodeFiles($seriesId)
  190. {
  191. $uri = 'episodefile';
  192. $response = [
  193. 'uri' => $uri,
  194. 'type' => 'get',
  195. 'data' => [
  196. 'SeriesId' => $seriesId
  197. ]
  198. ];
  199. return $this->processRequest($response);
  200. }
  201. /**
  202. * Returns the episode file with the matching id
  203. *
  204. * @param $id
  205. * @return string
  206. */
  207. public function getEpisodeFile($id)
  208. {
  209. $uri = 'episodefile';
  210. $response = [
  211. 'uri' => $uri . '/' . $id,
  212. 'type' => 'get',
  213. 'data' => []
  214. ];
  215. return $this->processRequest($response);
  216. }
  217. /**
  218. * Delete the given episode file
  219. *
  220. * @param $id
  221. * @return string
  222. */
  223. public function deleteEpisodeFile($id)
  224. {
  225. $uri = 'episodefile';
  226. $response = [
  227. 'uri' => $uri . '/' . $id,
  228. 'type' => 'delete',
  229. 'data' => []
  230. ];
  231. return $this->processRequest($response);
  232. }
  233. /**
  234. * Gets history (grabs/failures/completed).
  235. *
  236. * @param int $page Page Number
  237. * @param int $pageSize Results per Page
  238. * @param string $sortKey 'series.title' or 'date'
  239. * @param string $sortDir 'asc' or 'desc'
  240. * @return array|object|string
  241. */
  242. public function getHistory($page = 1, $pageSize = 10, $sortKey = 'series.title', $sortDir = 'asc')
  243. {
  244. $uri = 'history';
  245. $response = [
  246. 'uri' => $uri,
  247. 'type' => 'get',
  248. 'data' => [
  249. 'page' => $page,
  250. 'pageSize' => $pageSize,
  251. 'sortKey' => $sortKey,
  252. 'sortDir' => $sortDir
  253. ]
  254. ];
  255. return $this->processRequest($response);
  256. }
  257. /**
  258. * Gets missing episode (episodes without files).
  259. *
  260. * @param int $page Page Number
  261. * @param int $pageSize Results per Page
  262. * @param string $sortKey 'series.title' or 'airDateUtc'
  263. * @param string $sortDir 'asc' or 'desc'
  264. * @return array|object|string
  265. */
  266. public function getWantedMissing($page = 1, $pageSize = 10, $sortKey = 'series.title', $sortDir = 'asc')
  267. {
  268. $uri = 'wanted/missing';
  269. $response = [
  270. 'uri' => $uri,
  271. 'type' => 'get',
  272. 'data' => [
  273. 'page' => $page,
  274. 'pageSize' => $pageSize,
  275. 'sortKey' => $sortKey,
  276. 'sortDir' => $sortDir
  277. ]
  278. ];
  279. return $this->processRequest($response);
  280. }
  281. /**
  282. * Displays currently downloading info
  283. *
  284. * @return array|object|string
  285. */
  286. public function getQueue()
  287. {
  288. $uri = 'queue';
  289. $response = [
  290. 'uri' => $uri,
  291. 'type' => 'get',
  292. 'data' => []
  293. ];
  294. return $this->processRequest($response);
  295. }
  296. /**
  297. * Gets all quality profiles
  298. *
  299. * @return array|object|string
  300. */
  301. public function getProfiles()
  302. {
  303. $uri = 'profile';
  304. $response = [
  305. 'uri' => $uri,
  306. 'type' => 'get',
  307. 'data' => []
  308. ];
  309. return $this->processRequest($response);
  310. }
  311. /**
  312. * Get release by episode id
  313. *
  314. * @param $episodeId
  315. * @return string
  316. */
  317. public function getRelease($episodeId)
  318. {
  319. $uri = 'release';
  320. $uriData = [
  321. 'episodeId' => $episodeId
  322. ];
  323. $response = [
  324. 'uri' => $uri,
  325. 'type' => 'get',
  326. 'data' => $uriData
  327. ];
  328. return $this->processRequest($response);
  329. }
  330. /**
  331. * Adds a previously searched release to the download client,
  332. * if the release is still in Sonarr's search cache (30 minute cache).
  333. * If the release is not found in the cache Sonarr will return a 404.
  334. *
  335. * @param $guid
  336. * @return string
  337. */
  338. public function postRelease($guid)
  339. {
  340. $uri = 'release';
  341. $uriData = [
  342. 'guid' => $guid
  343. ];
  344. $response = [
  345. 'uri' => $uri,
  346. 'type' => 'post',
  347. 'data' => $uriData
  348. ];
  349. return $this->processRequest($response);
  350. }
  351. /**
  352. * Push a release to download client
  353. *
  354. * @param $title
  355. * @param $downloadUrl
  356. * @param $downloadProtocol (Usenet or Torrent)
  357. * @param $publishDate (ISO8601 Date)
  358. * @return string
  359. */
  360. public function postReleasePush($title, $downloadUrl, $downloadProtocol, $publishDate)
  361. {
  362. $uri = 'release';
  363. $uriData = [
  364. 'title' => $title,
  365. 'downloadUrl' => $downloadUrl,
  366. 'downloadProtocol' => $downloadProtocol,
  367. 'publishDate' => $publishDate
  368. ];
  369. $response = [
  370. 'uri' => $uri,
  371. 'type' => 'post',
  372. 'data' => $uriData
  373. ];
  374. return $this->processRequest($response);
  375. }
  376. /**
  377. * Gets root folder
  378. *
  379. * @return array|object|string
  380. */
  381. public function getRootFolder()
  382. {
  383. $uri = 'rootfolder';
  384. $response = [
  385. 'uri' => $uri,
  386. 'type' => 'get',
  387. 'data' => []
  388. ];
  389. return $this->processRequest($response);
  390. }
  391. /**
  392. * Returns all series in your collection
  393. *
  394. * @return array|object|string
  395. */
  396. public function getSeries()
  397. {
  398. $uri = 'series';
  399. $response = [
  400. 'uri' => $uri,
  401. 'type' => 'get',
  402. 'data' => []
  403. ];
  404. return $this->processRequest($response);
  405. }
  406. /**
  407. * Adds a new series to your collection
  408. *
  409. * NOTE: if you do not add the required params, then the series wont function.
  410. * Some of these without the others can indeed make a "series". But it wont function properly in Sonarr.
  411. *
  412. * Required: tvdbId (int) title (string) qualityProfileId (int) titleSlug (string) seasons (array)
  413. * See GET output for format
  414. *
  415. * path (string) - full path to the series on disk or rootFolderPath (string)
  416. * Full path will be created by combining the rootFolderPath with the series title
  417. *
  418. * Optional: tvRageId (int) seasonFolder (bool) monitored (bool)
  419. *
  420. * @param array $data
  421. * @param bool|true $onlyFutureEpisodes It can be used to control which episodes Sonarr monitors
  422. * after adding the series, setting to true (default) will only monitor future episodes.
  423. *
  424. * @return array|object|string
  425. */
  426. public function postSeries(array $data, $onlyFutureEpisodes = true)
  427. {
  428. $uri = 'series';
  429. $uriData = [];
  430. // Required
  431. $uriData['tvdbId'] = $data['tvdbId'];
  432. $uriData['title'] = $data['title'];
  433. $uriData['qualityProfileId'] = $data['qualityProfileId'];
  434. if ( array_key_exists('titleSlug', $data) ) { $uriData['titleSlug'] = $data['titleSlug']; }
  435. if ( array_key_exists('seasons', $data) ) { $uriData['seasons'] = $data['seasons']; }
  436. if ( array_key_exists('path', $data) ) { $uriData['path'] = $data['path']; }
  437. if ( array_key_exists('rootFolderPath', $data) ) { $uriData['rootFolderPath'] = $data['rootFolderPath']; }
  438. if ( array_key_exists('tvRageId', $data) ) { $uriData['tvRageId'] = $data['tvRageId']; }
  439. $uriData['seasonFolder'] = ( array_key_exists('seasonFolder', $data) ) ? $data['seasonFolder'] : true;
  440. if ( array_key_exists('monitored', $data) ) { $uriData['monitored'] = $data['monitored']; }
  441. if ( $onlyFutureEpisodes ) {
  442. $uriData['addOptions'] = [
  443. 'ignoreEpisodesWithFiles' => true,
  444. 'ignoreEpisodesWithoutFiles' => true
  445. ];
  446. }
  447. $response = [
  448. 'uri' => $uri,
  449. 'type' => 'post',
  450. 'data' => $uriData
  451. ];
  452. return $this->processRequest($response);
  453. }
  454. /**
  455. * Delete the series with the given ID
  456. *
  457. * @param int $id
  458. * @param bool|true $deleteFiles
  459. * @return string
  460. */
  461. public function deleteSeries($id, $deleteFiles = true)
  462. {
  463. $uri = 'series';
  464. $uriData = [];
  465. $uriData['deleteFiles'] = ($deleteFiles) ? 'true' : 'false';
  466. $response = [
  467. 'uri' => $uri . '/' . $id,
  468. 'type' => 'delete',
  469. 'data' => $uriData
  470. ];
  471. return $this->processRequest($response);
  472. }
  473. /**
  474. * Searches for new shows on trakt
  475. * Search by name or tvdbid
  476. * Example: 'The Blacklist' or 'tvdb:266189'
  477. *
  478. * @param string $searchTerm query string for the search (Use tvdb:12345 to lookup TVDB ID 12345)
  479. * @return string
  480. */
  481. public function getSeriesLookup($searchTerm)
  482. {
  483. $uri = 'series/lookup';
  484. $uriData = [
  485. 'term' => $searchTerm
  486. ];
  487. $response = [
  488. 'uri' => $uri,
  489. 'type' => 'get',
  490. 'data' => $uriData
  491. ];
  492. return $this->processRequest($response);
  493. }
  494. /**
  495. * Get System Status
  496. *
  497. * @return string
  498. */
  499. public function getSystemStatus()
  500. {
  501. $uri = 'system/status';
  502. $response = [
  503. 'uri' => $uri,
  504. 'type' => 'get',
  505. 'data' => []
  506. ];
  507. return $this->processRequest($response);
  508. }
  509. /**
  510. * Process requests with Guzzle
  511. *
  512. * @param array $params
  513. * @return \Psr\Http\Message\ResponseInterface
  514. */
  515. protected function _request(array $params)
  516. {
  517. $client = new Client(['verify' => getCert()]);
  518. $options = [
  519. 'headers' => [
  520. 'X-Api-Key' => $this->apiKey
  521. ]
  522. ];
  523. if ( $this->httpAuthUsername && $this->httpAuthPassword ) {
  524. $options['auth'] = [
  525. $this->httpAuthUsername,
  526. $this->httpAuthPassword
  527. ];
  528. }
  529. $lidarr = ( $this->lidarr == true ) ? 'v1/' : '';
  530. if ( $params['type'] == 'get' ) {
  531. $url = $this->url . '/api/' . $lidarr . $params['uri'] . '?' . http_build_query($params['data']);
  532. return $client->get($url, $options);
  533. }
  534. if ( $params['type'] == 'put' ) {
  535. $url = $this->url . '/api/' . $lidarr . $params['uri'];
  536. $options['json'] = $params['data'];
  537. return $client->put($url, $options);
  538. }
  539. if ( $params['type'] == 'post' ) {
  540. $url = $this->url . '/api/' . $lidarr . $params['uri'];
  541. $options['json'] = $params['data'];
  542. return $client->post($url, $options);
  543. }
  544. if ( $params['type'] == 'delete' ) {
  545. $url = $this->url . '/api/' . $lidarr . $params['uri'] . '?' . http_build_query($params['data']);
  546. return $client->delete($url, $options);
  547. }
  548. }
  549. /**
  550. * Process requests, catch exceptions, return json response
  551. *
  552. * @param array $request uri, type, data from method
  553. * @return string json encoded response
  554. */
  555. protected function processRequest(array $request)
  556. {
  557. try {
  558. $response = $this->_request(
  559. [
  560. 'uri' => $request['uri'],
  561. 'type' => $request['type'],
  562. 'data' => $request['data']
  563. ]
  564. );
  565. } catch ( \Exception $e ) {
  566. return json_encode(array(
  567. 'error' => array(
  568. 'msg' => $e->getMessage(),
  569. 'code' => $e->getCode(),
  570. ),
  571. ));
  572. exit();
  573. }
  574. return $response->getBody()->getContents();
  575. }
  576. /**
  577. * Verify date is in proper format
  578. *
  579. * @param $date
  580. * @param string $format
  581. * @return bool
  582. */
  583. private function validateDate($date, $format = 'Y-m-d')
  584. {
  585. $d = \DateTime::createFromFormat($format, $date);
  586. return $d && $d->format($format) == $date;
  587. }
  588. }