sabnzbd.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. trait SabNZBdHomepageItem
  3. {
  4. public function testConnectionSabNZBd()
  5. {
  6. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  7. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  8. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  9. try {
  10. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  11. $response = Requests::get($url, array(), $options);
  12. if ($response->success) {
  13. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  14. return true;
  15. }
  16. } catch (Requests_Exception $e) {
  17. $this->setAPIResponse('error', $e->getMessage(), 500);
  18. return false;
  19. };
  20. } else {
  21. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  22. return 'URL and/or Token not setup';
  23. }
  24. }
  25. public function getSabNZBdHomepageQueue()
  26. {
  27. if (!$this->config['homepageSabnzbdEnabled']) {
  28. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  29. return false;
  30. }
  31. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  32. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  33. return false;
  34. }
  35. if (empty($this->config['sabnzbdURL'])) {
  36. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  37. return false;
  38. }
  39. if (empty($this->config['sabnzbdToken'])) {
  40. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  41. return false;
  42. }
  43. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  44. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  45. try {
  46. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  47. $response = Requests::get($url, array(), $options);
  48. if ($response->success) {
  49. $api['content']['queueItems'] = json_decode($response->body, true);
  50. }
  51. } catch (Requests_Exception $e) {
  52. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  53. $this->setAPIResponse('error', $e->getMessage(), 500);
  54. return false;
  55. };
  56. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  57. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  58. try {
  59. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  60. $response = Requests::get($url, array(), $options);
  61. if ($response->success) {
  62. $api['content']['historyItems'] = json_decode($response->body, true);
  63. }
  64. } catch (Requests_Exception $e) {
  65. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  66. $this->setAPIResponse('error', $e->getMessage(), 500);
  67. return false;
  68. };
  69. $api['content'] = isset($api['content']) ? $api['content'] : false;
  70. $this->setAPIResponse('success', null, 200, $api);
  71. return $api;
  72. }
  73. public function pauseSabNZBdQueue($target = null)
  74. {
  75. if (!$this->config['homepageSabnzbdEnabled']) {
  76. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  77. return false;
  78. }
  79. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  80. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  81. return false;
  82. }
  83. if (empty($this->config['sabnzbdURL'])) {
  84. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  85. return false;
  86. }
  87. if (empty($this->config['sabnzbdToken'])) {
  88. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  89. return false;
  90. }
  91. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  92. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  93. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  94. try {
  95. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  96. $response = Requests::get($url, array(), $options);
  97. if ($response->success) {
  98. $api['content'] = json_decode($response->body, true);
  99. }
  100. } catch (Requests_Exception $e) {
  101. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  102. $this->setAPIResponse('error', $e->getMessage(), 500);
  103. return false;
  104. };
  105. $api['content'] = isset($api['content']) ? $api['content'] : false;
  106. $this->setAPIResponse('success', null, 200, $api);
  107. return $api;
  108. }
  109. public function resumeSabNZBdQueue($target = null)
  110. {
  111. if (!$this->config['homepageSabnzbdEnabled']) {
  112. $this->setAPIResponse('error', 'SabNZBd homepage item is not enabled', 409);
  113. return false;
  114. }
  115. if (!$this->qualifyRequest($this->config['homepageSabnzbdAuth'])) {
  116. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  117. return false;
  118. }
  119. if (empty($this->config['sabnzbdURL'])) {
  120. $this->setAPIResponse('error', 'Plex URL is not defined', 422);
  121. return false;
  122. }
  123. if (empty($this->config['sabnzbdToken'])) {
  124. $this->setAPIResponse('error', 'Plex Token is not defined', 422);
  125. return false;
  126. }
  127. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  128. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  129. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  130. try {
  131. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  132. $response = Requests::get($url, array(), $options);
  133. if ($response->success) {
  134. $api['content'] = json_decode($response->body, true);
  135. }
  136. } catch (Requests_Exception $e) {
  137. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  138. $this->setAPIResponse('error', $e->getMessage(), 500);
  139. return false;
  140. };
  141. $api['content'] = isset($api['content']) ? $api['content'] : false;
  142. $this->setAPIResponse('success', null, 200, $api);
  143. return $api;
  144. }
  145. }