deluge.php 7.3 KB

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