SickRage.php 35 KB

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