SickRage.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. <?php
  2. namespace Kryptonit3\SickRage;
  3. use GuzzleHttp\Client;
  4. use Kryptonit3\SickRage\Exceptions\InvalidException;
  5. class SickRage
  6. {
  7. protected $url;
  8. protected $apiKey;
  9. protected $httpAuthUsername;
  10. protected $httpAuthPassword;
  11. public $options;
  12. public function __construct($url, $apiKey, $httpAuthUsername = null, $httpAuthPassword = null, $options = [])
  13. {
  14. $this->url = rtrim($url, '/\\'); // Example: http://127.0.0.1:8081 (no trailing forward-backward slashes)
  15. $this->apiKey = $apiKey;
  16. $this->httpAuthUsername = $httpAuthUsername;
  17. $this->httpAuthPassword = $httpAuthPassword;
  18. $this->options = $options;
  19. }
  20. /**
  21. * Displays the information of a specific episode matching the corresponding tvdbid, season and episode number.
  22. *
  23. * @param int $tvdbId tvdbid unique show id
  24. * @param int $season season number
  25. * @param int $episode episode number
  26. * @param int $fullPath 0: file name only 1: full path
  27. * @return string
  28. * @throws InvalidException
  29. */
  30. public function episode($tvdbId, $season, $episode, $fullPath = 0)
  31. {
  32. $uri = 'episode';
  33. $uriData = [
  34. 'tvdbid' => $tvdbId,
  35. 'season' => $season,
  36. 'episode' => $episode,
  37. 'full_path' => $fullPath
  38. ];
  39. try {
  40. $response = $this->_request(
  41. [
  42. 'uri' => $uri,
  43. 'type' => 'get',
  44. 'data' => $uriData
  45. ]
  46. );
  47. } catch (\Exception $e) {
  48. throw new InvalidException($e->getMessage());
  49. }
  50. return $response->getBody()->getContents();
  51. }
  52. /**
  53. * Initiate a search for a specific episode matching the corresponding tvdbid, season and episode number.
  54. *
  55. * @param int $tvdbId tvdbid unique show id
  56. * @param int $season season number
  57. * @param int $episode episode number
  58. * @return string
  59. * @throws InvalidException
  60. */
  61. public function episodeSearch($tvdbId, $season, $episode)
  62. {
  63. $uri = 'episode';
  64. $uriData = [
  65. 'tvdbid' => $tvdbId,
  66. 'season' => $season,
  67. 'episode' => $episode
  68. ];
  69. try {
  70. $response = $this->_request(
  71. [
  72. 'uri' => $uri,
  73. 'type' => 'get',
  74. 'data' => $uriData
  75. ]
  76. );
  77. } catch (\Exception $e) {
  78. throw new InvalidException($e->getMessage());
  79. }
  80. return $response->getBody()->getContents();
  81. }
  82. /**
  83. * Set the status of an epsiode or season.
  84. *
  85. * @param int $tvdbId tvdbid unique show id
  86. * @param int $season season number
  87. * @param string $status wanted, skipped, archived, ignored
  88. * @param int|null $episode episode number
  89. * --- if an episode is not provided, then the whole seasons' status will be set.
  90. * @param int $force 0: not existing episodes 1: include existing episodes (can replace downloaded episodes)
  91. * @return string
  92. * @throws InvalidException
  93. */
  94. public function episodeSetStatus($tvdbId, $season, $status, $episode = null, $force = 0)
  95. {
  96. $uri = 'episode.setstatus';
  97. $uriData = [
  98. 'tvdbid' => $tvdbId,
  99. 'season' => $season,
  100. 'status' => $status,
  101. 'force' => $force
  102. ];
  103. if ( $episode ) { $uriData['episode'] = $episode; }
  104. try {
  105. $response = $this->_request(
  106. [
  107. 'uri' => $uri,
  108. 'type' => 'get',
  109. 'data' => $uriData
  110. ]
  111. );
  112. } catch (\Exception $e) {
  113. throw new InvalidException($e->getMessage());
  114. }
  115. return $response->getBody()->getContents();
  116. }
  117. /**
  118. * Display scene exceptions for all or a given show.
  119. *
  120. * @param int|null $tvdbId tvdbid unique show id
  121. * @return string
  122. * @throws InvalidException
  123. */
  124. public function exceptions($tvdbId = null)
  125. {
  126. $uri = 'exceptions';
  127. $uriData = [];
  128. if ( $tvdbId ) { $uriData['tvdbid'] = $tvdbId; }
  129. try {
  130. $response = $this->_request(
  131. [
  132. 'uri' => $uri,
  133. 'type' => 'get',
  134. 'data' => $uriData
  135. ]
  136. );
  137. } catch (\Exception $e) {
  138. throw new InvalidException($e->getMessage());
  139. }
  140. return $response->getBody()->getContents();
  141. }
  142. /**
  143. * Display the upcoming episodes for the shows currently added in the users' database.
  144. *
  145. * @param string $sort date, network, name
  146. * @param string $type missed, today, soon, later - multiple types can be passed when delimited by |
  147. * --- missed - show's date is older than today
  148. * --- today - show's date is today
  149. * --- soon - show's date greater than today but less than a week
  150. * --- later - show's date greater than a week
  151. * @param int|null $paused 0: do not show paused 1: show paused
  152. * --- if not set then the user's default setting in SickRage is used
  153. * @return string
  154. * @throws InvalidException
  155. */
  156. public function future($sort = 'date', $type = 'missed|today|soon|later', $paused = null)
  157. {
  158. $uri = 'future';
  159. $uriData = [
  160. 'sort' => $sort,
  161. 'type' => $type
  162. ];
  163. if ( $paused ) { $uriData['paused'] = $paused; }
  164. try {
  165. $response = $this->_request(
  166. [
  167. 'uri' => $uri,
  168. 'type' => 'get',
  169. 'data' => $uriData
  170. ]
  171. );
  172. } catch (\Exception $e) {
  173. throw new InvalidException($e->getMessage());
  174. }
  175. return $response->getBody()->getContents();
  176. }
  177. /**
  178. * Display SickRage's downloaded/snatched history.
  179. *
  180. * @param int $limit Use 0 if you want to see all history, note this could cause
  181. * --- heavy cpu/disk usage for the user as well as cause your application to time out
  182. * --- while it's waiting for the data.
  183. * @param string|null $type downloaded, snatched
  184. * @return string
  185. * @throws InvalidException
  186. */
  187. public function history($limit = 100, $type = null)
  188. {
  189. $uri = 'history';
  190. $uriData = [
  191. 'limit' => $limit
  192. ];
  193. if ( $type ) { $uriData['type'] = $type; }
  194. try {
  195. $response = $this->_request(
  196. [
  197. 'uri' => $uri,
  198. 'type' => 'get',
  199. 'data' => $uriData
  200. ]
  201. );
  202. } catch (\Exception $e) {
  203. throw new InvalidException($e->getMessage());
  204. }
  205. return $response->getBody()->getContents();
  206. }
  207. /**
  208. * Clear SickRage's history.
  209. *
  210. * @return string
  211. * @throws InvalidException
  212. */
  213. public function historyClear()
  214. {
  215. $uri = 'history.clear';
  216. try {
  217. $response = $this->_request(
  218. [
  219. 'uri' => $uri,
  220. 'type' => 'get',
  221. 'data' => []
  222. ]
  223. );
  224. } catch (\Exception $e) {
  225. throw new InvalidException($e->getMessage());
  226. }
  227. return $response->getBody()->getContents();
  228. }
  229. /**
  230. * Trim SickRage's history by removing entries greater than 30 days old.
  231. *
  232. * @return string
  233. * @throws InvalidException
  234. */
  235. public function historyTrim()
  236. {
  237. $uri = 'history.trim';
  238. try {
  239. $response = $this->_request(
  240. [
  241. 'uri' => $uri,
  242. 'type' => 'get',
  243. 'data' => []
  244. ]
  245. );
  246. } catch (\Exception $e) {
  247. throw new InvalidException($e->getMessage());
  248. }
  249. return $response->getBody()->getContents();
  250. }
  251. /**
  252. * View SickRage's log.
  253. *
  254. * @param string $minLevel debug, info, warning, error
  255. * @return string
  256. * @throws InvalidException
  257. */
  258. public function logs($minLevel = 'error')
  259. {
  260. $uri = 'history.trim';
  261. $uriData = [
  262. 'min_level' => $minLevel
  263. ];
  264. try {
  265. $response = $this->_request(
  266. [
  267. 'uri' => $uri,
  268. 'type' => 'get',
  269. 'data' => $uriData
  270. ]
  271. );
  272. } catch (\Exception $e) {
  273. throw new InvalidException($e->getMessage());
  274. }
  275. return $response->getBody()->getContents();
  276. }
  277. /**
  278. * Display information for a given show.
  279. *
  280. * @param int $tvdbId tvdbid unique show id
  281. * @return string
  282. * @throws InvalidException
  283. */
  284. public function show($tvdbId)
  285. {
  286. $uri = 'show';
  287. $uriData = [
  288. 'tvdbid' => $tvdbId
  289. ];
  290. try {
  291. $response = $this->_request(
  292. [
  293. 'uri' => $uri,
  294. 'type' => 'get',
  295. 'data' => $uriData
  296. ]
  297. );
  298. } catch (\Exception $e) {
  299. throw new InvalidException($e->getMessage());
  300. }
  301. return $response->getBody()->getContents();
  302. }
  303. /**
  304. * Add a show to SickRage using an existing folder.
  305. *
  306. * @param int $tvdbId tvdbid unique show id
  307. * @param string $location path to existing show folder
  308. * @param int|null $flattenFolders
  309. * --- 0: use season folders if part of rename string
  310. * --- 1: do not use season folders
  311. * --- if not provided then the config setting (default) is used
  312. * @param string|null $initial multiple types can be passed when delimited by |
  313. * --- sdtv, sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray, unknown
  314. * --- if not provided then the config setting (default) is used
  315. * @param string|null $archive multiple types can be passed when delimited by |
  316. * --- sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray
  317. * --- if not provided then the config setting (default) is used
  318. * @return string
  319. * @throws InvalidException
  320. */
  321. public function showAddExisting($tvdbId, $location, $flattenFolders = null, $initial = null, $archive = null)
  322. {
  323. $uri = 'show.addexisting';
  324. $uriData = [
  325. 'tvdbid' => $tvdbId,
  326. 'location' => $location
  327. ];
  328. if ( $flattenFolders ) { $uriData['flatten_folders'] = $flattenFolders; }
  329. if ( $initial ) { $uriData['initial'] = $initial; }
  330. if ( $archive ) { $uriData['archive'] = $archive; }
  331. try {
  332. $response = $this->_request(
  333. [
  334. 'uri' => $uri,
  335. 'type' => 'get',
  336. 'data' => $uriData
  337. ]
  338. );
  339. } catch (\Exception $e) {
  340. throw new InvalidException($e->getMessage());
  341. }
  342. return $response->getBody()->getContents();
  343. }
  344. /**
  345. * Add a show to SickRage providing the parent directory where the tv shows folder should be created.
  346. *
  347. * @param int $tvdbId tvdbid unique show id
  348. * @param string|null $location path to existing folder to store show
  349. * --- if not provided then the config setting (default) is used -- if valid
  350. * @param string $lang two letter tvdb language, en = english
  351. * --- en, zh, hr, cs, da, nl, fi, fr, de, el, he, hu, it, ja, ko, no, pl, pt, ru, sl, es, sv, tr
  352. * @param int|null $flattenFolders
  353. * --- 0: use season folders if part of rename string
  354. * --- 1: do not use season folders
  355. * --- if not provided then the config setting (default) is used
  356. * @param string|null $status wanted, skipped, archived, ignored
  357. * --- if not provided then the config setting (default) is used
  358. * @param string|null $initial multiple types can be passed when delimited by |
  359. * --- sdtv, sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray, unknown
  360. * --- if not provided then the config setting (default) is used
  361. * @param string|null $archive multiple types can be passed when delimited by |
  362. * --- sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray
  363. * --- if not provided then the config setting (default) is used
  364. * @return string
  365. * @throws InvalidException
  366. */
  367. public function showAddNew($tvdbId, $location = null, $lang = 'en', $flattenFolders = null,
  368. $status = null, $initial = null, $archive = null)
  369. {
  370. $uri = 'show.addnew';
  371. $uriData = [
  372. 'tvdbid' => $tvdbId,
  373. 'lang' => $lang
  374. ];
  375. if ( $flattenFolders ) { $uriData['flatten_folders'] = $flattenFolders; }
  376. if ( $initial ) { $uriData['initial'] = $initial; }
  377. if ( $archive ) { $uriData['archive'] = $archive; }
  378. if ( $status ) { $uriData['status'] = $status; }
  379. if ( $location ) { $uriData['location'] = $location; }
  380. try {
  381. $response = $this->_request(
  382. [
  383. 'uri' => $uri,
  384. 'type' => 'get',
  385. 'data' => $uriData
  386. ]
  387. );
  388. } catch (\Exception $e) {
  389. throw new InvalidException($e->getMessage());
  390. }
  391. return $response->getBody()->getContents();
  392. }
  393. /**
  394. * Display if the poster/banner SickRage's image cache is valid.
  395. *
  396. * @param int $tvdbId tvdbid unique show id
  397. * @return string
  398. * @throws InvalidException
  399. */
  400. public function showCache($tvdbId)
  401. {
  402. $uri = 'show.cache';
  403. $uriData = [
  404. 'tvdbid' => $tvdbId
  405. ];
  406. try {
  407. $response = $this->_request(
  408. [
  409. 'uri' => $uri,
  410. 'type' => 'get',
  411. 'data' => $uriData
  412. ]
  413. );
  414. } catch (\Exception $e) {
  415. throw new InvalidException($e->getMessage());
  416. }
  417. return $response->getBody()->getContents();
  418. }
  419. /**
  420. * Delete a show from SickRage.
  421. *
  422. * @param int $tvdbId tvdbid unique show id
  423. * @return string
  424. * @throws InvalidException
  425. */
  426. public function showDelete($tvdbId)
  427. {
  428. $uri = 'show.delete';
  429. $uriData = [
  430. 'tvdbid' => $tvdbId
  431. ];
  432. try {
  433. $response = $this->_request(
  434. [
  435. 'uri' => $uri,
  436. 'type' => 'get',
  437. 'data' => $uriData
  438. ]
  439. );
  440. } catch (\Exception $e) {
  441. throw new InvalidException($e->getMessage());
  442. }
  443. return $response->getBody()->getContents();
  444. }
  445. /**
  446. * Retrieve the stored banner image from SickRage's cache for a particular tvdbid.
  447. * If no image is found then the default sb banner is shown.
  448. *
  449. * @param int $tvdbId tvdbid unique show id
  450. * @return string
  451. * @throws InvalidException
  452. */
  453. public function showGetBanner($tvdbId)
  454. {
  455. $uri = 'show.getbanner';
  456. $uriData = [
  457. 'tvdbid' => $tvdbId
  458. ];
  459. try {
  460. $response = $this->_request(
  461. [
  462. 'uri' => $uri,
  463. 'type' => 'get',
  464. 'data' => $uriData
  465. ]
  466. );
  467. } catch (\Exception $e) {
  468. throw new InvalidException($e->getMessage());
  469. }
  470. return $response->getBody()->getContents();
  471. }
  472. /**
  473. * Retrieve the stored poster image from SickRage's cache for a particular tvdbid.
  474. * If no image is found then the default sb poster is shown.
  475. *
  476. * @param int $tvdbId tvdbid unique show id
  477. * @return string
  478. * @throws InvalidException
  479. */
  480. public function showGetPoster($tvdbId)
  481. {
  482. $uri = 'show.getposter';
  483. $uriData = [
  484. 'tvdbid' => $tvdbId
  485. ];
  486. try {
  487. $response = $this->_request(
  488. [
  489. 'uri' => $uri,
  490. 'type' => 'get',
  491. 'data' => $uriData
  492. ]
  493. );
  494. } catch (\Exception $e) {
  495. throw new InvalidException($e->getMessage());
  496. }
  497. return $response->getBody()->getContents();
  498. }
  499. /**
  500. * Get quality settings of a show in SickRage.
  501. *
  502. * @param int $tvdbId tvdbid unique show id
  503. * @return string
  504. * @throws InvalidException
  505. */
  506. public function showGetQuality($tvdbId)
  507. {
  508. $uri = 'show.getquality';
  509. $uriData = [
  510. 'tvdbid' => $tvdbId
  511. ];
  512. try {
  513. $response = $this->_request(
  514. [
  515. 'uri' => $uri,
  516. 'type' => 'get',
  517. 'data' => $uriData
  518. ]
  519. );
  520. } catch (\Exception $e) {
  521. throw new InvalidException($e->getMessage());
  522. }
  523. return $response->getBody()->getContents();
  524. }
  525. /**
  526. * Set a show's paused state in SickRage.
  527. *
  528. * @param int $tvdbId tvdbid unique show id
  529. * @param int $pause 0: unpause show 1: pause show
  530. * @return string
  531. * @throws InvalidException
  532. */
  533. public function showPause($tvdbId, $pause = 0)
  534. {
  535. $uri = 'show.pause';
  536. $uriData = [
  537. 'tvdbid' => $tvdbId,
  538. 'pause' => $pause
  539. ];
  540. try {
  541. $response = $this->_request(
  542. [
  543. 'uri' => $uri,
  544. 'type' => 'get',
  545. 'data' => $uriData
  546. ]
  547. );
  548. } catch (\Exception $e) {
  549. throw new InvalidException($e->getMessage());
  550. }
  551. return $response->getBody()->getContents();
  552. }
  553. /**
  554. * Refresh a show in SickRage by rescanning local files.
  555. *
  556. * @param int $tvdbId tvdbid unique show id
  557. * @return string
  558. * @throws InvalidException
  559. */
  560. public function showRefresh($tvdbId)
  561. {
  562. $uri = 'show.refresh';
  563. $uriData = [
  564. 'tvdbid' => $tvdbId
  565. ];
  566. try {
  567. $response = $this->_request(
  568. [
  569. 'uri' => $uri,
  570. 'type' => 'get',
  571. 'data' => $uriData
  572. ]
  573. );
  574. } catch (\Exception $e) {
  575. throw new InvalidException($e->getMessage());
  576. }
  577. return $response->getBody()->getContents();
  578. }
  579. /**
  580. * Display the season list for a given show.
  581. *
  582. * @param int $tvdbId tvdbid unique show id
  583. * @param string $sort asc, desc
  584. * @return string
  585. * @throws InvalidException
  586. */
  587. public function showSeasonList($tvdbId, $sort = 'desc')
  588. {
  589. $uri = 'show.seasonlist';
  590. $uriData = [
  591. 'tvdbid' => $tvdbId,
  592. 'sort' => $sort
  593. ];
  594. try {
  595. $response = $this->_request(
  596. [
  597. 'uri' => $uri,
  598. 'type' => 'get',
  599. 'data' => $uriData
  600. ]
  601. );
  602. } catch (\Exception $e) {
  603. throw new InvalidException($e->getMessage());
  604. }
  605. return $response->getBody()->getContents();
  606. }
  607. /**
  608. * Display a listing of episodes for all or a given season.
  609. *
  610. * @param int $tvdbId tvdbid unique show id
  611. * @param int|null $season season number
  612. * @return string
  613. * @throws InvalidException
  614. */
  615. public function showSeasons($tvdbId, $season = null)
  616. {
  617. $uri = 'show.seasons';
  618. $uriData = [
  619. 'tvdbid' => $tvdbId
  620. ];
  621. if ( is_numeric($season) ) { $uriData['season'] = $season; }
  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. * Set desired quality of a show in SickRage.
  637. *
  638. * @param int $tvdbId tvdbid unique show id
  639. * @param string|null $initial multiple types can be passed when delimited by |
  640. * --- sdtv, sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray, unknown
  641. * @param string|null $archive multiple types can be passed when delimited by |
  642. * --- sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray
  643. * @return string
  644. * @throws InvalidException
  645. */
  646. public function showSetQuality($tvdbId, $initial = null, $archive = null)
  647. {
  648. $uri = 'show.setquality';
  649. $uriData = [
  650. 'tvdbid' => $tvdbId
  651. ];
  652. if ( $initial ) { $uriData['initial'] = $initial; }
  653. if ( $archive ) { $uriData['archive'] = $archive; }
  654. try {
  655. $response = $this->_request(
  656. [
  657. 'uri' => $uri,
  658. 'type' => 'get',
  659. 'data' => $uriData
  660. ]
  661. );
  662. } catch (\Exception $e) {
  663. throw new InvalidException($e->getMessage());
  664. }
  665. return $response->getBody()->getContents();
  666. }
  667. /**
  668. * Display episode statistics for a given show.
  669. *
  670. * @param int $tvdbId tvdbid unique show id
  671. * @return string
  672. * @throws InvalidException
  673. */
  674. public function showStats($tvdbId)
  675. {
  676. $uri = 'show.stats';
  677. $uriData = [
  678. 'tvdbid' => $tvdbId
  679. ];
  680. try {
  681. $response = $this->_request(
  682. [
  683. 'uri' => $uri,
  684. 'type' => 'get',
  685. 'data' => $uriData
  686. ]
  687. );
  688. } catch (\Exception $e) {
  689. throw new InvalidException($e->getMessage());
  690. }
  691. return $response->getBody()->getContents();
  692. }
  693. /**
  694. * Update a show in SickRage by pulling down information from TVDB and rescan local files.
  695. *
  696. * @param int $tvdbId tvdbid unique show id
  697. * @return string
  698. * @throws InvalidException
  699. */
  700. public function showUpdate($tvdbId)
  701. {
  702. $uri = 'show.update';
  703. $uriData = [
  704. 'tvdbid' => $tvdbId
  705. ];
  706. try {
  707. $response = $this->_request(
  708. [
  709. 'uri' => $uri,
  710. 'type' => 'get',
  711. 'data' => $uriData
  712. ]
  713. );
  714. } catch (\Exception $e) {
  715. throw new InvalidException($e->getMessage());
  716. }
  717. return $response->getBody()->getContents();
  718. }
  719. /**
  720. * Display a list of all shows in SickRage.
  721. *
  722. * @param string $sort id, name
  723. * --- id - sort by tvdbid
  724. * --- name - sort by show name
  725. * @param int|null $paused if not set then both paused and non paused are shown
  726. * --- 0: show only non paused
  727. * --- 1: show only paused
  728. * @return string
  729. * @throws InvalidException
  730. */
  731. public function shows($sort = 'id', $paused = null)
  732. {
  733. $uri = 'shows';
  734. $uriData = [
  735. 'sort' => $sort
  736. ];
  737. if ( $paused ) { $uriData['paused'] = $paused; }
  738. try {
  739. $response = $this->_request(
  740. [
  741. 'uri' => $uri,
  742. 'type' => 'get',
  743. 'data' => $uriData
  744. ]
  745. );
  746. } catch (\Exception $e) {
  747. throw new InvalidException($e->getMessage());
  748. }
  749. return $response->getBody()->getContents();
  750. }
  751. /**
  752. * Display global episode and show statistics.
  753. *
  754. * @return string
  755. * @throws InvalidException
  756. */
  757. public function showsStats()
  758. {
  759. $uri = 'shows.stats';
  760. try {
  761. $response = $this->_request(
  762. [
  763. 'uri' => $uri,
  764. 'type' => 'get',
  765. 'data' => []
  766. ]
  767. );
  768. } catch (\Exception $e) {
  769. throw new InvalidException($e->getMessage());
  770. }
  771. return $response->getBody()->getContents();
  772. }
  773. /**
  774. * Display misc SickRage related information.
  775. * This is also the default command that the api will show if none is specified.
  776. *
  777. * @return string
  778. * @throws InvalidException
  779. */
  780. public function sb()
  781. {
  782. $uri = 'sb';
  783. try {
  784. $response = $this->_request(
  785. [
  786. 'uri' => $uri,
  787. 'type' => 'get',
  788. 'data' => []
  789. ]
  790. );
  791. } catch (\Exception $e) {
  792. throw new InvalidException($e->getMessage());
  793. }
  794. return $response->getBody()->getContents();
  795. }
  796. /**
  797. * Add a root (parent) directory (only if it is a valid location),
  798. * and set as the default root dir if requested to SickRages's config.
  799. *
  800. * @param string $location full path to a root (parent) directory of tv shows
  801. * @param int $default
  802. * --- 0: do not change global default
  803. * --- 1: set location as the new global default
  804. * @return string
  805. * @throws InvalidException
  806. */
  807. public function sbAddRootDir($location, $default = 0)
  808. {
  809. $uri = 'sb.addrootdir';
  810. $uriData = [
  811. 'location' => $location,
  812. 'default' => $default
  813. ];
  814. try {
  815. $response = $this->_request(
  816. [
  817. 'uri' => $uri,
  818. 'type' => 'get',
  819. 'data' => $uriData
  820. ]
  821. );
  822. } catch (\Exception $e) {
  823. throw new InvalidException($e->getMessage());
  824. }
  825. return $response->getBody()->getContents();
  826. }
  827. /**
  828. * Query the SickBeard scheduler.
  829. *
  830. * @return string
  831. * @throws InvalidException
  832. */
  833. public function sbCheckScheduler()
  834. {
  835. $uri = 'sb.checkscheduler';
  836. try {
  837. $response = $this->_request(
  838. [
  839. 'uri' => $uri,
  840. 'type' => 'get',
  841. 'data' => []
  842. ]
  843. );
  844. } catch (\Exception $e) {
  845. throw new InvalidException($e->getMessage());
  846. }
  847. return $response->getBody()->getContents();
  848. }
  849. /**
  850. * Delete a root (parent) directory from the root directory list in SickRage's config.
  851. *
  852. * @param string $location
  853. * @return string
  854. * @throws InvalidException
  855. */
  856. public function sbDeleteRootDir($location)
  857. {
  858. $uri = 'sb.deleterootdir';
  859. $uriData = [
  860. 'location' => $location
  861. ];
  862. try {
  863. $response = $this->_request(
  864. [
  865. 'uri' => $uri,
  866. 'type' => 'get',
  867. 'data' => $uriData
  868. ]
  869. );
  870. } catch (\Exception $e) {
  871. throw new InvalidException($e->getMessage());
  872. }
  873. return $response->getBody()->getContents();
  874. }
  875. /**
  876. * Force the episode search early.
  877. *
  878. * @return string
  879. * @throws InvalidException
  880. */
  881. public function sbForceSearch()
  882. {
  883. $uri = 'sb.forcesearch';
  884. try {
  885. $response = $this->_request(
  886. [
  887. 'uri' => $uri,
  888. 'type' => 'get',
  889. 'data' => []
  890. ]
  891. );
  892. } catch (\Exception $e) {
  893. throw new InvalidException($e->getMessage());
  894. }
  895. return $response->getBody()->getContents();
  896. }
  897. /**
  898. * Get default settings from SickBeard's config.
  899. *
  900. * @return string
  901. * @throws InvalidException
  902. */
  903. public function sbGetDefaults()
  904. {
  905. $uri = 'sb.getdefaults';
  906. try {
  907. $response = $this->_request(
  908. [
  909. 'uri' => $uri,
  910. 'type' => 'get',
  911. 'data' => []
  912. ]
  913. );
  914. } catch (\Exception $e) {
  915. throw new InvalidException($e->getMessage());
  916. }
  917. return $response->getBody()->getContents();
  918. }
  919. /**
  920. * Get un-claimed messages from the ui.notification queue.
  921. *
  922. * @return string
  923. * @throws InvalidException
  924. */
  925. public function sbGetMessages()
  926. {
  927. $uri = 'sb.getmessages';
  928. try {
  929. $response = $this->_request(
  930. [
  931. 'uri' => $uri,
  932. 'type' => 'get',
  933. 'data' => []
  934. ]
  935. );
  936. } catch (\Exception $e) {
  937. throw new InvalidException($e->getMessage());
  938. }
  939. return $response->getBody()->getContents();
  940. }
  941. /**
  942. * Get the root (parent) directories from SickBeard's config, test if valid, and which is the default.
  943. *
  944. * @return string
  945. * @throws InvalidException
  946. */
  947. public function sbGetRootDirs()
  948. {
  949. $uri = 'sb.getrootdirs';
  950. try {
  951. $response = $this->_request(
  952. [
  953. 'uri' => $uri,
  954. 'type' => 'get',
  955. 'data' => []
  956. ]
  957. );
  958. } catch (\Exception $e) {
  959. throw new InvalidException($e->getMessage());
  960. }
  961. return $response->getBody()->getContents();
  962. }
  963. /**
  964. * Pause the backlog search.
  965. *
  966. * @param int $pause
  967. * --- 0: unpause the backlog
  968. * --- 1: pause the backlog
  969. * @return string
  970. * @throws InvalidException
  971. */
  972. public function sbPauseBacklog($pause = 0)
  973. {
  974. $uri = 'sb.pausebacklog';
  975. $uriData = [
  976. 'pause' => $pause
  977. ];
  978. try {
  979. $response = $this->_request(
  980. [
  981. 'uri' => $uri,
  982. 'type' => 'get',
  983. 'data' => $uriData
  984. ]
  985. );
  986. } catch (\Exception $e) {
  987. throw new InvalidException($e->getMessage());
  988. }
  989. return $response->getBody()->getContents();
  990. }
  991. /**
  992. * Check to see if SickRage is running.
  993. *
  994. * @return string
  995. * @throws InvalidException
  996. */
  997. public function sbPing()
  998. {
  999. $uri = 'sb.ping';
  1000. try {
  1001. $response = $this->_request(
  1002. [
  1003. 'uri' => $uri,
  1004. 'type' => 'get',
  1005. 'data' => []
  1006. ]
  1007. );
  1008. } catch (\Exception $e) {
  1009. throw new InvalidException($e->getMessage());
  1010. }
  1011. return $response->getBody()->getContents();
  1012. }
  1013. /**
  1014. * Restart SickRage.
  1015. *
  1016. * @return string
  1017. * @throws InvalidException
  1018. */
  1019. public function sbRestart()
  1020. {
  1021. $uri = 'sb.restart';
  1022. try {
  1023. $response = $this->_request(
  1024. [
  1025. 'uri' => $uri,
  1026. 'type' => 'get',
  1027. 'data' => []
  1028. ]
  1029. );
  1030. } catch (\Exception $e) {
  1031. throw new InvalidException($e->getMessage());
  1032. }
  1033. return $response->getBody()->getContents();
  1034. }
  1035. /**
  1036. * Search TVDB for a show with a given string or tvdbid.
  1037. *
  1038. * @param string|null $name show name
  1039. * @param int|null $tvdbId tvdbid unique show id
  1040. * @param string $lang two letter tvdb language, en = english
  1041. * --- en, zh, hr, cs, da, nl, fi, fr, de, el, he, hu, it, ja, ko, no, pl, pt, ru, sl, es, sv, tr
  1042. * @return string
  1043. * @throws InvalidException
  1044. */
  1045. public function sbSearchTvdb($name = null, $tvdbId = null, $lang = 'en')
  1046. {
  1047. $uri = 'sb.searchtvdb';
  1048. $uriData = [
  1049. 'lang' => $lang
  1050. ];
  1051. if ( $name ) { $uriData['name'] = $name; }
  1052. if ( $tvdbId ) { $uriData['tvdbid'] = $tvdbId; }
  1053. try {
  1054. $response = $this->_request(
  1055. [
  1056. 'uri' => $uri,
  1057. 'type' => 'get',
  1058. 'data' => $uriData
  1059. ]
  1060. );
  1061. } catch (\Exception $e) {
  1062. throw new InvalidException($e->getMessage());
  1063. }
  1064. return $response->getBody()->getContents();
  1065. }
  1066. /**
  1067. * Set default settings for SickRage.
  1068. *
  1069. * @param int|null $futureShowPaused
  1070. * --- 0: exclude paused shows on coming ep
  1071. * --- 1: include paused shows on coming ep
  1072. * @param string|null $status wanted, skipped, archived, ignored
  1073. * @param int|null $flattenFolders
  1074. * --- 0: use season folders if part of rename string
  1075. * --- 1: do not use season folders
  1076. * @param string|null $initial multiple types can be passed when delimited by |
  1077. * --- sdtv, sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray, unknown
  1078. * @param string|null $archive multiple types can be passed when delimited by |
  1079. * --- sddvd, hdtv, rawhdtv, fullhdtv, hdwebdl, fullhdwebdl, hdbluray, fullhdbluray
  1080. * @return string
  1081. * @throws InvalidException
  1082. */
  1083. public function sbSetDefaults($futureShowPaused = null, $status = null,
  1084. $flattenFolders = null, $initial = null, $archive = null)
  1085. {
  1086. $uri = 'sb.setdefaults';
  1087. $uriData = [];
  1088. if ( $futureShowPaused ) { $uriData['future_show_paused'] = $futureShowPaused; }
  1089. if ( $status ) { $uriData['status'] = $status; }
  1090. if ( $flattenFolders ) { $uriData['flatten_folders'] = $flattenFolders; }
  1091. if ( $initial ) { $uriData['initial'] = $initial; }
  1092. if ( $archive ) { $uriData['archive'] = $archive; }
  1093. try {
  1094. $response = $this->_request(
  1095. [
  1096. 'uri' => $uri,
  1097. 'type' => 'get',
  1098. 'data' => $uriData
  1099. ]
  1100. );
  1101. } catch (\Exception $e) {
  1102. throw new InvalidException($e->getMessage());
  1103. }
  1104. return $response->getBody()->getContents();
  1105. }
  1106. /**
  1107. * Shutdown SickRage.
  1108. *
  1109. * @return string
  1110. * @throws InvalidException
  1111. */
  1112. public function sbShutdown()
  1113. {
  1114. $uri = 'sb.shutdown';
  1115. try {
  1116. $response = $this->_request(
  1117. [
  1118. 'uri' => $uri,
  1119. 'type' => 'get',
  1120. 'data' => []
  1121. ]
  1122. );
  1123. } catch (\Exception $e) {
  1124. throw new InvalidException($e->getMessage());
  1125. }
  1126. return $response->getBody()->getContents();
  1127. }
  1128. /**
  1129. * Process requests with Guzzle
  1130. *
  1131. * @param array $params
  1132. * @return \Psr\Http\Message\ResponseInterface
  1133. */
  1134. protected function _request(array $params)
  1135. {
  1136. $client = new Client($this->options);
  1137. if ( $params['type'] == 'get' ) {
  1138. $url = $this->url . '/api/' . $this->apiKey . '/?cmd=' . $params['uri'] . '&' . http_build_query($params['data']);
  1139. $options = [];
  1140. if ( $this->httpAuthUsername && $this->httpAuthPassword ) {
  1141. $options['auth'] = [
  1142. $this->httpAuthUsername,
  1143. $this->httpAuthPassword
  1144. ];
  1145. }
  1146. return $client->get($url, $options);
  1147. }
  1148. }
  1149. }