-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import base64 | ||
import datetime | ||
|
||
from deluge_client import DelugeRPCClient | ||
|
||
|
||
class Client: | ||
def __init__(self, config): | ||
self.client = DelugeRPCClient(config['host'], int(config['port']), | ||
config['username'], config['password']) | ||
|
||
def add_torrent(self, file_name, torrent, timeout=None, **kwargs): | ||
barray = torrent.download_as_bytearray() | ||
filedump = base64.b64encode(barray).decode('utf-8') | ||
self.client.core.add_torrent_file(file_name, filedump, timeout, | ||
**kwargs) | ||
|
||
def get_torrents(self): | ||
"""Yields Torrents objects.""" | ||
yield from map(lambda torrent: Torrent(torrent, self.client), | ||
self.client.core.get_torrents_status({}, ())) | ||
|
||
|
||
class Torrent: | ||
|
||
PROPERTIES = ('name', 'state', 'hash', 'progress', 'eta', 'peers', | ||
'download_payload_rate') | ||
|
||
def __init__(self, torrent_hash, client): | ||
self.torrent = None | ||
self.client = client | ||
self.update(torrent_hash) | ||
|
||
def is_downloading(self) -> bool: | ||
return self.torrent[b'state'] == b'Downloading' | ||
|
||
def update(self, torrent_hash=None): | ||
h = torrent_hash if torrent_hash else self.torrent[b'hash'] | ||
self.torrent = self.client.core.get_torrent_status(h, self.PROPERTIES) | ||
|
||
@property | ||
def download_rate(self) -> int: | ||
return self.torrent[b'download_payload_rate'] | ||
|
||
@property | ||
def eta(self): | ||
return datetime.timedelta(seconds=self.torrent[b'eta']) | ||
|
||
@property | ||
def id(self): | ||
return self.torrent[b'hash'] | ||
|
||
@property | ||
def name(self): | ||
return str(self.torrent[b'name'], 'utf-8') | ||
|
||
@property | ||
def peers_connected(self): | ||
return len(self.torrent[b'peers']) | ||
|
||
@property | ||
def percent_done(self): | ||
return self.torrent[b'progress'] / 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters