-
Notifications
You must be signed in to change notification settings - Fork 12
/
vehicle-info.py
131 lines (105 loc) · 4.42 KB
/
vehicle-info.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pytesseract
import argparse
import sys
import re
import os
import requests
import cv2
import json
import numpy as np
from time import sleep
try:
import Image
except ImportError:
from PIL import Image, ImageEnhance
from bs4 import BeautifulSoup, SoupStrainer
from urllib import urlretrieve
from io import BytesIO
app_url = 'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml'
captcha_image_url = 'https://vahan.nic.in/nrservices/cap_img.jsp'
number = sys.argv[1]
#MARK: Get Request to get webpage elements like textFields, image, etc
r = requests.get(url=app_url)
cookies = r.cookies
soup = BeautifulSoup(r.text, 'html.parser')
#MARK: ViewState contains token which needs to be passed in POST Request
# ViewState is a hidden element. Open debugger to inspect element
viewstate = soup.select('input[name="javax.faces.ViewState"]')[0]['value']
#MARK: Get Request to get Captcha Image from URL
## Captcha Image Changes each time the URL is fired
iresponse = requests.get(captcha_image_url)
img = Image.open(BytesIO(iresponse.content))
img.save("downloadedpng.png")
def resolve(img):
enhancedImage = enhance()
return pytesseract.image_to_string(enhancedImage)
def enhance():
img = cv2.imread('downloadedpng.png', 0)
kernel = np.ones((2,2), np.uint8)
img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)
erosion_again = cv2.erode(img_dilation, kernel, iterations=1)
final = cv2.GaussianBlur(erosion_again, (1, 1), 0)
return final
print('Resolving Captcha')
captcha_text = resolve(img)
extracted_text = captcha_text.replace(" ", "").replace("\n", "")
print("OCR Result => ", extracted_text)
print(extracted_text)
# MARK: Identifying Submit Button which will be responsible to make POST Request
button = soup.find("button",{"type": "submit"})
encodedViewState = viewstate.replace("/", "%2F").replace("+", "%2B").replace("=", "%3D")
# MARK: Data, which needs to be passed in POST Request | Verify this manually in debugger
data = {
'javax.faces.partial.ajax':'true',
'javax.faces.source': button['id'],
'javax.faces.partial.execute':'@all',
'javax.faces.partial.render': 'rcDetailsPanel resultPanel userMessages capatcha txt_ALPHA_NUMERIC',
button['id']:button['id'],
'masterLayout':'masterLayout',
'regn_no1_exact': number,
'txt_ALPHA_NUMERIC': extracted_text,
'javax.faces.ViewState': viewstate,
'j_idt32':''
}
# MARK: Data in Query format.. But not in use for now
query = "javax.faces.partial.ajax=true&javax.faces.source=%s&javax.faces.partial.execute=%s&javax.faces.partial.render=rcDetailsPanel+resultPanel+userMessages+capatcha+txt_ALPHA_NUMERIC&j_idt42=j_idt42&masterLayout=masterLayout&j_idt32=®n_no1_exact=%s&txt_ALPHA_NUMERIC=%s&javax.faces.ViewState=%s"%(button['id'], '%40all', number, extracted_text, encodedViewState)
# MARK: Request Headers which may or may not needed to be passed in POST Request
# Verify in debugger
headers = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/xml, text/xml, */*; q=0.01',
'Accept-Language': 'en-us',
'Accept-Encoding': 'gzip, deflate, br',
'Host': 'vahan.nic.in',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15',
'Cookie': 'JSESSIONID=%s; SERVERID_7081=vahanapi_7081; SERVERID_7082=nrservice_7082' % cookies['JSESSIONID'],
'X-Requested-With':'XMLHttpRequest',
'Faces-Request':'partial/ajax',
'Origin':'https://vahan.nic.in',
'Referer':'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml',
'Connection':'keep-alive'
# 'User-Agent': 'python-requests/0.8.0',
# 'Access-Control-Allow-Origin':'*',
}
print(headers)
print("\nCookie JSESSIONID => ", cookies['JSESSIONID'])
print("\nData => \n")
print(data)
# MARK: Added delay
sleep(2.0)
#MARK: Send POST Request
postResponse = requests.post(url=app_url, data=data, headers=headers, cookies=cookies)
print("\nPOST Request -> Response =>\n")
print(postResponse)
rsoup = BeautifulSoup(postResponse.text, 'html.parser')
print("Mark: postResponse soup => ")
print(rsoup.prettify())
#MARK: Following code finds tr which means <table> element from html response
# the required response is appended in <table> only. Verify it in debugger
table = SoupStrainer('tr')
tsoup = BeautifulSoup(rsoup.get_text(), 'html.parser', parse_only=table)
print("Table Soup => ")
print(tsoup.prettify())
#MARK: Result Table not appending to the response data
#Fix Needed