summaryrefslogtreecommitdiffstats
path: root/module/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'module/plugins')
-rw-r--r--module/plugins/Hook.py14
-rw-r--r--module/plugins/Plugin.py75
-rw-r--r--module/plugins/hooks/CaptchaTrader.py131
3 files changed, 167 insertions, 53 deletions
diff --git a/module/plugins/Hook.py b/module/plugins/Hook.py
index 1b3c05ba1..4bbf6e33a 100644
--- a/module/plugins/Hook.py
+++ b/module/plugins/Hook.py
@@ -19,7 +19,6 @@
"""
-
class Hook():
__name__ = "Hook"
__version__ = "0.2"
@@ -38,6 +37,9 @@ class Hook():
self.interval = 60
self.setup()
+
+ def __repr__(self):
+ return self.__name__
def setup(self):
""" more init stuff if needed"""
@@ -84,3 +86,13 @@ class Hook():
def unrarFinished(self, folder, fname):
pass
+
+ def newCaptchaTask(self, task):
+ """ new captcha task for the plugin, it MUST set the handler and timeout or will be ignored """
+ pass
+
+ def captchaCorrect(self, task):
+ pass
+
+ def captchaWrong(self, task):
+ pass \ No newline at end of file
diff --git a/module/plugins/Plugin.py b/module/plugins/Plugin.py
index 617e2e20a..3891cba94 100644
--- a/module/plugins/Plugin.py
+++ b/module/plugins/Plugin.py
@@ -29,7 +29,6 @@ from os import chmod
from os import stat
from os.path import exists
from os.path import join
-from os.path import basename
if os.name != "nt":
from os import chown
@@ -38,8 +37,6 @@ if os.name != "nt":
from itertools import islice
-from thread import start_new_thread
-
from module.utils import save_join
def chunks(iterable, size):
@@ -110,7 +107,7 @@ class Plugin(object):
self.lastDownload = "" # location where the last call to download was saved
self.lastCheck = None #re match of last checked matched
self.js = self.core.js # js engine
- self.ctresult = None
+ self.cTask = None #captcha task
self.html = None #some plugins store html code here
@@ -221,17 +218,15 @@ class Plugin(object):
def retry(self):
""" begin again from the beginning """
- if self.ctresult:
- self.ctresult.fail()
raise Retry
def invalidCaptcha(self):
- if self.ctresult:
- self.ctresult.fail()
+ if self.cTask:
+ self.cTask.invalid()
def correctCaptcha(self):
- if self.ctresult:
- self.ctresult.success()
+ if self.cTask:
+ self.cTask.success()
def decryptCaptcha(self, url, get={}, post={}, cookies=False, forceUser=False, imgtype="jpg"):
""" loads the catpcha and decrypt it or ask the user for input """
@@ -260,52 +255,28 @@ class Plugin(object):
else:
captchaManager = self.core.captchaManager
- task = captchaManager.newTask(self)
- task.setCaptcha(content, imgtype)
- task.setWaiting()
-
- ct = None
- if self.core.config["captchatrader"]["username"] and self.core.config["captchatrader"]["password"]:
- task.setWatingForUser(exclusive=True)
- from module.lib.captchatrader import CaptchaTrader
- ct = CaptchaTrader(self.core.config["captchatrader"]["username"], self.core.config["captchatrader"]["password"])
- if ct.getCredits < 10:
- self.log.info("Not enough credits for CaptchaTrader")
- task.setWaiting()
- else:
- self.log.info("Submitting to CaptchaTrader")
- def threaded(ct):
- cf = open(join("tmp","tmpCaptcha_%s_%s.%s" % (self.__name__, id, imgtype)), "rb")
- try:
- result = ct.submit(cf)
- except:
- self.log.warning("CaptchaTrader error!")
- if self.core.debug:
- from traceback import print_exc
- print_exc()
- ct = None
- task.setWaiting()
- else:
- self.ctresult = result
- task.setResult(result.getResult())
- self.log.debug("CaptchaTrader response: %s" % result.getResult())
- task.setDone()
- start_new_thread(threaded, (ct, ))
+ task = captchaManager.newTask(content, imgtype, temp.name)
+ self.cTask = task
+ captchaManager.handleCaptcha(task)
- while not task.getStatus() == "done":
- if not self.core.isClientConnected():
- task.removeTask()
- #temp.unlink(temp.name)
- if has_plugin and not ct:
- self.fail(_("Pil and tesseract not installed and no Client connected for captcha decrypting"))
- elif not ct:
- self.fail(_("No Client connected for captcha decrypting"))
+ while task.isWaiting():
if self.pyfile.abort:
- task.removeTask()
+ captchaManager.removeTask(task)
raise Abort
sleep(1)
- result = task.getResult()
- task.removeTask()
+
+ captchaManager.removeTask(task)
+
+ if task.error and has_plugin: #ignore default error message since the user could use OCR
+ self.fail(_("Pil and tesseract not installed and no Client connected for captcha decrypting"))
+ elif task.error:
+ self.fail(task.error)
+ elif not task.result:
+ self.fail(_("No captcha result obtained in appropiate time by any of the plugins."))
+
+
+ result = task.result
+ self.log.debug("Received captcha result: %s" % result)
if not self.core.debug:
try:
diff --git a/module/plugins/hooks/CaptchaTrader.py b/module/plugins/hooks/CaptchaTrader.py
new file mode 100644
index 000000000..9bb8dd2f0
--- /dev/null
+++ b/module/plugins/hooks/CaptchaTrader.py
@@ -0,0 +1,131 @@
+# -*- 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
+"""
+
+try:
+ from json import loads
+except ImportError:
+ from simplejson import loads
+
+from thread import start_new_thread
+from pycurl import FORM_FILE
+
+from module.network.RequestFactory import getURL
+
+from module.plugins.Hook import Hook
+
+PYLOAD_KEY = "9f65e7f381c3af2b076ea680ae96b0b7"
+
+class CaptchaTraderException(Exception):
+ def __init__(self, err):
+ self.err = err
+
+ def getCode(self):
+ return self.err
+
+ def __str__(self):
+ return "<CaptchaTraderException %s>" % self.err
+
+ def __repr__(self):
+ return "<CaptchaTraderException %s>" % self.err
+
+class CaptchaTrader(Hook):
+ __name__ = "CaptchaTrader"
+ __version__ = "0.1"
+ __description__ = """send captchas to captchatrader.com"""
+ __config__ = [("activated", "bool", "Activated", "True"),
+ ("username", "str", "Username", ""),
+ ("passkey", "password", "Password", "")]
+ __author_name__ = ("RaNaN")
+ __author_mail__ = ("RaNaN@pyload.org")
+
+ SUBMIT_URL = "http://captchatrader.com/api/submit"
+ RESPOND_URL = "http://captchatrader.com/api/respond"
+ GETCREDITS_URL = "http://captchatrader.com/api/get_credits/username:%(user)s/password:%(password)s/"
+
+
+ def getCredits(self):
+ json = getURL(CaptchaTrader.GETCREDITS_URL % {"user": self.getConfig("username"),
+ "password": self.getConfig("passkey")})
+ response = loads(json)
+ if response[0] < 0:
+ raise CaptchaTraderException(response[1])
+ else:
+ self.log.debug("CaptchaTrader: %s credits left" % response[1])
+ return response[1]
+
+ def submit(self, captcha, captchaType="file", match=None):
+ if not PYLOAD_KEY:
+ raise CaptchaTraderException("No API Key Specified!")
+
+ #if type(captcha) == str and captchaType == "file":
+ # raise CaptchaTraderException("Invalid Type")
+ assert captchaType in ("file", "url-jpg", "url-jpeg", "url-png", "url-bmp")
+ json = getURL(CaptchaTrader.SUBMIT_URL, post={"api_key": PYLOAD_KEY,
+ "username": self.getConfig("username"),
+ "password": self.getConfig("passkey"),
+ "value": (FORM_FILE, captcha),
+ "type": captchaType}, multipart=True)
+ response = loads(json)
+ if response[0] < 0:
+ raise CaptchaTraderException(response[1])
+
+ ticket = response[0]
+ result = response[1]
+ self.log.debug("CaptchaTrader result %s : %s" % (ticket,result))
+
+ return ticket, result
+
+ def respond(self, ticket, success):
+ json = getURL(CaptchaTrader.RESPOND_URL, post={"is_correct": 1 if success else 0,
+ "username": self.getConfig("username"),
+ "password": self.getConfig("passkey"),
+ "ticket": ticket})
+ response = loads(json)
+ if response[0] < 0:
+ raise CaptchaTraderException(response[1])
+
+ def newCaptchaTask(self, task):
+ if not self.getConfig("username") or not self.getConfig("passkey"):
+ return False
+
+ if self.getCredits() > 10:
+
+ task.handler = self
+ task.setWaiting(40)
+ start_new_thread(self.processCaptcha, (task,))
+
+ else:
+ self.log.info(_("Your CaptchaTrader Account has not enough credits"))
+
+ def captchaCorrect(self, task):
+ ticket = task.data["ticket"]
+ self.respond(ticket, True)
+
+ def captchaWrong(self, task):
+ ticket = task.data["ticket"]
+ self.respond(ticket, True)
+
+ def processCaptcha(self, task):
+ c = task.captchaFile
+ try:
+ ticket, result = self.submit(c)
+ except CaptchaTraderException, e:
+ task.error = e.getCode()
+
+ task.data["ticket"] = ticket
+ task.setResult(result)