diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-07-30 21:35:29 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2010-07-30 21:35:29 +0200 |
commit | 2497c100de34c113304227f72015bfb3755854a3 (patch) | |
tree | e382f92368a37d623f5aea5d02609a13a1c338d8 /module/plugins/Plugin.py | |
parent | restart working and client information (diff) | |
download | pyload-2497c100de34c113304227f72015bfb3755854a3.tar.xz |
daily commit
Diffstat (limited to 'module/plugins/Plugin.py')
-rw-r--r-- | module/plugins/Plugin.py | 180 |
1 files changed, 101 insertions, 79 deletions
diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py index e8df540a8..d8d8aae3c 100644 --- a/module/plugins/Plugin.py +++ b/module/plugins/Plugin.py @@ -13,7 +13,7 @@ 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: RaNaN, spoob, mkaay """ @@ -29,23 +29,26 @@ from os.path import exists from os import makedirs +from tempfile import NamedTemporaryFile +from mimetypes import guess_type + def dec(func): def new(*args): - if args[0].pyfile.abort: - raise Abort - return func(*args) + if args[0].pyfile.abort: + raise Abort + return func(*args) return new class Abort(Exception): """ raised when aborted """ - + class Fail(Exception): """ raised when failed """ - + class Reconnect(Exception): """ raised when reconnected """ - + class Retry(Exception): """ raised when start again from beginning """ @@ -57,63 +60,64 @@ class Plugin(object): __description__ = """Base Plugin""" __author_name__ = ("RaNaN", "spoob", "mkaay") __author_mail__ = ("RaNaN@pyload.org", "spoob@pyload.org", "mkaay@mkaay.de") - + def __new__(cls, *args, **kws): - for f in dir(cls): - if not f.startswith("_") and f not in ("checksum"): - setattr(cls, f, dec(getattr(cls, f)) ) - + for f in dir(cls): + if not f.startswith("_") and f not in ("checksum"): + setattr(cls, f, dec(getattr(cls, f)) ) + o = super(cls.__class__, cls).__new__(cls) - #wrap decorator around every method - return o - + #wrap decorator around every method + return o + def __init__(self, pyfile): self.config = pyfile.m.core.config - + self.core = pyfile.m.core + self.req = pyfile.m.core.requestFactory.getRequest(self.__name__) - + self.wantReconnect = False self.multiDL = True - + self.waitUntil = 0 # time() + wait in seconds self.premium = False - + self.ocr = None # captcha reader instance self.account = pyfile.m.core.accountManager.getAccount(self.__name__) # account handler instance - self.req = pyfile.m.core.requestFactory.getRequest(self.__name__, self.account) - + self.req = pyfile.m.core.requestFactory.getRequest(self.__name__, self.account) + self.log = logging.getLogger("log") - + self.pyfile = pyfile self.thread = None # holds thread in future - - self.setup() - + + self.setup() + def __call__(self): - return self.__name__ - + return self.__name__ + def setup(self): - """ more init stuff if needed """ - pass - + """ more init stuff if needed """ + pass + def preprocessing(self, thread): """ handles important things to do before starting """ self.thread = thread - - if not self.account: - self.req.clearCookies() - - self.pyfile.setStatus("starting") - + + if not self.account: + self.req.clearCookies() + + self.pyfile.setStatus("starting") + return self.process(self.pyfile) #---------------------------------------------------------------------- def process(self, pyfile): """the 'main' method of every plugin""" raise NotImplementedError - - + + def checksum(self, local_file=None): """ return codes: @@ -124,9 +128,9 @@ class Plugin(object): 20 - unknown error """ #@TODO checksum check hook - + return (True, 10) - + def setConf(self, option, value): """ sets a config value """ @@ -139,65 +143,83 @@ class Plugin(object): def getConf(self, option): """ gets a config value """ return self.config.getPlugin(self.__name__, option) - - + + def setWait(self, seconds): """ set the wait time to specified seconds """ - self.waitUntil = time() + int(seconds) + self.pyfile.waitUntil = time() + int(seconds) def wait(): """ waits the time previously set """ pass - + def fail(self, reason): """ fail and give reason """ raise Fail(reason) - + def offline(self): - """ fail and indicate file is offline """ - raise Fail("offline") - + """ fail and indicate file is offline """ + raise Fail("offline") + def retry(self): """ begin again from the beginning """ raise Retry - - def askCaptcha(self, url): + + def decryptCaptcha(self, url, get={}, post={}): """ loads the catpcha and decrypt it or ask the user for input """ - pass - - def waitForCaptcha(self, captchaData, imgType): - captchaManager = self.parent.core.captchaManager - task = captchaManager.newTask(self) - task.setCaptcha(captchaData, imgType) - task.setWaiting() - while not task.getStatus() == "done": - if not self.parent.core.isGUIConnected(): - task.removeTask() - raise CaptchaError - sleep(1) - result = task.getResult() - task.removeTask() + + content = self.load(url, get, post) + + temp = NamedTemporaryFile() + + f = temp.file + f.write(content) + #f.close() + + + + ocr = self.core.pluginManager.getCaptchaPlugin(self.__name__) + if ocr: + #@TODO decrypt + result = "" + else: + captchaManager = self.core.captchaManager + mime = guess_type(temp.name) + task = captchaManager.newTask(self) + task.setCaptcha(content, mime[0]) + task.setWaiting() + while not task.getStatus() == "done": + if not self.core.isClientConnected(): + task.removeTask() + #temp.unlink(temp.name) + self.fail(_("No Client connected for captcha decrypting.")) + sleep(1) + result = task.getResult() + task.removeTask() + + #temp.unlink(temp.name) return result + def load(self, url, get={}, post={}, ref=True, cookies=True, just_header=False): """ returns the content loaded """ return self.req.load(url, get, post, ref, cookies, just_header) - + def download(self, url, get={}, post={}, ref=True, cookies=True): """ downloads the url content to disk """ - - self.pyfile.setStatus("downloading") - + + self.pyfile.setStatus("downloading") + download_folder = self.config['general']['download_folder'] - - location = join(download_folder, self.pyfile.package().folder.decode(sys.getfilesystemencoding())) - - if not exists(location): - makedirs(location) - - newname = self.req.download(url, self.pyfile.name, location, get, post, ref, cookies) - - self.pyfile.size = self.req.dl_size - + + location = join(download_folder, self.pyfile.package().folder.decode(sys.getfilesystemencoding())) + + if not exists(location): + makedirs(location) + + newname = self.req.download(url, self.pyfile.name, location, get, post, ref, cookies) + + self.pyfile.size = self.req.dl_size + if newname: - self.pyfile.name = newname + self.pyfile.name = newname |