From 81a5a8abd7f9eee7efcc1d3b6bbd5f0f844e4d7c Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sat, 9 Jun 2007 23:50:10 +0000 Subject: [PATCH] allow url from command line - tarka --- scripts/deluge | 52 ++++++++++++++++++++++++---------------------- src/interface.py | 34 +++++++++++++++++++++++++----- src/ipc_manager.py | 5 +++++ 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/scripts/deluge b/scripts/deluge index 3e54196c5..9d94ca487 100755 --- a/scripts/deluge +++ b/scripts/deluge @@ -46,10 +46,31 @@ else: dbus_imported = True parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION) parser.add_option("--tray", dest="tray", help="start Deluge hidden in system tray", metavar="TRAY", action="store_true") +parser.add_option("--url", dest="url", help="Load a torrent from a URL", + metavar="URL", action="store") (options, args) = parser.parse_args() +def start_deluge(): + print "Starting new Deluge session..." + interface = deluge.interface.DelugeGTK() + add_args(interface) + interface.start(hidden=options.tray) + + +def add_args(interface): + if options.url: + interface.external_add_url(options.url) + else: + for arg in args: + apath = os.path.abspath(arg) + if apath.endswith(".torrent"): + interface.external_add_torrent(apath) + else: + print "Error,", arg, " does not seem to be a .torrent file" + + if dbus_imported: bus = dbus.SessionBus() @@ -58,16 +79,8 @@ if dbus_imported: interface = None if not "org.deluge_torrent.Deluge" in dbus_objects: - print "no existing Deluge session" - - interface = deluge.interface.DelugeGTK() - for arg in args: - apath = os.path.abspath(arg) - if apath.endswith(".torrent"): - interface.external_add_torrent(apath) - else: - print "Error,", arg, " does not seem to be a .torrent file" - interface.start(hidden=options.tray) + print "no existing Deluge session" + start_deluge() else: ## This connects to the deluge interface print "create proxy object" @@ -75,20 +88,9 @@ if dbus_imported: print "create iface" deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge') print "send to iface" - for arg in args: - apath = os.path.abspath(arg) - if apath.endswith(".torrent"): - deluge_iface.external_add_torrent(apath) - else: - print "Error,", arg, " does not seem to be a .torrent file" + + add_args(deluge_iface) + else: print "no existing Deluge session" - - interface = deluge.interface.DelugeGTK() - for arg in args: - apath = os.path.abspath(arg) - if apath.endswith(".torrent"): - interface.external_add_torrent(apath) - else: - print "Error,", arg, " does not seem to be a .torrent file" - interface.start(hidden=options.tray) + start_deluge() diff --git a/src/interface.py b/src/interface.py index b97924906..352a1ffcb 100644 --- a/src/interface.py +++ b/src/interface.py @@ -876,11 +876,35 @@ class DelugeGTK: dlg.destroy() if result == 1: - opener = urllib.URLopener() - filename, headers = opener.retrieve(url) - if filename.endswith(".torrent") or headers["content-type"]=="application/x-bittorrent": - self.interactive_add_torrent(filename) - + add_torrent_url(url) + + def external_add_url(self, url): + print "Got URL externally:", url + if self.is_running: + print "\t\tthe client seems to already be running, i'll try and add the URL" + self.add_torrent_url(url) + else: + print "\t\tthe client hasn't started yet, I'll queue the URL torrent file" + self.queue_torrent_url(url) + + def add_torrent_url(self, url): + filename, headers = self.fetch_url(url) + if filename: + self.interactive_add_torrent(filename) + + def queue_torrent_url(self, url): + filename, headers = self.fetch_url(url) + if filename: + self.torrent_file_queue.append(filename) + + def fetch_url(self, url): + filename, headers = urllib.urlretrieve(url) + if filename.endswith(".torrent") or headers["content-type"]=="application/x-bittorrent": + return filename, headers + else: + print "URL doesn't appear to be a valid torrent file:", url + return None, None + def remove_torrent_clicked(self, obj=None): torrent = self.get_selected_torrent() diff --git a/src/ipc_manager.py b/src/ipc_manager.py index 3a5e9fb9f..aed04a6fe 100644 --- a/src/ipc_manager.py +++ b/src/ipc_manager.py @@ -50,6 +50,9 @@ if dbus_imported: @dbus.service.method('org.deluge_torrent.Deluge') def external_add_torrent(self, torrent_file): self.interface.external_add_torrent(torrent_file) + @dbus.service.method('org.deluge_torrent.Deluge') + def external_add_url(self, url): + self.interface.external_add_url(url) else: # This is a fallback class in case dbus is not available class Manager: @@ -58,3 +61,5 @@ else: def external_add_torrent(self, torrent_file): print "I can't do anything with this." + def external_add_url(self, url): + print "I can't do anything with this."