-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
49 lines (42 loc) · 1.6 KB
/
cache.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
import configparser
import geopy
import sqlite3
class Cache(object):
def __init__(self, fn='app.db'):
self.conn = conn = sqlite3.connect(fn)
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS '
'geo ( '
'id INTEGER PRIMARY KEY, '
'location STRING, '
'timezone STRING '
')')
conn.commit()
def timezone_cached(self, location):
cur = self.conn.cursor()
cur.execute('SELECT timezone FROM geo WHERE location=?', (location,))
res = cur.fetchone()
if res is None: return False
return res[0]
def save_to_cache(self, timezone, location):
cur = self.conn.cursor()
cur.execute('INSERT INTO geo(location, timezone) VALUES(?, ?)',
(location, timezone))
self.conn.commit()
if __name__ == '__main__':
"""Test caching of timezones for locations to reduce API calls."""
cache = Cache('app.db')
location = '1 Murphy St, Sunnyvale, CA'
timezone = cache.timezone_cached(location)
if timezone:
print('was cached: {}'.format(timezone.zone))
else:
print('was not cached, looking up and caching now')
config = configparser.ConfigParser()
config.read('config.ini')
geo = geopy.geocoders.GoogleV3(api_key=config['DEFAULT']['GOOGLE_API_KEY'])
place, (lat, lng) = geo.geocode(location)
tz = geo.timezone((lat, lng))
print('found as: {}'.format(tz.zone))
cache.save_to_cache(location, tz.zone)
print('... and now cached.')