From de4d1756038624f90930e7dfed36802d884d7a6e Mon Sep 17 00:00:00 2001 From: Chandler Bachman Date: Thu, 4 Nov 2021 21:10:04 -0700 Subject: [PATCH 1/3] Add client_path --- .gitignore | 1 + README.rst | 2 ++ autotorrent/at.py | 7 +++++-- autotorrent/cmd.py | 4 +++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index dd3ffcd..c1fbfd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea *.mo *.egg-info *.egg diff --git a/README.rst b/README.rst index 0234b4b..b1dc28e 100644 --- a/README.rst +++ b/README.rst @@ -91,6 +91,8 @@ general - ``db`` - Path to the database file - ``store_path`` - Folder where the virtual folders seeded, resides +- ``client_path`` - Folder where the virtual folders seeded, resides + inside the torrent client. Defaults to `store_path` - ``ignore_files`` - A comma seperated list of files that should be ignored (supports wildcards) - ``add_limit_size`` - Max size, in bytes, the total torrent size is diff --git a/autotorrent/at.py b/autotorrent/at.py index 8c502fd..b8f5335 100644 --- a/autotorrent/at.py +++ b/autotorrent/at.py @@ -54,10 +54,11 @@ class IllegalPathException(Exception): pass class AutoTorrent(object): - def __init__(self, db, client, store_path, add_limit_size, add_limit_percent, delete_torrents, link_type='soft'): + def __init__(self, db, client, store_path, client_path, add_limit_size, add_limit_percent, delete_torrents, link_type='soft'): self.db = db self.client = client self.store_path = store_path + self.client_path = client_path self.add_limit_size = add_limit_size self.add_limit_percent = add_limit_percent self.delete_torrents = delete_torrents @@ -525,6 +526,7 @@ def handle_torrentfile(self, path, dry_run=False): if files['mode'] == 'link' or files['mode'] == 'hash': logger.info('Preparing torrent using link mode') destination_path = os.path.join(self.store_path, os.path.splitext(os.path.basename(path))[0]) + client_path = os.path.join(self.client_path, os.path.splitext(os.path.basename(path))[0]) if os.path.isdir(destination_path): logger.info('Folder exist but torrent is not seeded %s' % destination_path) @@ -535,6 +537,7 @@ def handle_torrentfile(self, path, dry_run=False): elif files['mode'] == 'exact': logger.info('Preparing torrent using exact mode') destination_path = files['source_path'] + client_path = files['source_path'] fast_resume = True if files['mode'] == 'hash': @@ -546,7 +549,7 @@ def handle_torrentfile(self, path, dry_run=False): logger.info('Removing torrent %r' % path) os.remove(path) - if self.client.add_torrent(torrent, destination_path, files['files'], fast_resume): + if self.client.add_torrent(torrent, client_path, files['files'], fast_resume): self.print_status(Status.OK, path, 'Torrent added successfully') return Status.OK else: diff --git a/autotorrent/cmd.py b/autotorrent/cmd.py index b8670e5..5b3e8b7 100644 --- a/autotorrent/cmd.py +++ b/autotorrent/cmd.py @@ -170,11 +170,13 @@ def commandline_handler(): client_options = dict(config.items(client_option)) client_options.pop('client') client = TORRENT_CLIENTS[client_name](**client_options) + store_path = config.get('general', 'store_path') at = AutoTorrent( db, client, - config.get('general', 'store_path'), + store_path, + (config.get('general', 'client_path') if config.has_option('general', 'client_path') else store_path), config.getint('general', 'add_limit_size'), config.getfloat('general', 'add_limit_percent'), args.delete_torrents, From 20ef9c76fc98d67bf1517d887461c4448257caad Mon Sep 17 00:00:00 2001 From: Chandler Bachman Date: Sat, 6 Nov 2021 10:42:07 -0700 Subject: [PATCH 2/3] Fix rtorrent --- autotorrent/at.py | 2 +- autotorrent/clients/_base.py | 2 +- autotorrent/clients/deluge.py | 2 +- autotorrent/clients/qbittorrent.py | 2 +- autotorrent/clients/rtorrent.py | 6 +++--- autotorrent/clients/transmission.py | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/autotorrent/at.py b/autotorrent/at.py index b8f5335..c2b8484 100644 --- a/autotorrent/at.py +++ b/autotorrent/at.py @@ -549,7 +549,7 @@ def handle_torrentfile(self, path, dry_run=False): logger.info('Removing torrent %r' % path) os.remove(path) - if self.client.add_torrent(torrent, client_path, files['files'], fast_resume): + if self.client.add_torrent(torrent, client_path, destination_path, files['files'], fast_resume): self.print_status(Status.OK, path, 'Torrent added successfully') return Status.OK else: diff --git a/autotorrent/clients/_base.py b/autotorrent/clients/_base.py index 7158272..db8a0ba 100644 --- a/autotorrent/clients/_base.py +++ b/autotorrent/clients/_base.py @@ -30,7 +30,7 @@ def get_torrents(self): """ raise NotImplementedError - def add_torrent(self, torrent, destination_path, files, fast_resume=True): + def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=True): """ Adds a torrent to the torrent client. """ diff --git a/autotorrent/clients/deluge.py b/autotorrent/clients/deluge.py index 80904da..82a2838 100644 --- a/autotorrent/clients/deluge.py +++ b/autotorrent/clients/deluge.py @@ -116,7 +116,7 @@ def get_torrents(self): result = self.rpcclient.call('core.get_torrents_status', {}, ['name']) return set(x.lower() for x in result.keys()) - def add_torrent(self, torrent, destination_path, files, fast_resume=True): + def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=True): """ Add a new torrent to Deluge. diff --git a/autotorrent/clients/qbittorrent.py b/autotorrent/clients/qbittorrent.py index f797688..eb7236e 100644 --- a/autotorrent/clients/qbittorrent.py +++ b/autotorrent/clients/qbittorrent.py @@ -79,7 +79,7 @@ def get_torrents(self): self._login_check() return set(torrent['hash'].lower() for torrent in self._session.get(urljoin(self.url, 'api/v2/torrents/info')).json()) - def add_torrent(self, torrent, destination_path, files, fast_resume=True): + def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=True): """ Add a new torrent to qBittorrent. diff --git a/autotorrent/clients/rtorrent.py b/autotorrent/clients/rtorrent.py index d64a3c2..badabc3 100644 --- a/autotorrent/clients/rtorrent.py +++ b/autotorrent/clients/rtorrent.py @@ -124,7 +124,7 @@ def get_torrents(self): def _get_mtime(self, path): return int(os.stat(path).st_mtime) - def add_torrent(self, torrent, destination_path, files, fast_resume=True): + def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=True): """ Add a new torrent to rtorrent. @@ -151,7 +151,7 @@ def add_torrent(self, torrent, destination_path, files, fast_resume=True): result = {b'priority': 1, b'completed': int(f['completed'])} if f['completed']: - result[b'mtime'] = self._get_mtime(os.path.join(destination_path, *f['path'])) + result[b'mtime'] = self._get_mtime(os.path.join(file_path, *f['path'])) torrent[b'libtorrent_resume'][b'files'].append(result) last_position = current_position + f['length'] @@ -172,7 +172,7 @@ def add_torrent(self, torrent, destination_path, files, fast_resume=True): logger.info('This torrent is incomplete, setting bitfield') torrent[b'libtorrent_resume'][b'bitfield'] = bitfield_to_string(bitfield) - torrent_file = os.path.join(destination_path, '__tmp_torrent%s.torrent' % uuid.uuid4()) + torrent_file = os.path.join(file_path, '__tmp_torrent%s.torrent' % uuid.uuid4()) with open(torrent_file, 'wb') as f: f.write(bencode(torrent)) diff --git a/autotorrent/clients/transmission.py b/autotorrent/clients/transmission.py index eaf0669..f653654 100644 --- a/autotorrent/clients/transmission.py +++ b/autotorrent/clients/transmission.py @@ -117,7 +117,7 @@ def get_torrents(self): result = self.call('torrent-get', fields=['hashString']) return set(x['hashString'].lower() for x in result['torrents']) - def add_torrent(self, torrent, destination_path, files, fast_resume=True): + def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=True): """ Add a new torrent to Transmission. From 812d89e70681897254f09fe50e3c5960c9dd5bd5 Mon Sep 17 00:00:00 2001 From: Chandler Bachman Date: Sat, 6 Nov 2021 10:55:52 -0700 Subject: [PATCH 3/3] Fix rtorrent filepath --- autotorrent/clients/rtorrent.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autotorrent/clients/rtorrent.py b/autotorrent/clients/rtorrent.py index badabc3..ea8429d 100644 --- a/autotorrent/clients/rtorrent.py +++ b/autotorrent/clients/rtorrent.py @@ -172,19 +172,19 @@ def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=T logger.info('This torrent is incomplete, setting bitfield') torrent[b'libtorrent_resume'][b'bitfield'] = bitfield_to_string(bitfield) - torrent_file = os.path.join(file_path, '__tmp_torrent%s.torrent' % uuid.uuid4()) - with open(torrent_file, 'wb') as f: + torrent_file = '__tmp_torrent%s.torrent' % uuid.uuid4() + with open(os.path.join(file_path, torrent_file), 'wb') as f: f.write(bencode(torrent)) infohash = hashlib.sha1(bencode(torrent[b'info'])).hexdigest() if 'load.start' in self.get_methods(): - cmd = [torrent_file, 'd.directory_base.set="%s"' % os.path.abspath(destination_path)] + cmd = [os.path.join(destination_path, torrent_file), 'd.directory_base.set="%s"' % os.path.abspath(destination_path)] cmd.append('d.custom1.set=%s' % quote(self.label)) logger.info('Sending to rtorrent: %r' % cmd) self.proxy.load.start('', *cmd) else: - cmd = [torrent_file, 'd.set_directory_base="%s"' % os.path.abspath(destination_path)] + cmd = [os.path.join(destination_path, torrent_file), 'd.set_directory_base="%s"' % os.path.abspath(destination_path)] cmd.append('d.set_custom1=%s' % quote(self.label)) logger.info('Sending to rtorrent: %r' % cmd) self.proxy.load_start(*cmd) @@ -199,6 +199,6 @@ def add_torrent(self, torrent, destination_path, file_path, files, fast_resume=T else: logger.warning('Torrent was not added to rtorrent within reasonable timelimit') - os.remove(torrent_file) + os.remove(os.path.join(file_path, torrent_file)) return successful