-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
155 lines (128 loc) · 5.92 KB
/
app.py
File metadata and controls
155 lines (128 loc) · 5.92 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from flask import Flask, make_response, request, redirect, url_for, jsonify
from selenium import webdriver
# from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from PIL import Image
import base64
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
import math
import pandas as pd
import ast
app = Flask(__name__)
def scrape_fun(driver, state_input, city_input, station_input):
state_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[1]/div/select"))
time.sleep(2)
city_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div/select"))
time.sleep(2)
station_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[3]/div/select"))
time.sleep(2)
state_dropdown.select_by_visible_text(state_input)
time.sleep(2)
city_dropdown.select_by_visible_text(city_input)
time.sleep(2)
station_dropdown.select_by_visible_text(station_input)
time.sleep(7)
aqi_element = driver.find_elements(By.ID, 'aqi-meter')
prompoll=driver.find_elements(By.XPATH, "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div[2]/table/tbody/tr[2]/td/big/span")
daydate=driver.find_elements(By.XPATH, "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div[2]/table/tbody/tr[3]/td/big/span")
space,info=[],[]
for i in aqi_element:
info.append(i.text)
finalaqi=info[0].split('\n')
aqi=(finalaqi[2])
aqitag=(finalaqi[1])
for j in prompoll:
prom=(j.text)
for k in daydate:
space.append(k.text)
date=(space[0])
return aqi, aqitag, prom, date
def distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def init_driver():
setting = webdriver.ChromeOptions()
# setting.add_argument("--headless")
setting.add_argument("disable-infobars")
setting.add_argument("--disable-extensions")
setting.add_argument("--disable-dev-shm-usage")
setting.add_argument("--no-sandbox")
setting.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(chrome_options=setting)
return driver
global driver
driver = init_driver()
@app.route("/closest-location/<float:lat>/<float:long>", methods=["GET"])
def closest_location(lat,long):
origin = (lat,long)
df = pd.read_excel('C:/Users/uddin/OneDrive/Desktop/airqualindex/AQIstations.xlsx')
coord,dist=[],[]
for index, row in df.iterrows():
destinations = ast.literal_eval(row['lat_long'])
coord.append((destinations[0],destinations[1]))
for i in coord:
dist.append(distance(origin, i))
min_dist = min(dist)
min_index = dist.index(min_dist)
state_input = df.iloc[min_index]['State']
city_input = df.iloc[min_index]['City']
station_input = df.iloc[min_index]['Station Name']
state_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[1]/div/select"))
time.sleep(2)
city_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div/select"))
time.sleep(2)
station_dropdown = Select(driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[3]/div/select"))
time.sleep(2)
state_dropdown.select_by_visible_text(state_input)
time.sleep(2)
city_dropdown.select_by_visible_text(city_input)
time.sleep(2)
station_dropdown.select_by_visible_text(station_input)
time.sleep(7)
aqi_element = driver.find_elements(By.ID, 'aqi-meter')
prompoll=driver.find_elements(By.XPATH, "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div[2]/table/tbody/tr[2]/td/big/span")
daydate=driver.find_elements(By.XPATH, "/html/body/div[3]/div[1]/div[2]/div[2]/div[2]/div[3]/div[1]/div/div[2]/table/tbody/tr[3]/td/big/span")
space,info=[],[]
for i in aqi_element:
info.append(i.text)
finalaqi=info[0].split('\n')
aqi=(finalaqi[2])
aqitag=(finalaqi[1])
for j in prompoll:
prom=(j.text)
for k in daydate:
space.append(k.text)
date=(space[0])
return jsonify({"State":state_input, "City":city_input, "Station":station_input, "AQI": aqi, "AQITAG": aqitag, "Prominent Pollutant": prom, "Date": date})
@app.route("/scrape-image", methods=["GET"])
def scrape_image():
driver.get("https://app.cpcbccr.com/AQI_India/")
captcha = driver.find_element(By.XPATH, "/html/body/div[7]/div/div/div[2]/div[1]/img")
captcha_url = captcha.get_attribute("src")
image_data = base64.b64decode(captcha_url.split(',')[1])
response = make_response(image_data)
response.headers.set('Content-Type', 'image/png')
response.headers.set('Content-Disposition', 'attachment', filename='captcha.png')
return response
@app.route("/submit-captcha/<captcha>", methods=["GET"])
def submit_captcha(captcha):
submit_button = driver.find_element(By.XPATH, "/html/body/div[7]/div/div/div[3]/button[2]")
captcha_input_field=driver.find_element(By.XPATH, "/html/body/div[7]/div/div/div[2]/input")
captcha_input_field.send_keys(captcha)
submit_button.click()
return redirect(url_for("submit_captcha_success"))
@app.route("/submit-captcha-success", methods=["GET"])
def submit_captcha_success():
return "Captcha input submitted successfully!"