Sonarr.php 19 KB

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