-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiamarobot.py
78 lines (73 loc) · 2.66 KB
/
iamarobot.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
67
68
69
70
71
72
73
74
75
76
77
78
from selenium.webdriver.common.action_chains import ActionChains
from random import randint
from time import sleep
from wget import download
from pydub import AudioSegment
import speech_recognition as sr
import os
class Docaptcha:
def __init__(self, driver, xpath) -> None:
self.driver = driver
self.xpath = xpath
def solve(self):
action = ActionChains(self.driver)
size = {'width': self.driver.execute_script('return document.body.clientWidth'), 'height': self.driver.execute_script('return document.body.clientHeight')}
print(f"size: {size}")
x, y = 0, 0
for i in range(randint(5, 10)):
randx = randint(-1 * (x - 1), size['width'] - (x + 1))
randy = randint(-1 * (y - 1), size['height'] - (y + 1))
x += randx
y += randy
action.move_by_offset(randx, randy)
action.pause(0.5)
action.move_to_element(self.driver.find_element_by_xpath(self.xpath))
action.pause(1)
action.perform()
self.driver.find_element_by_xpath(self.xpath).click()
sleep(1)
action.move_to_element(self.driver.find_element_by_xpath(self.xpath))
self.driver.switch_to.frame(self.driver.find_element_by_css_selector('iframe[title="recaptcha challenge"]'))
sleep(1)
action1 = ActionChains(self.driver)
action1.move_to_element(self.driver.find_element_by_id('recaptcha-help-button'))
action1.pause(0.5)
action1.move_to_element(self.driver.find_element_by_id('recaptcha-audio-button'))
action1.perform()
sleep(1)
self.driver.find_element_by_id('recaptcha-audio-button').click()
sleep(1)
while True:
try:
audio_btn = self.driver.find_element_by_class_name('rc-audiochallenge-tdownload-link')
except:
return
link = audio_btn.get_attribute('href')
if os.path.exists('audio.mp3'):
os.remove('audio.mp3')
download(link, 'audio.mp3')
else:
download(link, 'audio.mp3')
s = AudioSegment.from_mp3('audio.mp3')
s.export('audio.wav', format='wav')
r = sr.Recognizer()
with sr.AudioFile('audio.wav') as source:
audio = r.record(source)
try:
text = r.recognize_google(audio)
self.driver.find_element_by_id('audio-response').send_keys(text)
sleep(3)
action2 = ActionChains(self.driver)
action2.move_to_element(self.driver.find_element_by_id('recaptcha-reload-button'))
action2.move_to_element(self.driver.find_element_by_id('recaptcha-verify-button'))
action2.perform()
sleep(5)
self.driver.find_element_by_id('recaptcha-verify-button').click()
except sr.UnknownValueError:
print(" Could not understand audio")
except sr.RequestError as e:
print("Error: {0}".format(e))
if os.path.exists('audio.mp3'):
os.remove('audio.mp3')
if os.path.exists('audio.wav'):
os.remove('audio.wav')