-
Notifications
You must be signed in to change notification settings - Fork 136
/
RecaptchaSolver.py
66 lines (51 loc) · 2.29 KB
/
RecaptchaSolver.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os,urllib,random,pydub,speech_recognition,time
from DrissionPage.common import Keys
from DrissionPage import ChromiumPage
class RecaptchaSolver:
def __init__(self, driver:ChromiumPage):
self.driver = driver
def solveCaptcha(self):
iframe_inner = self.driver("@title=reCAPTCHA")
time.sleep(0.1)
# Click on the recaptcha
iframe_inner('.rc-anchor-content',timeout=1).click()
self.driver.wait.ele_displayed("xpath://iframe[contains(@title, 'recaptcha')]",timeout=3)
# Sometimes just clicking on the recaptcha is enough to solve it
if self.isSolved():
return
# Get the new iframe
iframe = self.driver("xpath://iframe[contains(@title, 'recaptcha')]")
# Click on the audio button
iframe('#recaptcha-audio-button',timeout=1).click()
time.sleep(.3)
# Get the audio source
src = iframe('#audio-source').attrs['src']
# Download the audio to the temp folder
path_to_mp3 = os.path.normpath(os.path.join((os.getenv("TEMP") if os.name=="nt" else "/tmp/")+ str(random.randrange(1,1000))+".mp3"))
path_to_wav = os.path.normpath(os.path.join((os.getenv("TEMP") if os.name=="nt" else "/tmp/")+ str(random.randrange(1,1000))+".wav"))
urllib.request.urlretrieve(src, path_to_mp3)
# Convert mp3 to wav
sound = pydub.AudioSegment.from_mp3(path_to_mp3)
sound.export(path_to_wav, format="wav")
sample_audio = speech_recognition.AudioFile(path_to_wav)
r = speech_recognition.Recognizer()
with sample_audio as source:
audio = r.record(source)
# Recognize the audio
key = r.recognize_google(audio)
# Input the key
iframe('#audio-response').input(key.lower())
time.sleep(0.1)
# Submit the key
iframe('#audio-response').input(Keys.ENTER)
time.sleep(.4)
# Check if the captcha is solved
if self.isSolved():
return
else:
raise Exception("Failed to solve the captcha")
def isSolved(self):
try:
return "style" in self.driver.ele(".recaptcha-checkbox-checkmark",timeout=1).attrs
except:
return False