|
| 1 | +import time |
| 2 | +from deluge_client import DelugeRPCClient |
| 3 | +from deluge_client.client import DelugeClientException |
| 4 | +from ..torrent import Torrent |
| 5 | +from ..torrentstatus import TorrentStatus |
| 6 | +from ..exception.loginfailure import LoginFailure |
| 7 | +from ..exception.remotefailure import RemoteFailure |
| 8 | + |
| 9 | +# Default port of Delgue |
| 10 | +DEFAULT_PORT = 58846 |
| 11 | + |
| 12 | +class Deluge(object): |
| 13 | + def __init__(self, host): |
| 14 | + # Host |
| 15 | + self._host = host |
| 16 | + # RPC Client |
| 17 | + self._client = None |
| 18 | + # Torrent Properties Cache |
| 19 | + self._torrent_cache = {} |
| 20 | + # Cache Valid Time |
| 21 | + self._refresh_expire_time = 30 |
| 22 | + # Last Time of Refreshing Cache |
| 23 | + self._last_refresh = 0 |
| 24 | + |
| 25 | + # Login to Deluge |
| 26 | + def login(self, username, password): |
| 27 | + # Split IP(or domain name) and port |
| 28 | + splits = self._host.split(':') |
| 29 | + host = splits[0] if len(splits) > 0 else '' |
| 30 | + port = int(splits[1]) if len(splits) > 1 else DEFAULT_PORT |
| 31 | + |
| 32 | + # Create RPC client and connect to Deluge |
| 33 | + self._client = DelugeRPCClient(host, port, username, password, decode_utf8 = True) |
| 34 | + try: |
| 35 | + self._client.connect() |
| 36 | + except DelugeClientException as e: |
| 37 | + # Display class name of the exception if there is no error messages |
| 38 | + raise LoginFailure(e.args[0].split('\n')[0] if len(e.args) > 0 else e.__class__.__name__) |
| 39 | + |
| 40 | + # A caller to call deluge api; includes exception processing |
| 41 | + def _call(self, method, *args, **kwargs): |
| 42 | + try: |
| 43 | + return self._client.call(method, *args, **kwargs) |
| 44 | + except DelugeClientException as e: |
| 45 | + # Raise our own exception |
| 46 | + raise RemoteFailure(e.args[0].split('\n')[0] if len(e.args) > 0 else e.__class__.__name__) |
| 47 | + |
| 48 | + # Get Deluge version |
| 49 | + def version(self): |
| 50 | + funcs = { |
| 51 | + 1: 'daemon.info', # For Deluge 1.x, use daemon.info |
| 52 | + 2: 'daemon.get_version', # For Deluge 2.x, use daemon.get_version |
| 53 | + } |
| 54 | + ver = self._call(funcs[self._client.deluge_version]) |
| 55 | + return ('Deluge %s' % ver) |
| 56 | + |
| 57 | + # Get API version |
| 58 | + def api_version(self): |
| 59 | + # Returns the protocol version |
| 60 | + return self._client.deluge_protocol_version if self._client.deluge_protocol_version is not None else 'not provided' |
| 61 | + |
| 62 | + # Get torrent list |
| 63 | + def torrents_list(self): |
| 64 | + # Save hashes |
| 65 | + torrents_hash = [] |
| 66 | + # Get torrent list (and their properties) |
| 67 | + torrent_list = self._call('core.get_torrents_status', {}, [ |
| 68 | + 'active_time', |
| 69 | + 'all_time_download', |
| 70 | + 'download_payload_rate', |
| 71 | + 'finished_time', |
| 72 | + 'hash', |
| 73 | + 'label', # Available when the plugin 'label' is enabled |
| 74 | + 'name', |
| 75 | + 'num_peers', |
| 76 | + 'num_seeds', |
| 77 | + 'progress', |
| 78 | + 'ratio', |
| 79 | + 'seeding_time', |
| 80 | + 'state', |
| 81 | + 'time_added', |
| 82 | + 'time_since_transfer', |
| 83 | + 'total_peers', |
| 84 | + 'total_seeds', |
| 85 | + 'total_size', |
| 86 | + 'total_uploaded', |
| 87 | + 'trackers', |
| 88 | + 'upload_payload_rate', |
| 89 | + ]) |
| 90 | + # Save properties to cache |
| 91 | + self._torrent_cache = torrent_list |
| 92 | + self._last_refresh = time.time() |
| 93 | + # Return torrent hashes |
| 94 | + for h in torrent_list: |
| 95 | + torrents_hash.append(h) |
| 96 | + return torrents_hash |
| 97 | + |
| 98 | + # Get Torrent Properties |
| 99 | + def torrent_properties(self, torrent_hash): |
| 100 | + # Check cache expiration |
| 101 | + if time.time() - self._last_refresh > self._refresh_expire_time: |
| 102 | + self.torrents_list() |
| 103 | + # Extract properties |
| 104 | + torrent = self._torrent_cache[torrent_hash] |
| 105 | + # Create torrent object |
| 106 | + torrent_obj = Torrent() |
| 107 | + torrent_obj.hash = torrent['hash'] |
| 108 | + torrent_obj.name = torrent['name'] |
| 109 | + if 'label' in torrent: |
| 110 | + torrent_obj.category = [torrent['label']] if len(torrent['label']) > 0 else [] |
| 111 | + torrent_obj.tracker = [tracker['url'] for tracker in torrent['trackers']] |
| 112 | + torrent_obj.status = Deluge._judge_status(torrent['state']) |
| 113 | + torrent_obj.size = torrent['total_size'] |
| 114 | + torrent_obj.ratio = torrent['ratio'] |
| 115 | + torrent_obj.uploaded = torrent['total_uploaded'] |
| 116 | + torrent_obj.create_time = int(torrent['time_added']) |
| 117 | + torrent_obj.seeding_time = torrent['seeding_time'] |
| 118 | + torrent_obj.upload_speed = torrent['upload_payload_rate'] |
| 119 | + torrent_obj.download_speed = torrent['download_payload_rate'] |
| 120 | + torrent_obj.seeder = torrent['total_seeds'] |
| 121 | + torrent_obj.connected_seeder = torrent['num_seeds'] |
| 122 | + torrent_obj.leecher = torrent['total_peers'] |
| 123 | + torrent_obj.connected_leecher = torrent['num_peers'] |
| 124 | + torrent_obj.average_upload_speed = torrent['total_uploaded'] / torrent['active_time'] if torrent['active_time'] > 0 else 0 |
| 125 | + if 'finished_time' in torrent: |
| 126 | + download_time = torrent['active_time'] - torrent['finished_time'] |
| 127 | + torrent_obj.average_download_speed = torrent['all_time_download'] / download_time if download_time > 0 else 0 |
| 128 | + if 'time_since_transfer' in torrent: |
| 129 | + # Set the last active time of those never active torrents to timestamp 0 |
| 130 | + torrent_obj.last_activity = torrent['time_since_transfer'] if torrent['time_since_transfer'] > 0 else 0 |
| 131 | + torrent_obj.progress = torrent['progress'] / 100 # Accept Range: 0-1 |
| 132 | + |
| 133 | + return torrent_obj |
| 134 | + |
| 135 | + # Judge Torrent Status |
| 136 | + @staticmethod |
| 137 | + def _judge_status(state): |
| 138 | + return { |
| 139 | + 'Allocating': TorrentStatus.Unknown, # Ignore this state |
| 140 | + 'Checking': TorrentStatus.Checking, |
| 141 | + 'Downloading': TorrentStatus.Downloading, |
| 142 | + 'Error': TorrentStatus.Error, |
| 143 | + 'Moving': TorrentStatus.Unknown, # Ignore this state |
| 144 | + 'Paused': TorrentStatus.Paused, |
| 145 | + 'Queued': TorrentStatus.Queued, |
| 146 | + 'Seeding': TorrentStatus.Uploading, |
| 147 | + }[state] |
| 148 | + |
| 149 | + # Batch Remove Torrents |
| 150 | + def remove_torrents(self, torrent_hash_list, remove_data): |
| 151 | + if self._client.deluge_version >= 2: # Method 'core.remove_torrents' is only available in Deluge 2.x |
| 152 | + failures = self._call('core.remove_torrents', torrent_hash_list, remove_data) |
| 153 | + failed_hash = [torrent[0] for torrent in failures] |
| 154 | + return ( |
| 155 | + [torrent for torrent in torrent_hash_list if torrent not in failed_hash], |
| 156 | + [{ |
| 157 | + 'hash': torrent[0], |
| 158 | + 'reason': torrent[1], |
| 159 | + } for torrent in failures], |
| 160 | + ) |
| 161 | + else: # For Deluge 1.x, remove torrents one by one |
| 162 | + success_hash = [] |
| 163 | + failures = [] |
| 164 | + for torrent in torrent_hash_list: |
| 165 | + try: |
| 166 | + self._call('core.remove_torrent', torrent, remove_data) |
| 167 | + success_hash.append(torrent) |
| 168 | + except RemoteFailure as e: |
| 169 | + failures.append({ |
| 170 | + 'hash': torrent, |
| 171 | + 'reason': e.args[0], |
| 172 | + }) |
| 173 | + return (success_hash, failures) |
0 commit comments