diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-12-11 17:03:31 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-12-11 17:03:31 +0100 |
commit | 5ae784da28024596e5f2621179625728cdc1e8f6 (patch) | |
tree | 273ae5dd85c8633d55491e438d893ee3434a8024 /pyload/network | |
parent | Clean up some import header (diff) | |
download | pyload-5ae784da28024596e5f2621179625728cdc1e8f6.tar.xz |
Bucket and CookieJar from 0.5.0
Diffstat (limited to 'pyload/network')
-rw-r--r-- | pyload/network/Bucket.py | 29 | ||||
-rw-r--r-- | pyload/network/CookieJar.py | 54 |
2 files changed, 50 insertions, 33 deletions
diff --git a/pyload/network/Bucket.py b/pyload/network/Bucket.py index 047329ad8..3aef2c733 100644 --- a/pyload/network/Bucket.py +++ b/pyload/network/Bucket.py @@ -2,28 +2,37 @@ # @author: RaNaN from time import time -from threading import Lock -class Bucket(object): + +# 10kb minimum rate +MIN_RATE = 10240 + + +class Bucket: + def __init__(self): - self.rate = 0 - self.tokens = 0 + self.rate = 0 # bytes per second, maximum targeted throughput + self.tokens = 0 self.timestamp = time() - self.lock = Lock() + self.lock = Lock() + def __nonzero__(self): - return False if self.rate < 10240 else True + return False if self.rate < MIN_RATE else True + def setRate(self, rate): self.lock.acquire() self.rate = int(rate) self.lock.release() + def consumed(self, amount): - """ return time the process have to sleep, after consumed specified amount """ - if self.rate < 10240: return 0 #min. 10kb, may become unresponsive otherwise - self.lock.acquire() + """ return the time the process has to sleep, after it consumed a specified amount """ + if self.rate < MIN_RATE: + return 0 #: May become unresponsive otherwise + self.lock.acquire() self.calc_tokens() self.tokens -= amount @@ -32,10 +41,10 @@ class Bucket(object): else: time = 0 - self.lock.release() return time + def calc_tokens(self): if self.tokens < self.rate: now = time() diff --git a/pyload/network/CookieJar.py b/pyload/network/CookieJar.py index 4e877b605..a2b302776 100644 --- a/pyload/network/CookieJar.py +++ b/pyload/network/CookieJar.py @@ -1,34 +1,42 @@ # -*- coding: utf-8 -*- -# @author: RaNaN, mkaay +# @author: RaNaN +import Cookie + +from datetime import datetime, timedelta from time import time -class CookieJar(object): - def __init__(self, pluginname, account=None): - self.cookies = {} - self.plugin = pluginname - self.account = account - def addCookies(self, clist): - for c in clist: - name = c.split("\t")[5] - self.cookies[name] = c +# monkey patch for 32 bit systems +def _getdate(future=0, weekdayname=Cookie._weekdayname, monthname=Cookie._monthname): + dt = datetime.now() + timedelta(seconds=int(future)) + return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \ + (weekdayname[dt.weekday()], dt.day, monthname[dt.month], dt.year, dt.hour, dt.minute, dt.second) - def getCookies(self): - return self.cookies.values() +Cookie._getdate = _getdate - def parseCookie(self, name): - if name in self.cookies: - return self.cookies[name].split("\t")[6] - else: - return None + +class CookieJar(Cookie.SimpleCookie): def getCookie(self, name): - return self.parseCookie(name) + return self[name].value + + def setCookie(self, domain, name, value, path="/", exp=None, secure="FALSE"): + self[name] = value + self[name]["domain"] = domain + self[name]["path"] = path + + # Value of expires should be integer if possible + # otherwise the cookie won't be used + if not exp: + expires = time() + 3600 * 24 * 180 + else: + try: + expires = int(exp) + except ValueError: + expires = exp - def setCookie(self, domain, name, value, path="/", exp=time()+3600*24*180): - s = ".%s TRUE %s FALSE %s %s %s" % (domain, path, exp, name, value) - self.cookies[name] = s + self[name]["expires"] = expires - def clear(self): - self.cookies = {} + if secure == "TRUE": + self[name]["secure"] = secure |