diff --git a/weboob/backends/gazelle/backend.py b/weboob/backends/gazelle/backend.py index 53969869..3ebfddb4 100644 --- a/weboob/backends/gazelle/backend.py +++ b/weboob/backends/gazelle/backend.py @@ -53,5 +53,12 @@ class GazelleBackend(BaseBackend, ICapTorrent): def get_torrent(self, id): return self.browser.get_torrent(id) + def get_torrent_file(self, id): + torrent = self.browser.get_torrent(id) + if not torrent: + return None + + return self.browser.openurl(torrent.url).read() + def iter_torrents(self, pattern): return self.browser.iter_torrents(pattern) diff --git a/weboob/capabilities/torrent.py b/weboob/capabilities/torrent.py index 56343cc9..be95bbe9 100644 --- a/weboob/capabilities/torrent.py +++ b/weboob/capabilities/torrent.py @@ -41,3 +41,6 @@ class ICapTorrent(ICap): def get_torrent(self, _id): raise NotImplementedError() + + def get_torrent_file(self, _id): + raise NotImplementedError() diff --git a/weboob/frontends/weboorrents/application.py b/weboob/frontends/weboorrents/application.py index 6dc7b4d6..e4182b59 100644 --- a/weboob/frontends/weboorrents/application.py +++ b/weboob/frontends/weboorrents/application.py @@ -35,16 +35,23 @@ class Weboorrents(ConsoleApplication): self.load_backends(ICapTorrent) return self.process_command(*argv[1:]) - @ConsoleApplication.command('Get information about a torrent') - def command_info(self, id): + def split_id(self, id): if not '.' in id: print >>sys.stderr, 'ID must be in form .' - return 1 + return None, None backend_name, id = id.split('.', 1) backend = self.weboob.backends.get(backend_name, None) if not backend: print >>sys.stderr, 'Backends "%s" not found' % backend_name + return None, None + + return backend, id + + @ConsoleApplication.command('Get information about a torrent') + def command_info(self, id): + backend, id = self.split_id(id) + if not backend: return 1 with backend: @@ -66,6 +73,24 @@ class Weboorrents(ConsoleApplication): rows.append(('Files', '\n'.join(torrent.files))) return {backend.name: rows} + @ConsoleApplication.command('Get the torrent file') + def command_getfile(self, id, dest): + backend, id = self.split_id(id) + if not backend: + return 1 + + with backend: + s = backend.get_torrent_file(id) + if not s: + print >>sys.stderr, 'Torrent "%s" not found' % id + return 1 + + if dest == '-': + print s + else: + with open(dest, 'w') as f: + f.write(s) + @ConsoleApplication.command('Search torrents') def command_search(self, pattern=None): results = {}