deluge.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. trait DelugeHomepageItem
  3. {
  4. public function delugeSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Deluge',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/deluge.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://github.com/idlesign/deluge-webapi/tree/master/dist" target="_blank">Download Plugin</a></li>
  23. <li><i class="fa fa-chevron-right text-danger"></i> Open Deluge Web UI, go to "Preferences -> Plugins -> Install plugin" and choose egg file.</li>
  24. <li><i class="fa fa-chevron-right text-danger"></i> Activate WebAPI plugin </li>
  25. </ul>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. ',
  32. 'Enable' => array(
  33. array(
  34. 'type' => 'switch',
  35. 'name' => 'homepageDelugeEnabled',
  36. 'label' => 'Enable',
  37. 'value' => $this->config['homepageDelugeEnabled']
  38. ),
  39. array(
  40. 'type' => 'select',
  41. 'name' => 'homepageDelugeAuth',
  42. 'label' => 'Minimum Authentication',
  43. 'value' => $this->config['homepageDelugeAuth'],
  44. 'options' => $this->groupOptions
  45. )
  46. ),
  47. 'Connection' => array(
  48. array(
  49. 'type' => 'input',
  50. 'name' => 'delugeURL',
  51. 'label' => 'URL',
  52. 'value' => $this->config['delugeURL'],
  53. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  54. 'placeholder' => 'http(s)://hostname:port'
  55. ),
  56. array(
  57. 'type' => 'password',
  58. 'name' => 'delugePassword',
  59. 'label' => 'Password',
  60. 'value' => $this->config['delugePassword']
  61. )
  62. ),
  63. 'Misc Options' => array(
  64. array(
  65. 'type' => 'switch',
  66. 'name' => 'delugeHideSeeding',
  67. 'label' => 'Hide Seeding',
  68. 'value' => $this->config['delugeHideSeeding']
  69. ), array(
  70. 'type' => 'switch',
  71. 'name' => 'delugeHideCompleted',
  72. 'label' => 'Hide Completed',
  73. 'value' => $this->config['delugeHideCompleted']
  74. ),
  75. array(
  76. 'type' => 'select',
  77. 'name' => 'homepageDownloadRefresh',
  78. 'label' => 'Refresh Seconds',
  79. 'value' => $this->config['homepageDownloadRefresh'],
  80. 'options' => $this->timeOptions()
  81. ),
  82. array(
  83. 'type' => 'switch',
  84. 'name' => 'delugeCombine',
  85. 'label' => 'Add to Combined Downloader',
  86. 'value' => $this->config['delugeCombine']
  87. ),
  88. ),
  89. 'Test Connection' => array(
  90. array(
  91. 'type' => 'blank',
  92. 'label' => 'Please Save before Testing'
  93. ),
  94. array(
  95. 'type' => 'button',
  96. 'label' => '',
  97. 'icon' => 'fa fa-flask',
  98. 'class' => 'pull-right',
  99. 'text' => 'Test Connection',
  100. 'attr' => 'onclick="testAPIConnection(\'deluge\')"'
  101. ),
  102. )
  103. )
  104. );
  105. }
  106. public function testConnectionDeluge()
  107. {
  108. if (empty($this->config['delugeURL'])) {
  109. $this->setAPIResponse('error', 'Deluge URL is not defined', 422);
  110. return false;
  111. }
  112. if (empty($this->config['delugePassword'])) {
  113. $this->setAPIResponse('error', 'Deluge Password is not defined', 422);
  114. return false;
  115. }
  116. try {
  117. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']));
  118. $torrents = $deluge->getTorrents(null, 'comment, download_payload_rate, eta, hash, is_finished, is_seed, message, name, paused, progress, queue, state, total_size, upload_payload_rate');
  119. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  120. return true;
  121. } catch (Exception $e) {
  122. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  123. $this->setAPIResponse('error', $e->getMessage(), 500);
  124. return false;
  125. }
  126. }
  127. public function delugeHomepagePermissions($key = null)
  128. {
  129. $permissions = [
  130. 'main' => [
  131. 'enabled' => [
  132. 'homepageDelugeEnabled'
  133. ],
  134. 'auth' => [
  135. 'homepageDelugeAuth'
  136. ],
  137. 'not_empty' => [
  138. 'delugeURL',
  139. 'delugePassword'
  140. ]
  141. ]
  142. ];
  143. if (array_key_exists($key, $permissions)) {
  144. return $permissions[$key];
  145. } elseif ($key == 'all') {
  146. return $permissions;
  147. } else {
  148. return [];
  149. }
  150. }
  151. public function homepageOrderdeluge()
  152. {
  153. if ($this->homepageItemPermissions($this->delugeHomepagePermissions('main'))) {
  154. $loadingBox = ($this->config['delugeCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  155. $builder = ($this->config['delugeCombine']) ? 'buildDownloaderCombined(\'deluge\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("deluge"));';
  156. return '
  157. <div id="' . __FUNCTION__ . '">
  158. ' . $loadingBox . '
  159. <script>
  160. // homepageOrderdeluge
  161. ' . $builder . '
  162. homepageDownloader("deluge", "' . $this->config['homepageDownloadRefresh'] . '");
  163. // End homepageOrderdeluge
  164. </script>
  165. </div>
  166. ';
  167. }
  168. }
  169. public function getDelugeHomepageQueue()
  170. {
  171. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  172. return false;
  173. }
  174. try {
  175. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']));
  176. $torrents = $deluge->getTorrents(null, 'comment, download_payload_rate, eta, hash, is_finished, is_seed, message, name, paused, progress, queue, state, total_size, upload_payload_rate');
  177. foreach ($torrents as $key => $value) {
  178. $tempStatus = $this->delugeStatus($value->queue, $value->state, $value->progress);
  179. if ($tempStatus == 'Seeding' && $this->config['delugeHideSeeding']) {
  180. //do nothing
  181. } elseif ($tempStatus == 'Finished' && $this->config['delugeHideCompleted']) {
  182. //do nothing
  183. } else {
  184. $api['content']['queueItems'][] = $value;
  185. }
  186. }
  187. $api['content']['queueItems'] = (empty($api['content']['queueItems'])) ? [] : $api['content']['queueItems'];
  188. $api['content']['historyItems'] = false;
  189. } catch (Excecption $e) {
  190. $this->writeLog('error', 'Deluge Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  191. $this->setAPIResponse('error', $e->getMessage(), 500);
  192. return false;
  193. }
  194. $api['content'] = isset($api['content']) ? $api['content'] : false;
  195. $this->setAPIResponse('success', null, 200, $api);
  196. return $api;
  197. }
  198. public function delugeStatus($queued, $status, $state)
  199. {
  200. if ($queued == '-1' && $state == '100' && ($status == 'Seeding' || $status == 'Queued' || $status == 'Paused')) {
  201. $state = 'Seeding';
  202. } elseif ($state !== '100') {
  203. $state = 'Downloading';
  204. } else {
  205. $state = 'Finished';
  206. }
  207. return ($state) ? $state : $status;
  208. }
  209. }