add routing and util to the right places
This commit is contained in:
parent
a9b8d4d630
commit
d4f87bed76
3 changed files with 130 additions and 0 deletions
13
crawler/5_routing.py
Normal file
13
crawler/5_routing.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from data_access import Listing
|
||||
from tqdm import tqdm
|
||||
|
||||
listings = Listing.get_all_listings()
|
||||
BROCK_STREET_LAT_LONG = 51.52570434674584, -0.13956495005056113
|
||||
|
||||
for listing in tqdm(listings):
|
||||
lat, long = BROCK_STREET_LAT_LONG
|
||||
listing.calculate_route(lat, long, recalculate=False)
|
||||
traveltime = listing.travel_time[0]
|
||||
duration_minutes = traveltime['duration'] / 60.
|
||||
|
||||
tqdm.write(f"{listing.identifier} {duration_minutes}")
|
||||
100
crawler/rec/routing.py
Normal file
100
crawler/rec/routing.py
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import requests
|
||||
from rec.utils import nextMonday
|
||||
from collections import defaultdict
|
||||
|
||||
API_KEY = 'AIzaSyBoBHzeQFgR7O-NlNsuHXQcC1B7ccEHpl8'
|
||||
url = "https://routes.googleapis.com/directions/v2:computeRoutes"
|
||||
|
||||
|
||||
def transit_route(origin_lat:float, origin_lon:float, dest_lat:float, dest_lon:float, compute_alternative_routes=True):
|
||||
monday9am = nextMonday()
|
||||
|
||||
header = {
|
||||
"X-Goog-Api-Key": API_KEY,
|
||||
"Content-Type": "application/json",
|
||||
"X-Goog-FieldMask": "routes.distanceMeters,routes.duration,routes.staticDuration,routes.legs.steps.distanceMeters,routes.legs.steps.staticDuration,routes.legs.steps.travelMode",
|
||||
}
|
||||
|
||||
body = {
|
||||
"origin":{
|
||||
"location":{
|
||||
"latLng":{
|
||||
"latitude": origin_lat,
|
||||
"longitude": origin_lon
|
||||
}
|
||||
}
|
||||
},
|
||||
"destination":{
|
||||
"location":{
|
||||
"latLng":{
|
||||
"latitude": dest_lat,
|
||||
"longitude": dest_lon
|
||||
}
|
||||
}
|
||||
},
|
||||
"travelMode": "TRANSIT",
|
||||
# "2023-10-15T15:01:23.045123456Z"
|
||||
"departureTime": monday9am.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"computeAlternativeRoutes": compute_alternative_routes,
|
||||
# "routeModifiers": {
|
||||
# "avoidTolls": false,
|
||||
# "avoidHighways": false,
|
||||
# "avoidFerries": false
|
||||
# },
|
||||
"languageCode": "en-US",
|
||||
"units": "METRIC"
|
||||
}
|
||||
|
||||
r = requests.post(url, json=body, headers=header)
|
||||
if r.status_code == 200:
|
||||
return r.json()
|
||||
|
||||
raise Exception(r.json())
|
||||
|
||||
def extract_time(d, limit:int=2):
|
||||
res = []
|
||||
for route in d['routes']:
|
||||
distance = route['distanceMeters']
|
||||
duration = int(route['duration'].strip('s'))
|
||||
duration_static = int(route['staticDuration'].strip('s'))
|
||||
|
||||
steps = route['legs'][0]['steps']
|
||||
duration_per_transit = defaultdict(lambda: 0)
|
||||
distance_per_transit = defaultdict(lambda: 0)
|
||||
number_of_transit_stops = 0
|
||||
|
||||
for step in steps:
|
||||
duration_per_transit[step['travelMode']] += int(step['staticDuration'].strip('s'))
|
||||
distance_per_transit[step['travelMode']] += step.get('distanceMeters', 0)
|
||||
if step['travelMode'] == 'TRANSIT':
|
||||
number_of_transit_stops += 1
|
||||
|
||||
res.append({
|
||||
'duration': duration,
|
||||
'distance': distance,
|
||||
'duration_static': duration_static,
|
||||
'duration_per_transit': dict(duration_per_transit),
|
||||
'distance_per_transit': dict(distance_per_transit),
|
||||
'number_of_transit_stops': number_of_transit_stops,
|
||||
})
|
||||
|
||||
return res[:limit]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import json
|
||||
with open('code/json/routing_routeapi.json', 'r') as f:
|
||||
d = json.load(f)
|
||||
|
||||
extract_time(d)
|
||||
|
||||
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# origin = 51.5635664310333, -0.1107173751570373 # home
|
||||
# dest = 51.50475678313417, 0.04915321000190009 # london city airport
|
||||
# d = travel_time(origin[0], origin[1], dest[0], dest[1])
|
||||
# import json
|
||||
# with open('code/json/routing_routeapi.json', 'w') as f:
|
||||
# json.dump(d, f)
|
||||
|
||||
17
crawler/rec/utils.py
Normal file
17
crawler/rec/utils.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
def nextMonday():
|
||||
"""
|
||||
I think this function doesnt work when the day is monday itself.
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
days_until_monday = (0 - now.weekday() + 7) % 7
|
||||
monday = now + timedelta(days=days_until_monday)
|
||||
monday_9am = monday.replace(hour=9, minute=0, second=0, microsecond=0, tzinfo=timezone.utc)
|
||||
return monday_9am
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(nextMonday())
|
||||
Loading…
Add table
Add a link
Reference in a new issue