Improve Magnet and Url support for add command in console

This commit is contained in:
Calum Lind 2012-03-21 22:35:14 +00:00
parent 61dbd349ab
commit d62da02bae
1 changed files with 17 additions and 18 deletions

View File

@ -44,28 +44,22 @@ import deluge.common
from optparse import make_option from optparse import make_option
import os import os
import base64 import base64
from urllib import url2pathname
from urlparse import urlparse
class Command(BaseCommand): class Command(BaseCommand):
"""Add a torrent""" """Add a torrent"""
option_list = BaseCommand.option_list + ( option_list = BaseCommand.option_list + (
make_option('-p', '--path', dest='path', make_option('-p', '--path', dest='path',
help='save path for torrent'), help='save path for torrent'),
make_option('-u', '--urls', action='store_true', default=False, dest='force_url',
help='Interpret all given torrent-file arguments as URLs'),
make_option('-f', '--files', action='store_true', default=False, dest='force_file',
help='Interpret all given torrent-file arguments as files'),
) )
usage = "Usage: add [-p <save-location>] [-u | --urls] [-f | --files] <torrent-file> [<torrent-file> ...]\n"\ usage = "Usage: add [-p <save-location>] <torrent-file> [<torrent-file> ...]\n"\
" <torrent-file> arguments can be file paths, URLs or magnet uris" " <torrent-file> arguments can be file paths, URLs or magnet uris"
def handle(self, *args, **options): def handle(self, *args, **options):
self.console = component.get("ConsoleUI") self.console = component.get("ConsoleUI")
if options["force_file"] and options["force_url"]:
self.console.write("{!error!}Cannot specify --urls and --files at the same time")
return
t_options = {} t_options = {}
if options["path"]: if options["path"]:
t_options["download_location"] = os.path.expanduser(options["path"]) t_options["download_location"] = os.path.expanduser(options["path"])
@ -78,23 +72,28 @@ class Command(BaseCommand):
# Keep a list of deferreds to make a DeferredList # Keep a list of deferreds to make a DeferredList
deferreds = [] deferreds = []
for arg in args: for arg in args:
if not options["force_file"] and (deluge.common.is_url(arg) or options["force_url"]): if not arg.strip():
continue
if deluge.common.is_url(arg):
self.console.write("{!info!}Attempting to add torrent from url: %s" % arg) self.console.write("{!info!}Attempting to add torrent from url: %s" % arg)
deferreds.append(client.core.add_torrent_url(arg, t_options).addCallback(on_success).addErrback(on_fail)) deferreds.append(client.core.add_torrent_url(arg, t_options).addCallback(on_success).addErrback(on_fail))
elif not options["force_file"] and (deluge.common.is_magnet(arg)): elif deluge.common.is_magnet(arg):
self.console.write("{!info!}Attempting to add torrent from magnet uri: %s" % arg) self.console.write("{!info!}Attempting to add torrent from magnet uri: %s" % arg)
deferreds.append(client.core.add_torrent_magnet(arg, t_options).addCallback(on_success).addErrback(on_fail)) deferreds.append(client.core.add_torrent_magnet(arg, t_options).addCallback(on_success).addErrback(on_fail))
else: else:
if not os.path.exists(arg): # Just a file
self.console.write("{!error!}%s doesn't exist!" % arg) if urlparse(arg).scheme == "file":
arg = url2pathname(urlparse(arg).path)
path = os.path.abspath(os.path.expanduser(arg))
if not os.path.exists(path):
self.console.write("{!error!}%s doesn't exist!" % path)
continue continue
if not os.path.isfile(arg): if not os.path.isfile(path):
self.console.write("{!error!}This is a directory!") self.console.write("{!error!}This is a directory!")
continue continue
self.console.write("{!info!}Attempting to add torrent: %s" % arg) self.console.write("{!info!}Attempting to add torrent: %s" % path)
filename = os.path.split(arg)[-1] filename = os.path.split(path)[-1]
filedump = base64.encodestring(open(arg, "rb").read()) filedump = base64.encodestring(open(path, "rb").read())
deferreds.append(client.core.add_torrent_file(filename, filedump, t_options).addCallback(on_success).addErrback(on_fail)) deferreds.append(client.core.add_torrent_file(filename, filedump, t_options).addCallback(on_success).addErrback(on_fail))
return defer.DeferredList(deferreds) return defer.DeferredList(deferreds)