-
Notifications
You must be signed in to change notification settings - Fork 0
/
applmacho.py
140 lines (121 loc) · 5.16 KB
/
applmacho.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
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Flask, escape, request, jsonify, render_template, Response
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import schedule
from datetime import datetime,date
import time
# import pandas as pd
from flask_cors import CORS
from pymongo import MongoClient
from flask_pymongo import PyMongo
import json
from bson import ObjectId
from collections import Iterable
from bson import json_util
import pyrebase
# from dictionary import cities_dic
config = {
"apiKey": "AIzaSyA2CU0xOpT1kXXSKEDeuLc8Rf604kCiWj8",
"authDomain": "researchdb-39220.firebaseapp.com",
"databaseURL": "https://researchdb-39220.firebaseio.com",
"projectId": "researchdb-39220",
"storageBucket": "researchdb-39220.appspot.com",
"messagingSenderId": "33539234789",
"appId": "1:33539234789:web:264cb7f4ad23c724"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
app = Flask(__name__, template_folder='templates')
CORS(app)
def get_routes(source,dest):
driver = webdriver.Chrome()
driver.set_page_load_timeout(30)
driver.implicitly_wait(20)
parent_han = driver.window_handles
try:
driver.get("https://www.google.com/maps/@6.829237,80.0981046,10z/data=!5m1!1e1")
inputElement1 = WebDriverWait(driver , 3000).until(lambda driver: driver.find_element_by_class_name("tactile-searchbox-input"))
inputElement1.send_keys(dest)
inputElement1.send_keys(u'\ue007')
directionsButton = driver.find_element_by_class_name("iRxY3GoUYUY__taparea")
directionsButton.click()
inputElement2 = WebDriverWait(driver , 3000).until(lambda driver: driver.find_element_by_xpath("//input[@placeholder='Choose starting point, or click on the map...']"))
inputElement2.send_keys(source)
inputElement2.send_keys(u'\ue007')
car=driver.find_element_by_xpath("//div[@class='directions-travel-mode-icon directions-drive-icon']")
car.click()
details = driver.find_elements_by_xpath("//div[@class='section-directions-trip-description']")
number_of_routes = len(details)
outputList = [];
for i in range(0, number_of_routes):
details = driver.find_elements_by_xpath("//div[@class='section-directions-trip-description']")
link = details[i]
i = 1
while i < 6:
try:
link.click()
break
except:
pass
i = i + 1
i = 1
while i < 6:
try:
link.click()
break
except:
pass
i = i + 1
time1 = driver.find_element_by_xpath("//h1[@class='section-trip-summary-title']//span[1]//span[1]")
timeOutput1= time1.text
distance1 = driver.find_element_by_xpath("//div[@class='section-trip-summary-header']//span[1]//span[2]")
disOutput1= distance1.text
route1= driver.find_element_by_xpath("//h1[@class='section-directions-trip-title']/child::*")
routeOutput1= route1.text
time.sleep(1)
backButton=driver.find_element_by_xpath("//button[@class='section-trip-header-back maps-sprite-common-arrow-back-white']")
backButton.click()
today = datetime.now()
day=today.strftime("%A")
hour=today.strftime("%H")
minute=today.strftime("%M")
outputList.append({
"source": source,
"destination": dest,
"day": today.strftime("%A"),
"current time": today.strftime("%X"),
"duration": timeOutput1,
"distance": disOutput1,
"route": routeOutput1,
})
db.child("traffic").child(source+" to "+dest).child(day).child(hour+":"+minute).push(outputList)
except TimeoutException:
print("Timed out waiting for page to load")
driver.quit()
driver.close()
return outputList;
cities_list=["colombo", "borella", "rajagiriya", "battaramulla", "koswatta", "malabe", "SLIIT","kaduwela"]
@app.route('/')
def script():
for x in range(0,len(cities_list)):
for y in range(0,len(cities_list)):
if x!=y:
print(cities_list[x]+" to "+cities_list[y])
source=cities_list[x]
destination=cities_list[y]
try:
routes = get_routes(source, destination)
time.sleep(10)
except:
print('error')
schedule.every().hour.at(":00").do(lambda: get_routes(source, destination))and schedule.every().hour.at(":30").do(lambda: get_routes(source, destination))
while True:
schedule.run_pending()
time.sleep(1)
print(type(routes))
return jsonify(routes)
if __name__=='__main__':
app.run(debug=True)