diff --git a/clients/rtorrent.py b/clients/rtorrent.py new file mode 100644 index 0000000000000000000000000000000000000000..b199e9f50d2acb8f6fd4ef48430a672644cf89ea --- /dev/null +++ b/clients/rtorrent.py @@ -0,0 +1,58 @@ +from rtorrent import RTorrent +from clients.torrentclient import TorrentClient +import sys +from urllib.parse import urlsplit + + +class rTorrentClient(TorrentClient): + + def __init__(self, logger, username=None, password=None, url=None, hostname=None): + + TorrentClient.__init__(self, logger, username=username, password=password, url=url, hostname=hostname) + self.torrent_client = 'rTorrent' + self._authenticate() + self.rtorrent = None + + self._authenticate() + + def _authenticate(self): + """ + Setup connection to rTorrent XMLRPC server + :return: + """ + + try: + self.rtorrent = RTorrent(self.url) + except ConnectionRefusedError as e: + self.send_log('Failed to connect to rTorrent. Aborting', 'critical') + sys.exit(1) + + self.send_log('Successfully connected to rTorrent', 'info') + + def _build_torrent_list(self, torrents): + """ + Take the resulting torrent list and create a consistent structure shared through all clients + :return: + """ + self.send_log('Structuring list of torrents', 'debug') + + for torrent in torrents: + self.torrent_list[torrent.info_hash] = {} + self.torrent_list[torrent.info_hash]['name'] = torrent.name + self.torrent_list[torrent.info_hash]['total_size'] = torrent.size_bytes + self.torrent_list[torrent.info_hash]['progress'] = round((torrent.bytes_done / torrent.size_bytes * 100), 2) + self.torrent_list[torrent.info_hash]['total_downloaded'] = torrent.bytes_done + self.torrent_list[torrent.info_hash]['total_uploaded'] = 1 # TODO Need to figure out where to get this + self.torrent_list[torrent.info_hash]['ratio'] = torrent.ratio + self.torrent_list[torrent.info_hash]['total_seeds'] = 'N/A' + self.torrent_list[torrent.info_hash]['state'] = torrent.get_state() + self.torrent_list[torrent.info_hash]['tracker'] = urlsplit(torrent.get_trackers()[0].url).netloc + self.torrent_list[torrent.info_hash]['total_files'] = torrent.size_files + + + def get_all_torrents(self): + """ + Return list of all torrents + :return: + """ + self._build_torrent_list(self.rtorrent.torrents) diff --git a/clients/utorrent.py b/clients/utorrent.py index 1d30a2344b180d6c314868ba963fc695c872c3a4..d98f2bbd3f18817a06f181de050859092f545f8c 100644 --- a/clients/utorrent.py +++ b/clients/utorrent.py @@ -68,8 +68,7 @@ class UTorrentClient(TorrentClient): :return: """ - msg = 'Structuring list of torrents' - self.send_log(msg, 'debug') + self.send_log('Structuring list of torrents', 'debug') for torrent in torrents: self.torrent_list[torrent[0]] = {} diff --git a/config.ini b/config.ini index 0c12b4ece8b8893207d5b8a16a6814a2498d8b52..45900ba93e1829273e84d4ecbc70cc0da9ce2c19 100644 --- a/config.ini +++ b/config.ini @@ -15,7 +15,8 @@ Verify_SSL = False [TORRENTCLIENT] # Leave blank to auto pick server -# Valid options are deluge, utorrent +# Valid Options: deluge, utorrent, rtorrent +# Deluge only needs password, uTorrent needs user and password, rtorrent needs neither Client = utorrent Username = admin Password = @@ -26,12 +27,9 @@ Url = [LOGGING] Enable = True - # Valid Options: critical, error, warning, info, debug Level = error - LogFile = output.log - # Removes things such as server names and ip addresses from logs CensorLogs = False @@ -42,4 +40,4 @@ CensorLogs = False # WARNING: 2 # ERROR: 3 # CRITICAL: 4 -PrintThreshold = 0 \ No newline at end of file +PrintThreshold = 2 \ No newline at end of file diff --git a/influxdbSeedbox.py b/influxdbSeedbox.py index 23ea24eb5a732bbe5b9aa0cb60ec61813da4a922..7ee02b5066475b0620205a5c936e463bd97434ee 100644 --- a/influxdbSeedbox.py +++ b/influxdbSeedbox.py @@ -13,6 +13,7 @@ from influxdb.exceptions import InfluxDBClientError, InfluxDBServerError # TODO Move urlopen login in each method call to one central method # TODO Validate that we get a valid URL from config +# TODO Deal with multiple trackers per torrent __author__ = 'barry' class configManager(): @@ -21,7 +22,7 @@ class configManager(): def __init__(self, config): - self.valid_torrent_clients = ['deluge', 'utorrent'] + self.valid_torrent_clients = ['deluge', 'utorrent', 'rtorrent'] self.valid_log_levels = { 'DEBUG': 0, 'INFO': 1, @@ -127,6 +128,7 @@ class influxdbSeedbox(): password=self.config.tor_client_password, url=self.config.tor_client_url, hostname=self.config.hostname) + elif self.config.tor_client == 'utorrent': from clients.utorrent import UTorrentClient print('Generating uTorrent Client') @@ -136,6 +138,15 @@ class influxdbSeedbox(): url=self.config.tor_client_url, hostname=self.config.hostname) + elif self.config.tor_client == 'rtorrent': + from clients.rtorrent import rTorrentClient + print('Generating rTorrent Client') + self.tor_client = rTorrentClient(self.send_log, + username=None, + password=None, + url=self.config.tor_client_url, + hostname=self.config.hostname) + def _set_logging(self): """ Create the logger object if enabled in the config @@ -257,6 +268,7 @@ def main(): parser = argparse.ArgumentParser(description="A tool to send Torrent Client statistics to InfluxDB") parser.add_argument('--config', default='config.ini', dest='config', help='Specify a custom location for the config file') + parser.add_argument('--silent', default=False, dest='config', help='Surpress All Output') args = parser.parse_args() monitor = influxdbSeedbox(config=args.config) monitor.run() diff --git a/requirements.txt b/requirements.txt index 7738dea555f7998358da701548fef3bdfaf6acb1..10dcfc8a3cf0c1631bcb0ece39c6fc9bc5c01a82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ influxdb -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +rtorrent-python \ No newline at end of file