Sonarr.php 22 KB

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