Revision: 7093
Author:   alexsh
Date:     2009-07-25 09:39:25 +0000 (Sat, 25 Jul 2009)

Log Message:
-----------
site().solveCaptcha():Add API Captcha solution accroding MW ConfirmEdit 
extension API details.

Modified Paths:
--------------
    trunk/pywikipedia/wikipedia.py

Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py      2009-07-25 08:38:23 UTC (rev 7092)
+++ trunk/pywikipedia/wikipedia.py      2009-07-25 09:39:25 UTC (rev 7093)
@@ -4599,24 +4599,38 @@
         return '&'.join(l)
 
     def solveCaptcha(self, data):
-        captchaW = re.compile('<label 
for="wpCaptchaWord">(?P<question>[^<]*)</label>')
-        captchaR = re.compile('<input type="hidden" name="wpCaptchaId" 
id="wpCaptchaId" value="(?P<id>\d+)" />')
-        match = captchaR.search(data)
-        if match:
-            id = match.group('id')
-            match = captchaW.search(data)
+        if type(data) == dict: # API Mode result
+            if data['result'].has_key("captcha"):
+                type = data['result']['captcha']['type']
+                id = data['result']['captcha']['id']
+                if type in ['simple', 'math', 'question']:
+                    answer = input('What is the answer to the captcha "%s" ?' 
% data['result']['captcha']['question'])
+                elif type == 'image':
+                    url = self.protocol() + '://' + self.hostname() + 
self.captcha_image_address(id)
+                    answer = ui.askForCaptcha(url)
+                else: #no captcha id result, maybe ReCaptcha.
+                    raise CaptchaError('We have been prompted for a ReCaptcha, 
but pywikipedia does not yet support ReCaptchas')
+                return {'id':id, 'answer':answer}
+            return None
+        else:
+            captchaW = re.compile('<label 
for="wpCaptchaWord">(?P<question>[^<]*)</label>')
+            captchaR = re.compile('<input type="hidden" name="wpCaptchaId" 
id="wpCaptchaId" value="(?P<id>\d+)" />')
+            match = captchaR.search(data)
             if match:
-                answer = input('What is the answer to the captcha "%s" ?' % 
match.group('question'))
-            else:
-                if not config.solve_captcha:
-                    raise CaptchaError(id)
-                url = self.protocol() + '://' + self.hostname() + 
self.captcha_image_address(id)
-                answer = ui.askForCaptcha(url)
-            return {'id':id, 'answer':answer}
-        Recaptcha = re.compile('<script type="text/javascript" 
src="http://api\.recaptcha\.net/[^"]*";></script>')
-        if Recaptcha.search(data):
-            raise CaptchaError('We have been prompted for a ReCaptcha, but 
pywikipedia does not yet support ReCaptchas')
-        return None
+                id = match.group('id')
+                match = captchaW.search(data)
+                if match:
+                    answer = input('What is the answer to the captcha "%s" ?' 
% match.group('question'))
+                else:
+                    if not config.solve_captcha:
+                        raise CaptchaError(id)
+                    url = self.protocol() + '://' + self.hostname() + 
self.captcha_image_address(id)
+                    answer = ui.askForCaptcha(url)
+                return {'id':id, 'answer':answer}
+            Recaptcha = re.compile('<script type="text/javascript" 
src="http://api\.recaptcha\.net/[^"]*";></script>')
+            if Recaptcha.search(data):
+                raise CaptchaError('We have been prompted for a ReCaptcha, but 
pywikipedia does not yet support ReCaptchas')
+            return None
 
     def postForm(self, address, predata, sysop=False, cookies = None):
         """Post http form data to the given address at this site.



_______________________________________________
Pywikipedia-svn mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn

Reply via email to