mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-03 06:58:42 +00:00
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:
parent
2731d5be17
commit
ac28a01e0b
4 changed files with 26 additions and 17 deletions
|
@ -267,7 +267,7 @@ class Core(component.Component):
|
||||||
:returns: a Deferred which returns the torrent_id as a str or None
|
:returns: a Deferred which returns the torrent_id as a str or None
|
||||||
"""
|
"""
|
||||||
log.info("Attempting to add url %s", url)
|
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
|
# We got the file, so add it to the session
|
||||||
data = open(filename, "rb").read()
|
data = open(filename, "rb").read()
|
||||||
return self.add_torrent_file(filename, base64.encodestring(data), options)
|
return self.add_torrent_file(filename, base64.encodestring(data), options)
|
||||||
|
@ -278,8 +278,7 @@ class Core(component.Component):
|
||||||
log.error("Reason: %s", failure.getErrorMessage())
|
log.error("Reason: %s", failure.getErrorMessage())
|
||||||
return failure
|
return failure
|
||||||
|
|
||||||
filename = url.split("/")[-1]
|
d = download_file(url, url.split("/")[-1], headers=headers)
|
||||||
d = download_file(url, filename, headers=headers)
|
|
||||||
d.addCallback(on_get_file)
|
d.addCallback(on_get_file)
|
||||||
d.addErrback(on_get_file_error)
|
d.addErrback(on_get_file_error)
|
||||||
return d
|
return d
|
||||||
|
|
|
@ -43,14 +43,19 @@ class HTTPDownloader(client.HTTPDownloader):
|
||||||
"""
|
"""
|
||||||
def __init__(self, url, filename, part_callback=None, headers=None):
|
def __init__(self, url, filename, part_callback=None, headers=None):
|
||||||
"""
|
"""
|
||||||
:param url: str, the url to download from
|
:param url: the url to download from
|
||||||
:param filename: str, the filename to save the file as
|
:type url: string
|
||||||
:param part_callback: func, a function to be called when a part of data
|
: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)
|
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.__part_callback = part_callback
|
||||||
self.current_length = 0
|
self.current_length = 0
|
||||||
|
self.value = filename
|
||||||
client.HTTPDownloader.__init__(self, url, filename, headers=headers)
|
client.HTTPDownloader.__init__(self, url, filename, headers=headers)
|
||||||
|
|
||||||
def gotStatus(self, version, status, message):
|
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
|
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.
|
specify a callback function to be called as parts are received.
|
||||||
|
|
||||||
:param url: str, the url to download from
|
:param url: the url to download from
|
||||||
:param filename: str, the filename to save the file as
|
:type url: string
|
||||||
:param callback: func, a function to be called when a part of data is received,
|
: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)
|
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
|
:raises t.w.e.PageRedirect: when server responds with a temporary redirect
|
||||||
or permanently moved.
|
or permanently moved.
|
||||||
:raises t.w.e.Error: for all other HTTP response errors (besides OK)
|
:raises t.w.e.Error: for all other HTTP response errors (besides OK)
|
||||||
|
|
|
@ -636,7 +636,7 @@ class AddTorrentDialog(component.Component):
|
||||||
|
|
||||||
def on_download_success(result):
|
def on_download_success(result):
|
||||||
log.debug("Download success!")
|
log.debug("Download success!")
|
||||||
self.add_from_files([tmp_file])
|
self.add_from_files([result])
|
||||||
dialog.destroy()
|
dialog.destroy()
|
||||||
|
|
||||||
def on_download_fail(result):
|
def on_download_fail(result):
|
||||||
|
|
|
@ -456,12 +456,9 @@ class WebApi(JSONComponent):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tmp_file = os.path.join(tempfile.gettempdir(), url.split("/")[-1])
|
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
|
@export
|
||||||
def get_torrent_info(self, filename):
|
def get_torrent_info(self, filename):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue