diff options
author | mkaay <mkaay@mkaay.de> | 2010-12-22 23:44:33 +0100 |
---|---|---|
committer | mkaay <mkaay@mkaay.de> | 2010-12-22 23:44:33 +0100 |
commit | c66a630a502a5e118d8773b32820aca861ce32cd (patch) | |
tree | fa7b4c5d0f107fb2448a3f399041927182e098a5 /module/network | |
parent | final cookie fix :D (diff) | |
download | pyload-c66a630a502a5e118d8773b32820aca861ce32cd.tar.xz |
new download backend integrated so far, downloading works, but still big todo list
Diffstat (limited to 'module/network')
-rw-r--r-- | module/network/Browser.py | 26 | ||||
-rw-r--r-- | module/network/HTTPChunk.py | 7 | ||||
-rw-r--r-- | module/network/HTTPDownload.py | 2 | ||||
-rw-r--r-- | module/network/helper.py | 45 |
4 files changed, 67 insertions, 13 deletions
diff --git a/module/network/Browser.py b/module/network/Browser.py index b68c39c53..28edf8e9d 100644 --- a/module/network/Browser.py +++ b/module/network/Browser.py @@ -23,6 +23,9 @@ class Browser(object): self.http = HTTPBase(interface=interface, proxies=proxies) self.setCookieJar(cookieJar) self.proxies = proxies + self.abort = property(lambda: False, lambda val: self.abortDownloads()) + + self.downloadConnections = [] def setCookieJar(self, cookieJar): self.cookieJar = cookieJar @@ -78,7 +81,22 @@ class Browser(object): except: pass return location - + + def _removeConnection(self, d): + i = self.downloadConnections.index(d) + del self.downloadConnections[i] + + def abortDownloads(self): + for d in self.downloadConnections: + d.abort = True + + @property + def speed(self): + speed = 0 + for d in self.downloadConnections: + speed += d.speed + return speed + def httpDownload(self, url, filename, get={}, post={}, referer=None, cookies=True, customHeaders={}, chunks=1, resume=False): if not referer: @@ -90,12 +108,16 @@ class Browser(object): dwnld.cookieJar = self.cookieJar d = dwnld.download(chunks=chunks, resume=resume) + self.downloadConnections.append(d) + d.addCallback(self._removeConnection, d) return d def ftpDownload(self, url, filename, resume=False): dwnld = FTPDownload(url, filename, bucket=self.bucket, interface=self.interface, proxies=self.proxies) d = dwnld.download(resume=resume) + self.downloadConnections.append(d) + d.addCallback(self._removeConnection, d) return d def xdccDownload(self, server, port, channel, bot, pack, filename, nick="pyload_%d" % randint(1000, 9999), @@ -104,6 +126,8 @@ class Browser(object): interface=self.interface, proxies=self.proxies) d = dwnld.download() + self.downloadConnections.append(d) + d.addCallback(self._removeConnection, d) return d #compatibility wrapper diff --git a/module/network/HTTPChunk.py b/module/network/HTTPChunk.py index 509ff3983..f26e681b6 100644 --- a/module/network/HTTPChunk.py +++ b/module/network/HTTPChunk.py @@ -22,6 +22,8 @@ from urllib2 import HTTPError from helper import * from time import sleep from traceback import print_exc +from module.plugins.Plugin import Abort +from module.plugins.Plugin import Fail class HTTPChunk(HTTPBase): def __init__(self, url, fh, get={}, post={}, referer=None, cookies=True, customHeaders={}, range=None, bucket=None, interface=None, proxies={}): @@ -109,11 +111,11 @@ class HTTPChunk(HTTPBase): self.fh.close() if self.abort: - self.deferred.error("abort") + self.deferred.error(Abort) elif self.size == self.arrived: self.deferred.callback(resp) else: - self.deferred.error("wrong content lenght") + self.deferred.error(Fail) def getEncoding(self): try: @@ -127,7 +129,6 @@ class HTTPChunk(HTTPBase): if self.range: self.customHeaders["Range"] = "bytes=%i-%i" % self.range try: - print "req" resp = self.getResponse(self.url, self.get, self.post, self.referer, self.cookies, self.customHeaders) self.resp = resp except HTTPError, e: diff --git a/module/network/HTTPDownload.py b/module/network/HTTPDownload.py index ebc8c8e65..e38e1ba1b 100644 --- a/module/network/HTTPDownload.py +++ b/module/network/HTTPDownload.py @@ -274,7 +274,7 @@ class HTTPDownload(object): dg.addCallback(self._copyChunks) if not len(self.chunks): dg.callback() - return WrappedHTTPDeferred(self, self.deferred) + return WrappedHTTPDeferred(self, dg) else: raise Exception("no chunks") diff --git a/module/network/helper.py b/module/network/helper.py index 8cc61d3ff..ed9a88798 100644 --- a/module/network/helper.py +++ b/module/network/helper.py @@ -24,6 +24,7 @@ class Deferred(): def __init__(self): self.call = [] self.err = [] + self.prgr = {} self.result = () self.errresult = () @@ -35,6 +36,13 @@ class Deferred(): kwargs.update(ckwargs) callInThread(f, *args, **kwargs) + #test not in use + def addProgress(self, chain, f): + if self.prgr.has_key(chain): + self.prgr[chain].append(f) + else: + self.prgr[chain] = [f] + def addErrback(self, f, *cargs, **ckwargs): self.err.append((f, cargs, ckwargs)) if self.errresult: @@ -58,6 +66,13 @@ class Deferred(): args+=tuple(cargs) kwargs.update(ckwargs) callInThread(f, *args, **kwargs) + + #test not in use + def progress(self, chain, *args, **kwargs): + if not self.prgr.has_key(chain): + return + for f in self.prgr[chain]: + callInThread(f, *args, **kwargs) #decorator def threaded(f): @@ -71,12 +86,17 @@ def waitFor(d): args = () err = None + def __init__(self, d): + self.d = d + def wait(self): - d.addCallback(self.callb) - d.addErrback(self.errb) + self.d.addCallback(self.callb) + self.d.addErrback(self.errb) while self.waiting: sleep(0.5) if self.err: + if issubclass(self.err[0][0], Exception): + raise self.err[0][0]() raise Exception(self.err) return self.args @@ -87,7 +107,7 @@ def waitFor(d): def errb(self, *args, **kwargs): self.waiting = False self.err = (args, kwargs) - w = Waiter() + w = Waiter(d) return w.wait() class DeferredGroup(Deferred): @@ -110,16 +130,25 @@ class DeferredGroup(Deferred): if len(self.group) == self.done: self.callback() -class WrappedDeferred(): +class WrappedDeferred(object): def __init__(self, download, d): - self.download = download - self.d = d + self.__dict__["download"] = download + self.__dict__["d"] = d def addCallback(self, *args, **kwargs): - self.d.addCallback(*args, **kwargs) + self.__dict__["d"].addCallback(*args, **kwargs) def addErrback(self, *args, **kwargs): - self.d.addErrback(*args, **kwargs) + self.__dict__["d"].addErrback(*args, **kwargs) + + def callback(self, *args, **kwargs): + self.__dict__["d"].callback(*args, **kwargs) + + def error(self, *args, **kwargs): + self.__dict__["d"].error(*args, **kwargs) def __getattr__(self, attr): return getattr(self.download, attr) + + def __setattr__(self, attr, val): + return setattr(self.download, attr, val) |