deluge.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'FYI' => [
  20. $this->settingsOption('html', null, ['override' => 12, 'html' => '
  21. <div class="row">
  22. <div class="col-lg-12">
  23. <div class="panel panel-info">
  24. <div class="panel-heading">
  25. <span lang="en">Notice</span>
  26. </div>
  27. <div class="panel-wrapper collapse in" aria-expanded="true">
  28. <div class="panel-body">
  29. <ul class="list-icons">
  30. <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>
  31. <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>
  32. <li><i class="fa fa-chevron-right text-danger"></i> Activate WebAPI plugin </li>
  33. </ul>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>']
  39. )
  40. ],
  41. 'Enable' => [
  42. $this->settingsOption('enable', 'homepageDelugeEnabled'),
  43. $this->settingsOption('auth', 'homepageDelugeAuth'),
  44. ],
  45. 'Connection' => [
  46. $this->settingsOption('url', 'delugeURL'),
  47. $this->settingsOption('password', 'delugePassword', ['help' => 'Note that using a blank password might not work correctly.']),
  48. $this->settingsOption('disable-cert-check', 'delugeDisableCertCheck'),
  49. $this->settingsOption('use-custom-certificate', 'delugeUseCustomCertificate'),
  50. ],
  51. 'Misc Options' => [
  52. $this->settingsOption('hide-seeding', 'delugeHideSeeding'),
  53. $this->settingsOption('hide-completed', 'delugeHideCompleted'),
  54. $this->settingsOption('refresh', 'delugeRefresh'),
  55. $this->settingsOption('combine', 'delugeCombine'),
  56. ],
  57. 'Test Connection' => [
  58. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing. Note that using a blank password might not work correctly.']),
  59. $this->settingsOption('test', 'deluge'),
  60. ]
  61. ]
  62. ];
  63. return array_merge($homepageInformation, $homepageSettings);
  64. }
  65. public function testConnectionDeluge()
  66. {
  67. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  68. return false;
  69. }
  70. try {
  71. $options = $this->requestOptions($this->config['delugeURL'], $this->config['delugeRefresh'], $this->config['delugeDisableCertCheck'], $this->config['delugeUseCustomCertificate'], ['organizr_cert' => $this->getCert(), 'custom_cert' => $this->getCustomCert()]);
  72. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']), $options);
  73. $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');
  74. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  75. return true;
  76. } catch (Exception $e) {
  77. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  78. $this->setAPIResponse('error', $e->getMessage(), 500);
  79. return false;
  80. }
  81. }
  82. public function delugeHomepagePermissions($key = null)
  83. {
  84. $permissions = [
  85. 'main' => [
  86. 'enabled' => [
  87. 'homepageDelugeEnabled'
  88. ],
  89. 'auth' => [
  90. 'homepageDelugeAuth'
  91. ],
  92. 'not_empty' => [
  93. 'delugeURL'
  94. ]
  95. ]
  96. ];
  97. return $this->homepageCheckKeyPermissions($key, $permissions);
  98. }
  99. public function homepageOrderdeluge()
  100. {
  101. if ($this->homepageItemPermissions($this->delugeHomepagePermissions('main'))) {
  102. $loadingBox = ($this->config['delugeCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  103. $builder = ($this->config['delugeCombine']) ? 'buildDownloaderCombined(\'deluge\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("deluge"));';
  104. return '
  105. <div id="' . __FUNCTION__ . '">
  106. ' . $loadingBox . '
  107. <script>
  108. // homepageOrderdeluge
  109. ' . $builder . '
  110. homepageDownloader("deluge", "' . $this->config['delugeRefresh'] . '");
  111. // End homepageOrderdeluge
  112. </script>
  113. </div>
  114. ';
  115. }
  116. }
  117. public function getDelugeHomepageQueue()
  118. {
  119. if (!$this->homepageItemPermissions($this->delugeHomepagePermissions('main'), true)) {
  120. return false;
  121. }
  122. try {
  123. $options = $this->requestOptions($this->config['delugeURL'], $this->config['delugeRefresh'], $this->config['delugeDisableCertCheck'], $this->config['delugeUseCustomCertificate'], ['organizr_cert' => $this->getCert(), 'custom_cert' => $this->getCustomCert()]);
  124. $deluge = new deluge($this->config['delugeURL'], $this->decrypt($this->config['delugePassword']),$options);
  125. $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');
  126. foreach ($torrents as $key => $value) {
  127. $tempStatus = $this->delugeStatus($value->queue, $value->state, $value->progress);
  128. if ($tempStatus == 'Seeding' && $this->config['delugeHideSeeding']) {
  129. //do nothing
  130. } elseif ($tempStatus == 'Finished' && $this->config['delugeHideCompleted']) {
  131. //do nothing
  132. } else {
  133. $api['content']['queueItems'][] = $value;
  134. }
  135. }
  136. $api['content']['queueItems'] = (empty($api['content']['queueItems'])) ? [] : $api['content']['queueItems'];
  137. $api['content']['historyItems'] = false;
  138. } catch (Excecption $e) {
  139. $this->writeLog('error', 'Deluge Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  140. $this->setAPIResponse('error', $e->getMessage(), 500);
  141. return false;
  142. }
  143. $api['content'] = isset($api['content']) ? $api['content'] : false;
  144. $this->setAPIResponse('success', null, 200, $api);
  145. return $api;
  146. }
  147. public function delugeStatus($queued, $status, $state)
  148. {
  149. if ($queued == '-1' && $state == '100' && ($status == 'Seeding' || $status == 'Queued' || $status == 'Paused')) {
  150. $state = 'Seeding';
  151. } elseif ($state !== '100') {
  152. $state = 'Downloading';
  153. } else {
  154. $state = 'Finished';
  155. }
  156. return ($state) ? $state : $status;
  157. }
  158. }