diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-22 14:25:56 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-12-22 14:25:56 +0100 |
commit | 052d09687627999b91b45a8c0224aa9de4769924 (patch) | |
tree | 0ed3dd7cc49cd43fd2ce4c1b1573a13265daefb6 /module/network | |
parent | refactoring part 1: deprecation warnings, reduced debug, cookie compatibility (diff) | |
download | pyload-052d09687627999b91b45a8c0224aa9de4769924.tar.xz |
cleaned request factory
Diffstat (limited to 'module/network')
-rw-r--r-- | module/network/Browser.py | 6 | ||||
-rw-r--r-- | module/network/CookieJar.py | 41 | ||||
-rw-r--r-- | module/network/RequestFactory.py | 68 |
3 files changed, 112 insertions, 3 deletions
diff --git a/module/network/Browser.py b/module/network/Browser.py index df0f24b4d..3d2d5a73f 100644 --- a/module/network/Browser.py +++ b/module/network/Browser.py @@ -2,9 +2,9 @@ from random import randint from helper import * from os.path import join from logging import getLogger -from cookielib import CookieJar import zlib +from CookieJar import CookieJar from HTTPBase import HTTPBase from HTTPDownload import HTTPDownload from FTPBase import FTPDownload @@ -13,7 +13,7 @@ from XDCCBase import XDCCDownload from traceback import print_stack class Browser(object): - def __init__(self, interface=None, cookieJar=CookieJar(), bucket=None, proxies={}): + def __init__(self, interface=None, cookieJar=None, bucket=None, proxies={}): self.log = getLogger("log") self.lastURL = None @@ -21,7 +21,7 @@ class Browser(object): self.bucket = bucket self.http = HTTPBase(interface=interface, proxies=proxies) - self.setCookieJar(cookieJar) + self.setCookieJar(cookieJar if cookieJar else CookieJar()) self.proxies = proxies def setCookieJar(self, cookieJar): diff --git a/module/network/CookieJar.py b/module/network/CookieJar.py new file mode 100644 index 000000000..fc6b5e076 --- /dev/null +++ b/module/network/CookieJar.py @@ -0,0 +1,41 @@ +# -*- 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 cookielib import CookieJar as PyCookieJar +from cookielib import Cookie + + +class CookieJar(PyCookieJar): + def __init__(self, pluginName=None, account=None): + PyCookieJar.__init__(self) + self.plugin = pluginName + self.account = account + + + def getCookie(self, name): + print "getCookie not implemented!" + return None + + def setCookie(self, domain, name, value, path="/"): + c = Cookie(version=0, name=name, value=value, port=None, port_specified=False, + domain=domain, domain_specified=False, + domain_initial_dot=(domain.startswith(".")), path=path, path_specified=True, + secure=False, expires=None, discard=True, comment=None, + comment_url=None, rest={'HttpOnly': None}, rfc2109=False) + self.set_cookie(c)
\ No newline at end of file diff --git a/module/network/RequestFactory.py b/module/network/RequestFactory.py new file mode 100644 index 000000000..d73ab5e20 --- /dev/null +++ b/module/network/RequestFactory.py @@ -0,0 +1,68 @@ +# -*- 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 threading import Lock + +from Browser import Browser +from HTTPBase import HTTPBase +from CookieJar import CookieJar + +class RequestFactory(): + def __init__(self, core): + self.lock = Lock() + self.core = core + self.cookiejars = {} + + iface = property(lambda self: self.core.config["general"]["download_interface"]) + + def getRequest(self, pluginName, account=None): + self.lock.acquire() + + req = Browser() + #@TODO proxy stuff, bucket + + if account: + cj = self.getCookieJar(pluginName, account) + req.setCookieJar(cj) + else: + req.setCookieJar(CookieJar(pluginName)) + + self.lock.release() + return req + + def getURL(self, url): + base = HTTPBase() + #@TODO proxies + #@TODO post,get... + + resp = base.getResponse(url) + resp = resp.read() + return resp + + def getCookieJar(self, pluginName, account=None): + if self.cookiejars.has_key((pluginName, account)): + return self.cookiejars[(pluginName, account)] + + cj = CookieJar(pluginName, account) + self.cookiejars[(pluginName, account)] = cj + return cj + +# needs pyreq in global namespace +def getURL(url): + pyreq.getURL(url)
\ No newline at end of file |