from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep
from pymsgbox import *
import traceback
import requests
import io
import random
import time
import os
import sys
from functools import reduce
# Speech Recognition Imports
from pydub import AudioSegment
import speech_recognition as sr
# Selenium
from selenium.common.exceptions import NoSuchElementException
# Randomization Related
MIN_RAND = 0.64
MAX_RAND = 1.27
LONG_MIN_RAND = 4.78
LONG_MAX_RAND = 6.1
NUMBER_OF_ITERATIONS = 5
RECAPTCHA_PAGE_URL = "https://www.google.com/recaptcha/api2/demo"
HOUNDIFY_CLIENT_ID = "IzKsCJiiW8nSTZHdTFg0PA=="
HOUNDIFY_CLIENT_KEY = "zLdwMYSaHud27N_V4Y7m_I78sM90ftEVy0p3A041dIkF-kJ8rp1hQJ-sPLPZu5svzPS8SG1LNXCO2rzsPoTItA=="
WIT_AI_KEY="JI5SZWSOEPZQRIZVUDDXNQ7TRYNKYQKS"
DIGITS_DICT = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"to": "2",
"for": "4",
"sex": "6",
}
class rebreakcaptcha(object):
def __init__(self,b):
self.driver = b
def is_exists_by_xpath(self, xpath):
try:
self.driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
def get_recaptcha_challenge(self):
for _ in range(10):
self.driver.switch_to.default_content()
# self.b.get(RECAPTCHA_PAGE_URL)
# Navigate to a ReCaptcha page
# time.sleep(random.uniform(MIN_RAND, MAX_RAND))
# Get all the iframes on the page
iframes = self.driver.find_elements_by_css_selector("iframe[title^='recaptcha']")
# Switch focus to ReCaptcha iframe
self.driver.switch_to_frame(iframes[0])
# time.sleep(random.uniform(MIN_RAND, MAX_RAND))
# Verify ReCaptcha checkbox is present
if not self.is_exists_by_xpath('//div[@class="recaptcha-checkbox-checkmark" and @role="presentation"]'):
print("[{0}] No element in the frame!!".format(self.current_iteration))
continue
# Click on ReCaptcha checkbox
self.driver.find_element_by_xpath('//div[@class="recaptcha-checkbox-checkmark" and @role="presentation"]').click()
sleep(3)
# time.sleep(random.uniform(LONG_MIN_RAND, LONG_MAX_RAND))
# Check if the ReCaptcha has no challenge
if self.is_exists_by_xpath('//span[@aria-checked="true"]'):
print("[{0}] ReCaptcha has no challenge.".format(self.current_iteration))
return False
else:
return True
def get_audio_challenge(self, iframes):
# Switch to the last iframe (the new one)
self.driver.switch_to_frame(iframes[-1])
# Check if the audio challenge button is present
if not self.is_exists_by_xpath('//button[@id="recaptcha-audio-button"]'):
print("[{0}] No element of audio challenge!!".format(self.current_iteration))
return False
if not self.is_exists_by_xpath('//button[@id="recaptcha-audio-button"][@style="display: none;"]'):
print("[{0}] Clicking on audio challenge".format(self.current_iteration))
# Click on the audio challenge button
self.driver.find_element_by_xpath('//button[@id="recaptcha-audio-button"]').click()
# time.sleep(random.uniform(LONG_MIN_RAND, LONG_MAX_RAND))
sleep(3)
else:
print('Already opened')
def get_challenge_audio(self, url):
request = requests.get(url,headers={'User-Agent':'Mozilla/5.0'})
audio_file = io.BytesIO(request.content)
# Convert the audio to a compatible format in memory
converted_audio = io.BytesIO()
sound = AudioSegment.from_mp3(audio_file)
sound.export(converted_audio, format="wav")
converted_audio.seek(0)
return converted_audio
def string_to_digits(self, recognized_string):
print (''.join([DIGITS_DICT.get(word, "|{}|".format(word)) for word in recognized_string.split(" ")]))
return ''.join([DIGITS_DICT.get(word, "") for word in recognized_string.split(" ")])
def speech_to_text(self, audio_source):
# Initialize a new recognizer with the audio in memory as source
recognizer = sr.Recognizer()
with sr.AudioFile(audio_source) as source:
audio = recognizer.record(source) # read the entire audio file
audio_output = ""
# recognize speech using Google Speech Recognition
try:
audio_output = recognizer.recognize_google(audio)
print("[{0}] Google Speech Recognition: ".format(self.current_iteration) + audio_output)
if any(character.isalpha() for character in audio_output):audio_output = ""
except sr.UnknownValueError as e:
print("[{0}] Google Speech Recognition could not understand audio".format(self.current_iteration))
audio_output = ""
except sr.RequestError as e:
print("[{0}] Could not request results from Google Speech Recognition service; {1}".format(self.current_iteration).format(e))
audio_output = ""
try:
if audio_output=='':
print("[{0}] Fallback to Houndify!".format(self.current_iteration))
_=4
_=None
if _ :
audio_output = self.string_to_digits(recognizer.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))
else:
print('Witty')
audio_output = self.string_to_digits(recognizer.recognize_wit(audio, key=WIT_AI_KEY))
print("[{0}] Houndify: ".format(self.current_iteration) + audio_output)
except sr.UnknownValueError:
print("Houndify could not understand audio")
except sr.RequestError as e:
print("Could not request results from Houndify service; {0}".format(e))
return audio_output
def solve_audio_challenge(self):
# Verify audio challenge download button is present
if not self.is_exists_by_xpath('//a[@class="rc-audiochallenge-tdownload-link"]') and \
not self.is_exists_by_xpath('//div[@class="rc-text-challenge"]'):
print("[{0}] No element in audio challenge download link!!".format(self.current_iteration))
return False
# If text challenge - reload the challenge
for i in range(5):
if not self.is_exists_by_xpath('//div[@class="rc-text-challenge"]'):break
print("[{0}] Got a text challenge! Reloading!".format(self.current_iteration))
self.driver.find_element_by_id('recaptcha-reload-button').click()
# time.sleep(random.uniform(MIN_RAND, MAX_RAND))
sleep(1)
for i in range(3):
try:
# Download the
# Get the audio challenge URI from the download link
print('Gotcha')
download_object = self.driver.find_element_by_xpath('//a[@class="rc-audiochallenge-tdownload-link"]')
download_link = download_object.get_attribute('href')
# Get the challenge audio to send to Google
converted_audio = self.get_challenge_audio(download_link)
audio_output = self.speech_to_text(converted_audio)
if not audio_output:raise
break
except:
self.driver.find_element_by_id('recaptcha-reload-button').click()
# time.sleep(random.uniform(MIN_RAND, MAX_RAND))
audio_output=''
sleep(2)
# Send the audio to Google Speech Recognition API and get the output
# Enter the audio challenge solution
self.driver.find_element_by_id('audio-response').send_keys(audio_output)
# time.sleep(random.uniform(LONG_MIN_RAND, LONG_MAX_RAND))
time.sleep(random.uniform(MIN_RAND, MAX_RAND))
# Click on verify
self.driver.find_element_by_id('recaptcha-verify-button').click()
# time.sleep(random.uniform(LONG_MIN_RAND, LONG_MAX_RAND))
time.sleep(random.uniform(MIN_RAND, MAX_RAND))
return True
def solve(self, current_iteration):
self.current_iteration = current_iteration + 1
# Get a ReCaptcha Challenge
if not self.get_recaptcha_challenge():return True
# Switch to page's main frame
self.driver.switch_to.default_content()
# Get all the iframes on the page again- there is a new one with a challenge
iframes = self.driver.find_elements_by_css_selector("iframe[title^='recaptcha']")
# Get audio challenge
self.get_audio_challenge(iframes)
# Solve the audio challenge
if not self.solve_audio_challenge():
return False
sleep(2)
# Check if there is another audio challenge and solve it too
for _ in range(10):
if not (self.is_exists_by_xpath('//div[@class="rc-audiochallenge-error-message"]') and \
self.is_exists_by_xpath('//div[contains(text(), "Multiple correct solutions required")]') ):break
print("[{0}] Need to solve more. Let's do this!".format(self.current_iteration))
self.solve_audio_challenge()
sleep(2)
# Switch to the ReCaptcha iframe to verify it is solved
self.driver.switch_to.default_content()
self.driver.switch_to_frame(iframes[0])
return self.get_recaptcha_challenge()
return self.is_exists_by_xpath('//span[@aria-checked="true"]')
class Amu:
def __init__(self,i,b):
self.b=b
self.i=i
def cv(self):
self.b.execute_script("document.querySelector('form').scrollIntoView();")
rebreakcaptcha_obj = rebreakcaptcha(b)
counter = 0
for i in xrange(NUMBER_OF_ITERATIONS):
if rebreakcaptcha_obj.solve(i):
print('CV')
counter += 1
self.b.switch_to.default_content()
return True
self.b.switch_to.default_content()
return False
def lsl(self,s=1):
try:
ls=self.b.execute_script("return +sessionStorage.a")
if(s):self.b.execute_script("document.forms[0].submit();")
for i in range(10):
if ls!=self.b.execute_script("return +sessionStorage.a"):break
if(s):sleep(1)
except Exception as e:
print('lsl',e)
def login(self,s):
if self.b.execute_script("return document.querySelector('#profile_part_id.profile_part')"):return
u=[
'un'
]
pwd=[
'pwd'
]
u_=u[s]
p_=pwd[s]
self.b.get('http://www.amulyam.in/')
self.b.execute_script("document.querySelector('#login_email_input_id').value={}".format(u_))
self.b.execute_script("document.querySelector('#login_pwd_input_id').value='{}'".format(p_))
self.b.execute_script("document.querySelector('#login_form_submit').click()")
sleep(2)
def loginc(self):
_='http://www.amulyam.in/daily-login-credits.do'
if self.b.current_url!=_:self.b.get(_)
if self.b.execute_script('return document.forms.length'):
for i in range(10):
if self.cv() :break
print('cving')
self.lsl()
else:
print('Done')
def trivia(self):
_='http://www.amulyam.in/playTrivia.do'
if 'playTrivia' not in self.b.current_url:self.b.get(_)
try:
c=int(self.b.execute_script("return document.querySelector('.question_div span').innerText.split(' ')[0]"))-1
except:
c=1
if self.b.execute_script("return document.querySelectorAll('.amu_add_new a')"):c=25
if self.b.execute_script("return document.querySelector('.game-completion')"):c=25
print('Count',c)
try:
for i in range(c,30+c):
print('No:',i,c+1)
if c==25 :break
for j in range(3):
_="document.getElementsByClassName('jquery-adi light')"
self.b.execute_script("if ({0}.length){0}[0].remove()".format(_))
fl=self.b.execute_script('return document.forms.length')
try:
c=int(self.b.execute_script("return document.querySelector('.question_div span').innerText.split(' ')[0]"))
except:c=c
if fl ==0 :
if 'amulyam' not in self.b.current_url:return
um='Captcha'
print(um)
sleep(3)
_=self.b.execute_script("if({0}.length) return {0}[0].children[0].href; else return 0".format("document.getElementsByClassName('trivia_ans_options')",random.randrange(5)))
if(_):self.b.get(_)
else:
if self.b.execute_script('return document.forms[0].elements[0].tagName')!="TEXTAREA" and j==0:
um='Trivia'
print(um)
self.lsl()
elif self.b.execute_script('return document.forms[0].elements[0].tagName')=="TEXTAREA":
um='ReCaptcha'
print(um)
for i in range(10):
if self.cv() :break
print('cving')
self.lsl()
if self.b.execute_script("return document.querySelectorAll('.amu_add_new a')"):
cw = self.b.current_window_handle
self.b.execute_script("for (a of document.querySelectorAll('.amu_add_new a'))a.click()")
sleep(3)
self.b.switch_to_window(self.b.window_handles[1]);self.lsl(0);self.b.close()
self.b.switch_to_window(self.b.window_handles[1]);self.lsl(0);self.b.close()
self.b.switch_to_window(cw)
self.lsl()
except Exception as e:
self.b.switch_to.default_content()
self.fexc(e)
def voc(self):
if 'Voca' not in self.b.current_url:self.b.get('http://www.amulyam.in/vocabulary.do')
try:
c=int(self.b.execute_script("return document.querySelector('.question_div span').innerText.split(' ')[1]"))-1
except:
c=1
if self.b.execute_script("return document.querySelectorAll('.amu_add_new a')"):c=15
if self.b.execute_script("return document.querySelector('.game-completion')"):c=15
print('Count',c)
try:
for i in range(c,20+c):
print('No:',i,c+1)
if c==15 :break
for j in range(4):
_="document.getElementsByClassName('jquery-adi light')"
self.b.execute_script("if ({0}.length){0}[0].remove()".format(_))
fl=self.b.execute_script('return document.forms.length')
try:
c=int(self.b.execute_script("return document.querySelector('.question_div span').innerText.split(' ')[0]"))
except:c=c
if not fl:
if 'amulyam' not in self.b.current_url:return
um='Captcha'
print(um)
sleep(3)
_=self.b.execute_script("if({0}.length) return {0}[0].children[0].href; else return 0".format("document.getElementsByClassName('trivia_ans_options')",random.randrange(5)))
if(_):self.b.get(_)
else:
fl=self.b.execute_script('return document.forms[0].elements.length')
if fl==2:
mx='ShowWord'
print(mx)
_="document.querySelector('.question_div p')";
self.b.execute_script("if ({0})sessionStorage.w={0}.innerText".format(_))
self.lsl()
elif fl==3 and self.b.execute_script('return document.forms[0].elements[0].tagName')!="TEXTAREA":
mx='EnterWord'
print(mx)
self.b.execute_script("document.querySelector('#submit-word').value=sessionStorage.w;")
self.lsl()
elif fl==3:
mx='Recaptcha'
print(mx)
for i in range(10):
if self.cv() :break
print('cving')
self.lsl()
if self.b.execute_script("return document.querySelectorAll('.amu_add_new a')"):
cw = self.b.current_window_handle
self.b.execute_script("for (a of document.querySelectorAll('.amu_add_new a'))a.click()")
sleep(3)
self.b.switch_to_window(self.b.window_handles[1]);self.lsl(0);self.b.close();sleep(1)
self.b.switch_to_window(self.b.window_handles[1]);self.lsl(0);self.b.close();sleep(1)
self.b.switch_to_window(cw)
self.lsl()
except Exception as e:
self.fexc(e)
def ad(self):
_="document.getElementsByClassName('jquery-adi light')[0]"
self.b.execute_script("if ({0}){0}.remove()".format(_))
def cwh(self):
self.b.switch_to_window(self.b.window_handles[-1])
def fexc(self,e,j=0):
global m_
exc_type, exc_obj, exc_tb = sys.exc_info()
tb = traceback.extract_tb(exc_tb)[-1]
print('{}\n{} {}'.format(e,tb[2],tb[1]))
traceback.print_exc()
print(self.i)
m_='Won'
def a(self):
global m_,bx
try:
t1 = time.time()
self.login(self.i)
self.loginc()
self.trivia()
self.voc()
t2 = time.time()
print("Elapsed time:", secondsToStr( t2-t1 ))
if(m_=='Done'):self.b.get('http://www.amulyam.in/credit-history.do')
_=(confirm( text=m_, title='Smart', buttons=['OK','Cancel']))
if _=='OK':self.b.get('http://www.amulyam.in/logout.do')
except KeyboardInterrupt as e:
print(e)
def __repr__(self):
self.a()
return ''
n=8
m_='Done'
def secondsToStr(t):
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],1000,60,60]))
from selenium import webdriver
class SessionRemote(webdriver.Remote):
def start_session(self, desired_capabilities, browser_profile=None):
# Skip the NEW_SESSION command issued by the original driver
# and set only some required attributes
self.w3c = True
url = 'http://127.0.0.1:51454'
b = SessionRemote(command_executor=url,desired_capabilities={})
b.session_id="2ae2694dc4001f092fe624739c767f6e"
i1=Amu(0,b)
i1.login(0)
i1.trivia()
Sunday, 10 June 2018
Auto Amulyam - Selenium
Amulyam
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment