diff options
author | mkaay <mkaay@mkaay.de> | 2010-05-06 17:46:57 +0200 |
---|---|---|
committer | mkaay <mkaay@mkaay.de> | 2010-05-06 17:46:57 +0200 |
commit | 70815efe19e50718bc149ed77466a35833f8dbd1 (patch) | |
tree | 8ae616d088456a35ec981bfcd8e69703d61793f5 /module | |
parent | dlc update (diff) | |
download | pyload-70815efe19e50718bc149ed77466a35833f8dbd1.tar.xz |
new request factory
Diffstat (limited to 'module')
-rw-r--r-- | module/RequestFactory.py | 53 | ||||
-rwxr-xr-x | module/network/Request.py | 25 | ||||
-rw-r--r-- | module/plugins/Plugin.py | 3 |
3 files changed, 75 insertions, 6 deletions
diff --git a/module/RequestFactory.py b/module/RequestFactory.py new file mode 100644 index 000000000..a2a3ad902 --- /dev/null +++ b/module/RequestFactory.py @@ -0,0 +1,53 @@ +# -*- 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 +""" + +from threading import Lock +from module.network.Request import Request +from tempfile import NamedTemporaryFile + +class RequestFactory(): + def __init__(self, core): + self.lock = Lock() + self.core = core + self.requests = [] + + def getRequest(self, pluginName, account=None): + self.lock.acquire() + for req in self.requests: + if req[0:2] == (pluginName, account): + self.lock.release() + return req[2] + name = pluginName + if account: + name += "_" + name += account + th = NamedTemporaryFile(mode="w", prefix="pyload_cookies_%s" % name, delete=False) + cookieFile = th.name + th.close() + + req = Request(cookieFile) + self.requests.append((pluginName, account, req)) + self.lock.release() + return req + + def clean(self): + self.lock.acquire() + for req in self.requests: + req[2].clean() + self.lock.release() diff --git a/module/network/Request.py b/module/network/Request.py index ef2e7d1a4..73ba06e2f 100755 --- a/module/network/Request.py +++ b/module/network/Request.py @@ -27,12 +27,14 @@ from os.path import exists import urllib from cStringIO import StringIO import pycurl +from tempfile import NamedTemporaryFile +from os import remove class AbortDownload(Exception): pass class Request: - def __init__(self): + def __init__(self, cookieFile=None): self.dl_time = 0 self.dl_finished = 0 @@ -63,6 +65,12 @@ class Request: self.speedLimitActive = False self.maxSpeed = 0 self.isSlow = False + + if not cookieFile: + th = NamedTemporaryFile(mode="w", prefix="pyload_cookies", delete=False) + cookieFile = th.name + th.close() + self.cookieFile = cookieFile self.init_curl() @@ -138,9 +146,8 @@ class Request: return self.get_rep() def curl_enable_cookies(self): - cookie_file = "module" + sep + "cookies.txt" - self.pycurl.setopt(pycurl.COOKIEFILE, cookie_file) - self.pycurl.setopt(pycurl.COOKIEJAR, cookie_file) + self.pycurl.setopt(pycurl.COOKIEFILE, self.cookieFile) + self.pycurl.setopt(pycurl.COOKIEJAR, self.cookieFile) def add_auth(self, user, pw): @@ -334,6 +341,16 @@ class Request: if not exists(temp_name): file_name = temp_name return file_name + + def __del__(self): + self.clean() + + def clean(self): + try: + remove(self.cookieFile) + self.pycurl.close() + except: + print "cant clean" def getURL(url): """ diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py index 107c4f0ca..50923498f 100644 --- a/module/plugins/Plugin.py +++ b/module/plugins/Plugin.py @@ -26,7 +26,6 @@ from time import sleep import sys from os.path import exists -from module.network.Request import Request from os import makedirs from module.DownloadThread import CaptchaError @@ -44,7 +43,7 @@ class Plugin(): self.configparser = parent.core.parser_plugins self.config = {} self.parent = parent - self.req = Request() + self.req = parent.core.requestFactory.getRequest(self.__name__) self.html = 0 self.time_plus_wait = 0 #time() + wait in seconds self.want_reconnect = False |