Skip to content

Commit 313e1dd

Browse files
committed
Close #30, update stand-alone script
1 parent 6feb7ab commit 313e1dd

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

talkmap.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
1-
2-
3-
# # Leaflet cluster map of talk locations
4-
#
5-
# (c) 2016-2017 R. Stuart Geiger, released under the MIT license
1+
# Leaflet cluster map of talk locations
62
#
7-
# Run this from the _talks/ directory, which contains .md files of all your talks.
8-
# This scrapes the location YAML field from each .md file, geolocates it with
9-
# geopy/Nominatim, and uses the getorg library to output data, HTML,
10-
# and Javascript for a standalone cluster map.
11-
#
12-
# Requires: glob, getorg, geopy
13-
3+
# Run this from the _talks/ directory, which contains .md files of all your
4+
# talks. This scrapes the location YAML field from each .md file, geolocates it
5+
# with geopy/Nominatim, and uses the getorg library to output data, HTML, and
6+
# Javascript for a standalone cluster map. This is functionally the same as the
7+
# #talkmap Jupyter notebook.
8+
import frontmatter
149
import glob
1510
import getorg
1611
from geopy import Nominatim
1712

18-
g = glob.glob("*.md")
19-
13+
# Collect the Markdown files
14+
g = glob.glob("_talks/*.md")
2015

21-
geocoder = Nominatim()
16+
# Prepare to geolocate
17+
geocoder = Nominatim(user_agent="academicpages.github.io")
2218
location_dict = {}
2319
location = ""
2420
permalink = ""
2521
title = ""
2622

27-
23+
# Perform geolocation
2824
for file in g:
29-
with open(file, 'r') as f:
30-
lines = f.read()
31-
if lines.find('location: "') > 1:
32-
loc_start = lines.find('location: "') + 11
33-
lines_trim = lines[loc_start:]
34-
loc_end = lines_trim.find('"')
35-
location = lines_trim[:loc_end]
36-
37-
38-
location_dict[location] = geocoder.geocode(location)
39-
print(location, "\n", location_dict[location])
40-
41-
42-
m = getorg.orgmap.create_map_obj()
43-
getorg.orgmap.output_html_cluster_map(location_dict, folder_name="../talkmap", hashed_usernames=False)
25+
# Read the file
26+
data = frontmatter.load(file)
27+
data = data.to_dict()
4428

29+
# Press on if the location is not present
30+
if 'location' not in data:
31+
continue
4532

33+
# Prepare the description
34+
title = data['title'].strip()
35+
venue = data['venue'].strip()
36+
location = data['location'].strip()
37+
description = f"{title}<br />{venue}; {location}"
4638

39+
# Geocode the location and report the status
40+
location_dict[description] = geocoder.geocode(location)
41+
print(description, location_dict[description])
4742

43+
# Save the map
44+
m = getorg.orgmap.create_map_obj()
45+
getorg.orgmap.output_html_cluster_map(location_dict, folder_name="talkmap", hashed_usernames=False)

0 commit comments

Comments
 (0)