diff options
Diffstat (limited to 'pyload/network/RequestFactory.py')
-rw-r--r-- | pyload/network/RequestFactory.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/pyload/network/RequestFactory.py b/pyload/network/RequestFactory.py new file mode 100644 index 000000000..e6015219f --- /dev/null +++ b/pyload/network/RequestFactory.py @@ -0,0 +1,108 @@ +# -*- coding: utf-8 -*- + +""" + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see <http://www.gnu.org/licenses/>. + + @author: mkaay, RaNaN +""" + +from Bucket import Bucket +from CookieJar import CookieJar + +from pyload.plugins.network.DefaultRequest import DefaultRequest, DefaultDownload + +class RequestFactory(): + def __init__(self, core): + self.core = core + self.bucket = Bucket() + self.updateBucket() + + def getURL(self, *args, **kwargs): + """ see HTTPRequest for argument list """ + h = DefaultRequest(self.getConfig()) + try: + rep = h.load(*args, **kwargs) + finally: + h.close() + + return rep + + ########## old api methods above + + def getRequest(self, context=None, klass=DefaultRequest): + """ Creates a request with new or given context """ + # also accepts cookiejars, not so nice, since it depends on implementation + if isinstance(context, CookieJar): + return klass(self.getConfig(), context) + elif context: + return klass(*context) + else: + return klass(self.getConfig()) + + def getDownloadRequest(self, request=None, klass=DefaultDownload): + """ Instantiates a instance for downloading """ + # TODO: load with plugin manager + return klass(self.bucket, request) + + def getInterface(self): + return self.core.config["download"]["interface"] + + def getProxies(self): + """ returns a proxy list for the request classes """ + if not self.core.config["proxy"]["proxy"]: + return {} + else: + type = "http" + setting = self.core.config["proxy"]["type"].lower() + if setting == "socks4": + type = "socks4" + elif setting == "socks5": + type = "socks5" + + username = None + if self.core.config["proxy"]["username"] and self.core.config["proxy"]["username"].lower() != "none": + username = self.core.config["proxy"]["username"] + + pw = None + if self.core.config["proxy"]["password"] and self.core.config["proxy"]["password"].lower() != "none": + pw = self.core.config["proxy"]["password"] + + return { + "type": type, + "address": self.core.config["proxy"]["address"], + "port": self.core.config["proxy"]["port"], + "username": username, + "password": pw, + } + + def getConfig(self): + """returns options needed for pycurl""" + return {"interface": self.getInterface(), + "proxies": self.getProxies(), + "ipv6": self.core.config["download"]["ipv6"]} + + def updateBucket(self): + """ set values in the bucket according to settings""" + if not self.core.config["download"]["limit_speed"]: + self.bucket.setRate(-1) + else: + self.bucket.setRate(self.core.config["download"]["max_speed"] * 1024) + +# needs pyreq in global namespace +def getURL(*args, **kwargs): + return pyreq.getURL(*args, **kwargs) + + +def getRequest(*args, **kwargs): + return pyreq.getRequest(*args, **kwargs) |