jackett.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. trait JackettHomepageItem
  3. {
  4. public function jackettSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Jackett',
  8. 'enabled' => true,
  9. 'image' => 'plugins/images/tabs/jackett.png',
  10. 'category' => 'Utility',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = array(
  17. 'debug' => true,
  18. 'settings' => array(
  19. 'Enable' => array(
  20. array(
  21. 'type' => 'switch',
  22. 'name' => 'homepageJackettEnabled',
  23. 'label' => 'Enable',
  24. 'value' => $this->config['homepageJackettEnabled']
  25. ),
  26. array(
  27. 'type' => 'select',
  28. 'name' => 'homepageJackettAuth',
  29. 'label' => 'Minimum Authentication',
  30. 'value' => $this->config['homepageJackettAuth'],
  31. 'options' => $this->groupOptions
  32. )
  33. ),
  34. 'Connection' => array(
  35. array(
  36. 'type' => 'input',
  37. 'name' => 'jackettURL',
  38. 'label' => 'URL',
  39. 'value' => $this->config['jackettURL'],
  40. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  41. 'placeholder' => 'http(s)://hostname:port'
  42. ),
  43. array(
  44. 'type' => 'password-alt',
  45. 'name' => 'jackettToken',
  46. 'label' => 'Token',
  47. 'value' => $this->config['jackettToken']
  48. )
  49. ),
  50. 'Options' => array(
  51. array(
  52. 'type' => 'switch',
  53. 'name' => 'homepageJackettBackholeDownload',
  54. 'label' => 'Prefer black hole download',
  55. 'help' => 'Prefer black hole download link instead of direct/magnet download',
  56. 'value' => $this->config['homepageJackettBackholeDownload']
  57. )
  58. ),
  59. )
  60. );
  61. return array_merge($homepageInformation, $homepageSettings);
  62. }
  63. public function jackettHomepagePermissions($key = null)
  64. {
  65. $permissions = [
  66. 'main' => [
  67. 'enabled' => [
  68. 'homepageJackettEnabled'
  69. ],
  70. 'auth' => [
  71. 'homepageJackettAuth'
  72. ],
  73. 'not_empty' => [
  74. 'jackettURL',
  75. 'jackettToken'
  76. ]
  77. ]
  78. ];
  79. if (array_key_exists($key, $permissions)) {
  80. return $permissions[$key];
  81. } elseif ($key == 'all') {
  82. return $permissions;
  83. } else {
  84. return [];
  85. }
  86. }
  87. public function homepageOrderJackett()
  88. {
  89. if ($this->homepageItemPermissions($this->jackettHomepagePermissions('main'))) {
  90. return '
  91. <div id="' . __FUNCTION__ . '">
  92. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Jackett...</h2></div>
  93. <script>
  94. // Jackett
  95. homepageJackett();
  96. // End Jackett
  97. </script>
  98. </div>
  99. ';
  100. }
  101. }
  102. public function searchJackettIndexers($query = null)
  103. {
  104. if (!$this->homepageItemPermissions($this->jackettHomepagePermissions('main'), true)) {
  105. return false;
  106. }
  107. if (!$query) {
  108. $this->setAPIResponse('error', 'Query was not supplied', 422);
  109. return false;
  110. }
  111. $apiURL = $this->qualifyURL($this->config['jackettURL']);
  112. $endpoint = $apiURL . '/api/v2.0/indexers/all/results?apikey=' . $this->config['jackettToken'] . '&Query=' . urlencode($query);
  113. try {
  114. $headers = array();
  115. $options = array('timeout' => 120);
  116. $response = Requests::get($endpoint, $headers, $options);
  117. if ($response->success) {
  118. $apiData = json_decode($response->body, true);
  119. $api['content'] = $apiData;
  120. unset($apiData);
  121. }
  122. } catch (Requests_Exception $e) {
  123. $this->writeLog('error', 'Weather And Air Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  124. $this->setAPIResponse('error', $e->getMessage(), 500);
  125. return false;
  126. };
  127. $api['content'] = isset($api['content']) ? $api['content'] : false;
  128. $this->setAPIResponse('success', null, 200, $api);
  129. return $api;
  130. }
  131. public function performJackettBackHoleDownload($url = null)
  132. {
  133. if (!$this->homepageItemPermissions($this->jackettHomepagePermissions('main'), true)) {
  134. return false;
  135. }
  136. if (!$url) {
  137. $this->setAPIResponse('error', 'URL was not supplied', 422);
  138. return false;
  139. }
  140. $apiURL = $this->qualifyURL($this->config['jackettURL']);
  141. $endpoint = $apiURL . $url;
  142. error_log($endpoint);
  143. try {
  144. $headers = array();
  145. $options = array('timeout' => 120);
  146. $response = Requests::get($endpoint, $headers, $options);
  147. if ($response->success) {
  148. $apiData = json_decode($response->body, true);
  149. $api['content'] = $apiData;
  150. unset($apiData);
  151. }
  152. } catch (Requests_Exception $e) {
  153. $this->writeLog('error', 'Jackett blackhole download failed ' . $e->getMessage(), 'SYSTEM');
  154. $this->setAPIResponse('error', $e->getMessage(), 500);
  155. return false;
  156. };
  157. $api['content'] = isset($api['content']) ? $api['content'] : false;
  158. if ($api['content'] && $api['content']['result'] == 'success') {
  159. $this->setAPIResponse('success', null, 200, $api);
  160. } else if ($api['content']) {
  161. $this->setAPIResponse('error', $api['content']['error'], 400, $api);
  162. } else {
  163. $this->setAPIResponse('error', 'Unknown error', 400, $api);
  164. }
  165. return $api;
  166. }
  167. }