jackett.php 4.8 KB

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