-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
captchouli.py
50 lines (42 loc) · 1.6 KB
/
captchouli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re
from html.parser import HTMLParser
import requests
from shared import app
def request_captcha():
response = requests.get(app.config["CAPTCHOULI_URL"])
parser = FormParser()
parser.feed(response.text)
return parser.get_captcha()
def valid_solution(captcha_form):
response = requests.post(app.config["CAPTCHOULI_URL"], data=captcha_form)
return response.text == captcha_form["captchouli-id"]
class FormParser(HTMLParser):
def __init__(self):
super().__init__()
self._captcha_id = ""
self._images = {}
self._current_image = ""
self._target_character = ""
self._processing_character = False
def get_captcha(self):
return CaptchouliCaptcha(self._captcha_id, self._target_character, self._images)
def handle_starttag(self, tag, attrs):
dict_attrs = dict(attrs)
if tag == "input":
if dict_attrs.get("name") == "captchouli-id":
self._captcha_id = dict_attrs["value"]
elif dict_attrs.get("class") == "captchouli-checkbox":
self._current_image = dict_attrs["name"]
elif tag == "img":
self._images[self._current_image] = dict_attrs["src"]
elif tag == "b":
self._processing_character = True
def handle_data(self, data):
if self._processing_character:
self._target_character = data
self._processing_character = False
class CaptchouliCaptcha:
def __init__(self, captcha_id, character, images):
self.captcha_id = captcha_id
self.character = character
self.images = images