jdownloader.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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->localURL($url)) ? array('verify' => false, 'timeout' => 30) : array('timeout' => 30);
  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 getJdownloaderHomepageQueue()
  112. {
  113. if (!$this->config['homepageJdownloaderEnabled']) {
  114. $this->setAPIResponse('error', 'JDownloader homepage item is not enabled', 409);
  115. return false;
  116. }
  117. if (!$this->qualifyRequest($this->config['homepageJdownloaderAuth'])) {
  118. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  119. return false;
  120. }
  121. if (empty($this->config['jdownloaderURL'])) {
  122. $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
  123. return false;
  124. }
  125. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  126. try {
  127. $options = ($this->localURL($url)) ? array('verify' => false, 'timeout' => 30) : array('timeout' => 30);
  128. $response = Requests::get($url, array(), $options);
  129. if ($response->success) {
  130. $temp = json_decode($response->body, true);
  131. $packages = $temp['packages'];
  132. if ($packages['downloader']) {
  133. $api['content']['queueItems'] = $packages['downloader'];
  134. } else {
  135. $api['content']['queueItems'] = [];
  136. }
  137. if ($packages['linkgrabber_decrypted']) {
  138. $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
  139. } else {
  140. $api['content']['grabberItems'] = [];
  141. }
  142. if ($packages['linkgrabber_failed']) {
  143. $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
  144. } else {
  145. $api['content']['encryptedItems'] = [];
  146. }
  147. if ($packages['linkgrabber_offline']) {
  148. $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
  149. } else {
  150. $api['content']['offlineItems'] = [];
  151. }
  152. $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
  153. }
  154. } catch (Requests_Exception $e) {
  155. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  156. $this->setAPIResponse('error', $e->getMessage(), 500);
  157. return false;
  158. };
  159. $api['content'] = isset($api['content']) ? $api['content'] : false;
  160. $this->setAPIResponse('success', null, 200, $api);
  161. return $api;
  162. }
  163. }