deluge.php 7.3 KB

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