Created
August 6, 2016 16:48
-
-
Save Gricha/6c229e0ed6e2b9923781067becd72b20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import googlemaps | |
import os | |
import sqlite3 | |
from datetime import datetime | |
def extractDurationInTrafficFromResponse(response): | |
return response[0]['legs'][0]['duration_in_traffic'] | |
def timesBetweenPoints(client, origin, destination): | |
best_guess_response = client.directions(origin, | |
destination, | |
departure_time=datetime.now(), | |
traffic_model="best_guess") | |
pessimistic_response = client.directions(origin, | |
destination, | |
departure_time=datetime.now(), | |
traffic_model="pessimistic") | |
return {"best_guess":extractDurationInTrafficFromResponse(best_guess_response), | |
"pessimistic":extractDurationInTrafficFromResponse(pessimistic_response)} | |
def currentState(client, home, work): | |
""" | |
home/work - 2 element tuple of (latitude, longitude), or a string describing location | |
""" | |
return { | |
"home_to_work": timesBetweenPoints(client, origin = home, destination = work), | |
"work_to_home": timesBetweenPoints(client, destination = home, origin = work), | |
} | |
def initTable(c): | |
c.execute('''create table if not exists durations ( | |
date text, | |
day_of_week integer, | |
traffic_mode text, | |
direction text, | |
duration_in_seconds integer, | |
duration_text text, | |
timestamp datetime, | |
key integer primary key autoincrement | |
)''') | |
def insertToTable(c, timestamp, date, day_of_week, traffic_mode, direction, duration_in_seconds, duration_text): | |
c.execute("insert into durations ('date', 'day_of_week', 'traffic_mode', 'direction', 'duration_in_seconds', 'duration_text', 'timestamp') values(?,?,?,?,?,?,?)", (date, day_of_week, traffic_mode, direction, duration_in_seconds, duration_text, timestamp)) | |
def persistStateInDBOnPath(path, state): | |
timestamp = datetime.now() | |
conn = sqlite3.connect(os.path.join(path, 'db.sqlite')) | |
c = conn.cursor() | |
initTable(c) | |
date = timestamp.strftime("%Y-%m-%d") | |
day_of_week = int(timestamp.strftime("%w")) | |
for direction, data in state.iteritems(): | |
for traffic_mode, durations in data.iteritems(): | |
insertToTable(c, timestamp, date, day_of_week, traffic_mode, direction, durations['value'], durations['text']) | |
conn.commit() | |
conn.close() | |
def main(): | |
# Load up API key, home and work address, keeping it in config.json file | |
# on the same level as script file | |
script_dir = os.path.dirname(os.path.realpath(__file__)) | |
with open(os.path.join(script_dir, "config.json")) as f: | |
config = json.loads(f.read()) | |
f.close() | |
client = googlemaps.Client(key=config['api_key']) | |
home = config['home'] | |
work = config['work'] | |
state = currentState(client, work, home) | |
persistStateInDBOnPath(script_dir, state) | |
# Print anyway | |
print json.dumps(state) | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment