-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmaps.py
72 lines (63 loc) · 2.54 KB
/
gmaps.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
import sqlite3
import configparser
import googlemaps
from datetime import datetime, timedelta
class Coordinate:
def __init__(self, latitude, longitude, toMetro, toWork):
self.latitude = latitude
self.longitude = longitude
self.toMetro = toMetro
self.toWork = toWork
class GMaps:
def __init__(self):
self.db = sqlite3.connect('coordinates.db')
cursor = self.db.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS coordinates(
latitude REAL,
longitude REAL,
toMetro INTEGER,
toWork INTEGER
)
''')
self.db.commit()
config = configparser.ConfigParser()
config.read("settings.ini")
apikey = config.get('Default', 'apikey')
self.gmaps = googlemaps.Client(key=apikey)
def insert(self, latitude, longitude, toMetro, toWork):
cursor = self.db.cursor()
cursor.execute('''INSERT INTO coordinates(
latitude,
longitude,
toMetro,
toWork
) VALUES(?,?,?,?)''', (latitude, longitude, toMetro, toWork))
self.db.commit()
def get_coordinates(self):
cursor = self.db.cursor()
cursor.execute('''SELECT latitude, longitude, toMetro, toWork FROM coordinates''')
result = []
for point in cursor:
result.append(Coordinate(point[0], point[1], point[2], point[3]))
return result
def finish(self):
self.db.close()
def _get_transit_time(self, point1: tuple, point2: tuple):
today_date = datetime.now().strftime('%Y-%m-%d 09:00:00')
date = datetime.strptime(today_date, '%Y-%m-%d %H:%M:%S') + timedelta(days=1)
time = date.timestamp()
self.gmaps.directions(str(point1[0]) + ' ' + str(point1[1]),
str(point2[0]) + ' ' + str(point2[1]),
units='metric',
mode="transit",
departure_time=time)
def _get_foot_time(self, point1: tuple, point2: tuple):
today_date = datetime.now().strftime('%Y-%m-%d 09:00:00')
date = datetime.strptime(today_date, '%Y-%m-%d %H:%M:%S') + timedelta(days=1)
time = date.timestamp()
self.gmaps.directions(str(point1[0]) + ' ' + str(point1[1]),
str(point2[0]) + ' ' + str(point2[1]),
units='metric',
mode="transit",
departure_time=time)