summaryrefslogtreecommitdiffstats
path: root/module/network/HTTPDownload.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/network/HTTPDownload.py')
-rw-r--r--module/network/HTTPDownload.py44
1 files changed, 21 insertions, 23 deletions
diff --git a/module/network/HTTPDownload.py b/module/network/HTTPDownload.py
index fe8075539..498cd5979 100644
--- a/module/network/HTTPDownload.py
+++ b/module/network/HTTPDownload.py
@@ -17,9 +17,9 @@
@author: RaNaN
"""
-from os import remove, fsync
+from os import remove
from os.path import dirname
-from time import sleep, time
+from time import time
from shutil import move
from logging import getLogger
@@ -28,14 +28,16 @@ import pycurl
from HTTPChunk import ChunkInfo, HTTPChunk
from HTTPRequest import BadHeader
-from module.plugins.Plugin import Abort
-from module.utils import save_join, fs_encode
+from module.plugins.Base import Abort
+from module.utils.fs import save_join, fs_encode
+
+# TODO: save content-disposition for resuming
class HTTPDownload():
- """ loads a url http + ftp """
+ """ loads an url, http + ftp supported """
def __init__(self, url, filename, get={}, post={}, referer=None, cj=None, bucket=None,
- options={}, progressNotify=None, disposition=False):
+ options={}, disposition=False):
self.url = url
self.filename = filename #complete file destination, not only name
self.get = get
@@ -49,7 +51,7 @@ class HTTPDownload():
self.abort = False
self.size = 0
- self.nameDisposition = None #will be parsed from content disposition
+ self._name = ""# will be parsed from content disposition
self.chunks = []
@@ -71,8 +73,6 @@ class HTTPDownload():
self.speeds = []
self.lastSpeeds = [0, 0]
- self.progressNotify = progressNotify
-
@property
def speed(self):
last = [sum(x) for x in self.lastSpeeds if x]
@@ -87,6 +87,10 @@ class HTTPDownload():
if not self.size: return 0
return (self.arrived * 100) / self.size
+ @property
+ def name(self):
+ return self._name if self.disposition else ""
+
def _copyChunks(self):
init = fs_encode(self.info.getChunkName(0)) #initial chunk name
@@ -113,8 +117,8 @@ class HTTPDownload():
remove(fname) #remove chunk
fo.close()
- if self.nameDisposition and self.disposition:
- self.filename = save_join(dirname(self.filename), self.nameDisposition)
+ if self.name:
+ self.filename = save_join(dirname(self.filename), self.name)
move(init, fs_encode(self.filename))
self.info.remove() #remove info file
@@ -144,8 +148,7 @@ class HTTPDownload():
finally:
self.close()
- if self.nameDisposition and self.disposition: return self.nameDisposition
- return None
+ return self.name
def _download(self, chunks, resume):
if not resume:
@@ -169,7 +172,7 @@ class HTTPDownload():
while 1:
#need to create chunks
- if not chunksCreated and self.chunkSupport and self.size: #will be setted later by first chunk
+ if not chunksCreated and self.chunkSupport and self.size: #will be set later by first chunk
if not resume:
self.info.setSize(self.size)
@@ -188,7 +191,7 @@ class HTTPDownload():
self.chunks.append(c)
self.m.add_handle(handle)
else:
- #close immediatly
+ #close immediately
self.log.debug("Invalid curl handle -> closed")
c.close()
@@ -202,6 +205,7 @@ class HTTPDownload():
t = time()
# reduce these calls
+ # when num_q is 0, the loop is exited
while lastFinishCheck + 0.5 < t:
# list of failed curl handles
failed = []
@@ -237,10 +241,10 @@ class HTTPDownload():
ex = e
else:
chunksDone.add(curl)
- if not num_q: # no more infos to get
+ if not num_q: # no more info to get
# check if init is not finished so we reset download connections
- # note that other chunks are closed and downloaded with init too
+ # note that other chunks are closed and everything downloaded with initial connection
if failed and init not in failed and init.c not in chunksDone:
self.log.error(_("Download chunks failed, fallback to single connection | %s" % (str(ex))))
@@ -281,12 +285,10 @@ class HTTPDownload():
self.speeds = [float(a) / (t - lastTimeCheck) for a in diff]
self.lastArrived = [c.arrived for c in self.chunks]
lastTimeCheck = t
- self.updateProgress()
if self.abort:
raise Abort()
- #sleep(0.003) #supress busy waiting - limits dl speed to (1 / x) * buffersize
self.m.select(1)
for chunk in self.chunks:
@@ -294,10 +296,6 @@ class HTTPDownload():
self._copyChunks()
- def updateProgress(self):
- if self.progressNotify:
- self.progressNotify(self.percent)
-
def findChunk(self, handle):
""" linear search to find a chunk (should be ok since chunk size is usually low) """
for chunk in self.chunks: