|
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 |
6 | 2 | #
|
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 |
14 | 9 | import glob
|
15 | 10 | import getorg
|
16 | 11 | from geopy import Nominatim
|
17 | 12 |
|
18 |
| -g = glob.glob("*.md") |
19 |
| - |
| 13 | +# Collect the Markdown files |
| 14 | +g = glob.glob("_talks/*.md") |
20 | 15 |
|
21 |
| -geocoder = Nominatim() |
| 16 | +# Prepare to geolocate |
| 17 | +geocoder = Nominatim(user_agent="academicpages.github.io") |
22 | 18 | location_dict = {}
|
23 | 19 | location = ""
|
24 | 20 | permalink = ""
|
25 | 21 | title = ""
|
26 | 22 |
|
27 |
| - |
| 23 | +# Perform geolocation |
28 | 24 | 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() |
44 | 28 |
|
| 29 | + # Press on if the location is not present |
| 30 | + if 'location' not in data: |
| 31 | + continue |
45 | 32 |
|
| 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}" |
46 | 38 |
|
| 39 | + # Geocode the location and report the status |
| 40 | + location_dict[description] = geocoder.geocode(location) |
| 41 | + print(description, location_dict[description]) |
47 | 42 |
|
| 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