download_file now returns the filename of the downloaded file

when the deferred is successfully fired.
Updated the docstrings in HTTPDownloader.
This commit is contained in:
John Garland 2009-07-25 03:56:33 +00:00
parent 2731d5be17
commit ac28a01e0b
4 changed files with 26 additions and 17 deletions

View file

@ -267,7 +267,7 @@ class Core(component.Component):
:returns: a Deferred which returns the torrent_id as a str or None
"""
log.info("Attempting to add url %s", url)
def on_get_file(result):
def on_get_file(filename):
# We got the file, so add it to the session
data = open(filename, "rb").read()
return self.add_torrent_file(filename, base64.encodestring(data), options)
@ -278,8 +278,7 @@ class Core(component.Component):
log.error("Reason: %s", failure.getErrorMessage())
return failure
filename = url.split("/")[-1]
d = download_file(url, filename, headers=headers)
d = download_file(url, url.split("/")[-1], headers=headers)
d.addCallback(on_get_file)
d.addErrback(on_get_file_error)
return d

View file

@ -43,14 +43,19 @@ class HTTPDownloader(client.HTTPDownloader):
"""
def __init__(self, url, filename, part_callback=None, headers=None):
"""
:param url: str, the url to download from
:param filename: str, the filename to save the file as
:param part_callback: func, a function to be called when a part of data
:param url: the url to download from
:type url: string
:param filename: the filename to save the file as
:type filename: string
:param part_callback: a function to be called when a part of data
is received, it's signature should be: func(data, current_length, total_length)
:param headers: dict, any optional headers to send
:type part_callback: function
:param headers: any optional headers to send
:type headers: dictionary
"""
self.__part_callback = part_callback
self.current_length = 0
self.value = filename
client.HTTPDownloader.__init__(self, url, filename, headers=headers)
def gotStatus(self, version, status, message):
@ -83,11 +88,19 @@ def download_file(url, filename, callback=None, headers=None):
Downloads a file from a specific URL and returns a Deferred. You can also
specify a callback function to be called as parts are received.
:param url: str, the url to download from
:param filename: str, the filename to save the file as
:param callback: func, a function to be called when a part of data is received,
:param url: the url to download from
:type url: string
:param filename: the filename to save the file as
:type filename: string
:param callback: a function to be called when a part of data is received,
it's signature should be: func(data, current_length, total_length)
:param headers: dict, any optional headers to send
:type callback: function
:param headers: any optional headers to send
:type headers: dictionary
:returns: the filename of the downloaded file
:rtype: Deferred
:raises t.w.e.PageRedirect: when server responds with a temporary redirect
or permanently moved.
:raises t.w.e.Error: for all other HTTP response errors (besides OK)

View file

@ -636,7 +636,7 @@ class AddTorrentDialog(component.Component):
def on_download_success(result):
log.debug("Download success!")
self.add_from_files([tmp_file])
self.add_from_files([result])
dialog.destroy()
def on_download_fail(result):

View file

@ -456,12 +456,9 @@ class WebApi(JSONComponent):
"""
tmp_file = os.path.join(tempfile.gettempdir(), url.split("/")[-1])
return httpdownloader.download_file(url, tmp_file).addCallback(self._on_torrent_downloaded, tmp_file)
log.debug("filename: %s", tmp_file)
return httpdownloader.download_file(url, tmp_file)
def _on_torrent_downloaded(self, result, filename):
log.debug("filename: %s", filename)
return filename
@export
def get_torrent_info(self, filename):
"""