summaryrefslogtreecommitdiffstats
path: root/module/network
diff options
context:
space:
mode:
Diffstat (limited to 'module/network')
-rw-r--r--module/network/Bucket.py10
-rw-r--r--module/network/CookieJar.py20
-rw-r--r--module/network/RequestFactory.py29
3 files changed, 25 insertions, 34 deletions
diff --git a/module/network/Bucket.py b/module/network/Bucket.py
index 69da277ae..ff80bda55 100644
--- a/module/network/Bucket.py
+++ b/module/network/Bucket.py
@@ -20,15 +20,18 @@
from time import time
from threading import Lock
+# 10kb minimum rate
+MIN_RATE = 10240
+
class Bucket:
def __init__(self):
- self.rate = 0
+ self.rate = 0 # bytes per second, maximum targeted throughput
self.tokens = 0
self.timestamp = time()
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()
@@ -37,7 +40,7 @@ class Bucket:
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
+ if self.rate < MIN_RATE: return 0 #May become unresponsive otherwise
self.lock.acquire()
self.calc_tokens()
@@ -47,7 +50,6 @@ class Bucket:
time = -self.tokens/float(self.rate)
else:
time = 0
-
self.lock.release()
return time
diff --git a/module/network/CookieJar.py b/module/network/CookieJar.py
index c05812334..a020d6f9e 100644
--- a/module/network/CookieJar.py
+++ b/module/network/CookieJar.py
@@ -20,10 +20,12 @@
from time import time
class CookieJar():
- def __init__(self, pluginname, account=None):
+ def __init__(self, pluginname):
self.cookies = {}
- self.plugin = pluginname
- self.account = account
+ self.pluginname = pluginname
+
+ def __repr__(self):
+ return ("<CookieJar plugin=%s>\n\t" % self.pluginname) + "\n\t".join(self.cookies.values())
def addCookies(self, clist):
for c in clist:
@@ -33,18 +35,18 @@ class CookieJar():
def getCookies(self):
return self.cookies.values()
- def parseCookie(self, name):
+ def getCookie(self, name):
if name in self.cookies:
return self.cookies[name].split("\t")[6]
else:
return None
- def getCookie(self, name):
- return self.parseCookie(name)
+ def setCookie(self, domain, name, value, path="/", exp=None):
+ if not exp: exp = time() + 3600 * 24 * 180
- 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)
+ # dot makes it valid on all subdomains
+ s = ".%s TRUE %s FALSE %s %s %s" % (domain.strip("."), path, exp, name, value)
self.cookies[name] = s
def clear(self):
- self.cookies = {}
+ self.cookies = {} \ No newline at end of file
diff --git a/module/network/RequestFactory.py b/module/network/RequestFactory.py
index 5b1528281..12fd66c95 100644
--- a/module/network/RequestFactory.py
+++ b/module/network/RequestFactory.py
@@ -24,34 +24,25 @@ from Bucket import Bucket
from HTTPRequest import HTTPRequest
from CookieJar import CookieJar
-from XDCCRequest import XDCCRequest
-
class RequestFactory():
def __init__(self, core):
self.lock = Lock()
self.core = core
self.bucket = Bucket()
self.updateBucket()
- self.cookiejars = {}
+ @property
def iface(self):
return self.core.config["download"]["interface"]
- def getRequest(self, pluginName, account=None, type="HTTP"):
- self.lock.acquire()
-
- if type == "XDCC":
- return XDCCRequest(proxies=self.getProxies())
-
+ def getRequest(self, pluginName, cj=None):
req = Browser(self.bucket, self.getOptions())
- if account:
- cj = self.getCookieJar(pluginName, account)
+ if cj:
req.setCookieJar(cj)
else:
req.setCookieJar(CookieJar(pluginName))
- self.lock.release()
return req
def getHTTPRequest(self, **kwargs):
@@ -67,16 +58,12 @@ class RequestFactory():
rep = h.load(*args, **kwargs)
finally:
h.close()
-
- return rep
- def getCookieJar(self, pluginName, account=None):
- if (pluginName, account) in self.cookiejars:
- return self.cookiejars[(pluginName, account)]
+ return rep
- cj = CookieJar(pluginName, account)
- self.cookiejars[(pluginName, account)] = cj
- return cj
+ def openCookieJar(self, pluginname):
+ """Create new CookieJar"""
+ return CookieJar(pluginname)
def getProxies(self):
""" returns a proxy list for the request classes """
@@ -106,7 +93,7 @@ class RequestFactory():
def getOptions(self):
"""returns options needed for pycurl"""
- return {"interface": self.iface(),
+ return {"interface": self.iface,
"proxies": self.getProxies(),
"ipv6": self.core.config["download"]["ipv6"]}