jackett.php 4.7 KB

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