-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoterInfo.py
50 lines (40 loc) · 1.54 KB
/
voterInfo.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
from utils import getCaptchaHeaders, getVoterPayload, getCookies
from captcha import solveCaptcha
import requests
def writeCaptchaToDisk(captcha, captchaPath):
file = open(captchaPath, "wb")
file.write(captcha)
file.close()
def generateCaptchaImage(headers):
response = requests.get(
'https://electoralsearch.in/Home/GetCaptcha?image=true', headers=headers)
return response.content
def getVoterInfo(name, dob, gender, relationName, captchaPath):
MAX_TRIES = 20
response = None
while MAX_TRIES > 0:
print(MAX_TRIES)
headers = getCaptchaHeaders()
captchaImage = generateCaptchaImage(headers)
writeCaptchaToDisk(captchaImage, captchaPath)
captchaText = solveCaptcha(captchaPath).strip()
requestData = getVoterPayload(
name, dob, relationName, gender, captchaText)
cookies = getCookies()
headers["Accept"] = 'application/json, text/plain, */*'
headers["Accept-Language"] = 'en-US,en;q=0.9,ta;q=0.8'
response = requests.post('https://electoralsearch.in/Home/searchVoter',
headers=headers, cookies=cookies, json=requestData)
if response.text == 'Wrong Captcha':
print(response.text)
MAX_TRIES = MAX_TRIES - 1
else:
break
if MAX_TRIES == 0:
print("Max Tries Reached. Try Again")
return None
docs = response.json()["response"]["docs"]
if len(docs) == 0:
print("No results found")
return None
return docs[0]