jdownloader.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. trait JDownloaderHomepageItem
  3. {
  4. public function jDownloaderSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'JDownloader',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/jdownloader.png',
  10. 'category' => 'Downloader',
  11. 'settings' => array(
  12. 'custom' => '
  13. <div class="row">
  14. <div class="col-lg-12">
  15. <div class="panel panel-info">
  16. <div class="panel-heading">
  17. <span lang="en">Notice</span>
  18. </div>
  19. <div class="panel-wrapper collapse in" aria-expanded="true">
  20. <div class="panel-body">
  21. <ul class="list-icons">
  22. <li><i class="fa fa-chevron-right text-danger"></i> <a href="https://pypi.org/project/myjd-api/" target="_blank">Download [myjd-api] Module</a></li>
  23. <li><i class="fa fa-chevron-right text-danger"></i> Add <b>/api/myjd</b> to the URL if you are using <a href="https://pypi.org/project/RSScrawler/" target="_blank">RSScrawler</a></li>
  24. </ul>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. ',
  31. 'Enable' => array(
  32. array(
  33. 'type' => 'switch',
  34. 'name' => 'homepageJdownloaderEnabled',
  35. 'label' => 'Enable',
  36. 'value' => $this->config['homepageJdownloaderEnabled']
  37. ),
  38. array(
  39. 'type' => 'select',
  40. 'name' => 'homepageJdownloaderAuth',
  41. 'label' => 'Minimum Authentication',
  42. 'value' => $this->config['homepageJdownloaderAuth'],
  43. 'options' => $this->groupOptions
  44. )
  45. ),
  46. 'Connection' => array(
  47. array(
  48. 'type' => 'input',
  49. 'name' => 'jdownloaderURL',
  50. 'label' => 'URL',
  51. 'value' => $this->config['jdownloaderURL'],
  52. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  53. 'placeholder' => 'http(s)://hostname:port'
  54. )
  55. ),
  56. 'Misc Options' => array(
  57. array(
  58. 'type' => 'select',
  59. 'name' => 'homepageDownloadRefresh',
  60. 'label' => 'Refresh Seconds',
  61. 'value' => $this->config['homepageDownloadRefresh'],
  62. 'options' => $this->timeOptions()
  63. ),
  64. array(
  65. 'type' => 'switch',
  66. 'name' => 'jdownloaderCombine',
  67. 'label' => 'Add to Combined Downloader',
  68. 'value' => $this->config['jdownloaderCombine']
  69. ),
  70. ),
  71. 'Test Connection' => array(
  72. array(
  73. 'type' => 'blank',
  74. 'label' => 'Please Save before Testing'
  75. ),
  76. array(
  77. 'type' => 'button',
  78. 'label' => '',
  79. 'icon' => 'fa fa-flask',
  80. 'class' => 'pull-right',
  81. 'text' => 'Test Connection',
  82. 'attr' => 'onclick="testAPIConnection(\'jdownloader\')"'
  83. ),
  84. )
  85. )
  86. );
  87. }
  88. public function testConnectionJDownloader()
  89. {
  90. if (empty($this->config['jdownloaderURL'])) {
  91. $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
  92. return false;
  93. }
  94. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  95. try {
  96. $options = $this->requestOptions($this->config['jdownloaderURL'], false, $this->config['homepageDownloadRefresh']);
  97. $response = Requests::get($url, array(), $options);
  98. if ($response->success) {
  99. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  100. return true;
  101. } else {
  102. $this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
  103. return false;
  104. }
  105. } catch (Requests_Exception $e) {
  106. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  107. $this->setAPIResponse('error', $e->getMessage(), 500);
  108. return false;
  109. };
  110. }
  111. public function jDownloaderHomepagePermissions($key = null)
  112. {
  113. $permissions = [
  114. 'main' => [
  115. 'enabled' => [
  116. 'homepageJdownloaderEnabled'
  117. ],
  118. 'auth' => [
  119. 'homepageJdownloaderAuth'
  120. ],
  121. 'not_empty' => [
  122. 'jdownloaderURL'
  123. ]
  124. ]
  125. ];
  126. if (array_key_exists($key, $permissions)) {
  127. return $permissions[$key];
  128. } elseif ($key == 'all') {
  129. return $permissions;
  130. } else {
  131. return [];
  132. }
  133. }
  134. public function homepageOrderjdownloader()
  135. {
  136. if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
  137. $loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  138. $builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
  139. return '
  140. <div id="' . __FUNCTION__ . '">
  141. ' . $loadingBox . '
  142. <script>
  143. // homepageOrderjdownloader
  144. ' . $builder . '
  145. homepageDownloader("jdownloader", "' . $this->config['homepageDownloadRefresh'] . '");
  146. // End homepageOrderjdownloader
  147. </script>
  148. </div>
  149. ';
  150. }
  151. }
  152. public function getJdownloaderHomepageQueue()
  153. {
  154. if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
  155. return false;
  156. }
  157. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  158. try {
  159. $options = $this->requestOptions($this->config['jdownloaderURL'], false, $this->config['homepageDownloadRefresh']);
  160. $response = Requests::get($url, array(), $options);
  161. if ($response->success) {
  162. $temp = json_decode($response->body, true);
  163. $packages = $temp['packages'];
  164. if ($packages['downloader']) {
  165. $api['content']['queueItems'] = $packages['downloader'];
  166. } else {
  167. $api['content']['queueItems'] = [];
  168. }
  169. if ($packages['linkgrabber_decrypted']) {
  170. $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
  171. } else {
  172. $api['content']['grabberItems'] = [];
  173. }
  174. if ($packages['linkgrabber_failed']) {
  175. $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
  176. } else {
  177. $api['content']['encryptedItems'] = [];
  178. }
  179. if ($packages['linkgrabber_offline']) {
  180. $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
  181. } else {
  182. $api['content']['offlineItems'] = [];
  183. }
  184. $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
  185. }
  186. } catch (Requests_Exception $e) {
  187. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  188. $this->setAPIResponse('error', $e->getMessage(), 500);
  189. return false;
  190. };
  191. $api['content'] = isset($api['content']) ? $api['content'] : false;
  192. $this->setAPIResponse('success', null, 200, $api);
  193. return $api;
  194. }
  195. }