allow url from command line - tarka

This commit is contained in:
Marcos Pinto 2007-06-09 23:50:10 +00:00
parent e77a3318da
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.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()

View File

@ -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()

View File

@ -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."