Sonarr.php 17 KB

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