allow url from command line - tarka

This commit is contained in:
Marcos Pinto 2007-06-09 23:50:10 +00:00
commit 81a5a8abd7
3 changed files with 61 additions and 30 deletions

View file

@ -46,10 +46,31 @@ else: dbus_imported = True
parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION) parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.PROGRAM_VERSION)
parser.add_option("--tray", dest="tray", help="start Deluge hidden in system tray", parser.add_option("--tray", dest="tray", help="start Deluge hidden in system tray",
metavar="TRAY", action="store_true") 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() (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: if dbus_imported:
bus = dbus.SessionBus() bus = dbus.SessionBus()
@ -58,16 +79,8 @@ if dbus_imported:
interface = None interface = None
if not "org.deluge_torrent.Deluge" in dbus_objects: if not "org.deluge_torrent.Deluge" in dbus_objects:
print "no existing Deluge session" print "no existing Deluge session"
start_deluge()
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)
else: else:
## This connects to the deluge interface ## This connects to the deluge interface
print "create proxy object" print "create proxy object"
@ -75,20 +88,9 @@ if dbus_imported:
print "create iface" print "create iface"
deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge') deluge_iface = dbus.Interface(proxy, 'org.deluge_torrent.Deluge')
print "send to iface" print "send to iface"
for arg in args:
apath = os.path.abspath(arg) add_args(deluge_iface)
if apath.endswith(".torrent"):
deluge_iface.external_add_torrent(apath)
else:
print "Error,", arg, " does not seem to be a .torrent file"
else: else:
print "no existing Deluge session" print "no existing Deluge session"
start_deluge()
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)

View file

@ -876,11 +876,35 @@ class DelugeGTK:
dlg.destroy() dlg.destroy()
if result == 1: if result == 1:
opener = urllib.URLopener() add_torrent_url(url)
filename, headers = opener.retrieve(url)
if filename.endswith(".torrent") or headers["content-type"]=="application/x-bittorrent": def external_add_url(self, url):
self.interactive_add_torrent(filename) 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): def remove_torrent_clicked(self, obj=None):
torrent = self.get_selected_torrent() torrent = self.get_selected_torrent()

View file

@ -50,6 +50,9 @@ if dbus_imported:
@dbus.service.method('org.deluge_torrent.Deluge') @dbus.service.method('org.deluge_torrent.Deluge')
def external_add_torrent(self, torrent_file): def external_add_torrent(self, torrent_file):
self.interface.external_add_torrent(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: else:
# This is a fallback class in case dbus is not available # This is a fallback class in case dbus is not available
class Manager: class Manager:
@ -58,3 +61,5 @@ else:
def external_add_torrent(self, torrent_file): def external_add_torrent(self, torrent_file):
print "I can't do anything with this." print "I can't do anything with this."
def external_add_url(self, url):
print "I can't do anything with this."