add_torrent_url now uses httpdownloader's download_file.

This also allows it to send request headers.
As a result it can download torrents from sites that require cookies.
This commit is contained in:
John Garland 2009-07-20 08:47:32 +00:00
parent 3a94a33b56
commit dcccc3f4de
1 changed files with 15 additions and 15 deletions

View File

@ -44,6 +44,7 @@ from twisted.internet import reactor, defer
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
import twisted.web.client import twisted.web.client
from deluge.httpdownloader import download_file
from deluge.log import LOG as log from deluge.log import LOG as log
try: try:
@ -261,35 +262,34 @@ class Core(component.Component):
return torrent_id return torrent_id
@export @export
def add_torrent_url(self, url, options): def add_torrent_url(self, url, options, headers=None):
""" """
Adds a torrent from a url. Deluge will attempt to fetch the torrent Adds a torrent from a url. Deluge will attempt to fetch the torrent
from url prior to adding it to the session. from url prior to adding it to the session.
:param url: str, the url pointing to the torrent file :param url: str, the url pointing to the torrent file
:param options: dict, the options to apply to the torrent on add :param options: dict, the options to apply to the torrent on add
:param headers: dict, any optional headers to send
:returns: the torrent_id as a str or None, if calling locally, then it :returns: the torrent_id as a str or None, if calling locally, then it
will return a Deferred that fires once the torrent has been added will return a Deferred that fires once the torrent has been added
""" """
log.info("Attempting to add url %s", url) log.info("Attempting to add url %s", url)
def on_get_page(page): def on_get_file(result):
# We got the data, so attempt adding it to the session # We got the file, so add it to the session
try: data = open(filename, "rb").read()
torrent_id = self.add_torrent_file(url.split("/")[-1], base64.encodestring(page), options) return self.add_torrent_file(filename, base64.encodestring(data), options)
except Exception, e:
d.errback(e)
else:
d.callback(torrent_id)
def on_get_page_error(reason): def on_get_file_error(failure):
# Log the error and pass the failure onto the client
log.error("Error occured downloading torrent from %s", url) log.error("Error occured downloading torrent from %s", url)
log.error("Reason: %s", reason) log.error("Reason: %s", failure.getErrorMessage())
# XXX: Probably should raise an exception to the client here return failure
d.errback(reason)
twisted.web.client.getPage(url).addCallback(on_get_page).addErrback(on_get_page_error) filename = url.split("/")[-1]
d = defer.Deferred() d = download_file(url, filename, headers=headers)
d.addCallback(on_get_file)
d.addErrback(on_get_file_error)
return d return d
@export @export