-
Notifications
You must be signed in to change notification settings - Fork 115
/
annotate_locations.py
49 lines (42 loc) · 1.2 KB
/
annotate_locations.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
# curl 'http://nominatim.openstreetmap.org/search?format=json&q=Georges-Kohler-Allee%20106%20Freiburg%20im%20Breisgau' | jq '.[0]'
import re
import frontmatter
import glob
import requests
import urllib.parse
import time
SERVER = 'http://nominatim.openstreetmap.org/search'
for file in glob.glob('_events/*.md'):
with open(file, 'r') as f:
post = frontmatter.load(f)
loc = post.metadata['location']
if loc == 'online':
continue
if 'geo' in post.metadata['location']:
continue
query = ' '.join(map(str, [
loc.get('street', ''),
loc.get('city', ''),
loc.get('postal', ''),
loc.get('region', ''),
loc.get('country', ''),
]))
query = re.sub(r'\s+', ' ', query).strip()
r = requests.get(SERVER, {'format': 'json','q': query})
print(r.url)
d = r.json()
print(query, d)
if len(d) == 0:
time.sleep(1)
continue
post.metadata['location']['geo'] = {
'lat': d[0]['lat'],
'lon': d[0]['lon']
}
with open(file, 'w') as f:
w = frontmatter.dumps(post)
if isinstance(w, str):
f.write(w)
else:
f.write(w.decode('utf-8'))
time.sleep(1)