diff --git a/.gitignore b/.gitignore index 997bc77..0793b3c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,4 @@ gamebuild/* !gamebuild/*.png placebuild/* !placebuild/.gitkeep -places.py variables.py \ No newline at end of file diff --git a/README.org b/README.org index de4e97a..3b49572 100644 --- a/README.org +++ b/README.org @@ -3,8 +3,9 @@ * About The scripts in this repository take information from a structured set of =.org= files and use them to build static webpages that track various activities (so far, gaming and travelling). These pages can be used as a replacement for third-party services e.g. Backloggery. * Dependencies -- =python3= +- =geopy= if you are tracking places visited (install via pip) - =orgparse= (install via pip) +- =python3= - =rclone= (for syncing to the server) * General setup ** Structure of the .org files @@ -40,8 +41,8 @@ In =variables.py=, edit: - =placestartyear=: year from which to begin tracking (an integer) - =placecss=: location of the CSS file you wish to apply, relative to the site root ** Testing -- Run =python3 generateplaces.py local= to build in =placebuild=. -- If new places have been added since the last build, they will be added to =places.py= and the terminal will signal this. Add the latitude and longitude for these places in =places.py= before building again. +- Run =python3 generateplaces.py local= to build in =placebuild=. (This may take some time.) +- Check whether places are displayed accurately in the output file. If not, add regions/countries to disambiguate any incorrectly displayed places to the =disambig= dictionary in =variables.py= (see examples). * First run (or if adding any new trackers) - Run =python3 init.py= and then =chmod +x build.sh=. - Create directories on the server corresponding to any =*serverpath= variables you have set. diff --git a/generateplaces.py b/generateplaces.py index 78a5804..89ce26c 100644 --- a/generateplaces.py +++ b/generateplaces.py @@ -1,6 +1,6 @@ import orgparse,os,re,sys,variables from datetime import datetime -import sys,variables +from geopy.geocoders import Nominatim try: if sys.argv[1] == "local": @@ -10,7 +10,7 @@ try: except: local = False -if variables.trackgames == True: +if variables.trackplaces == True: thisyear = datetime.now().strftime("%Y") @@ -18,6 +18,8 @@ if variables.trackgames == True: concernedfiles = [] + geo = Nominatim(user_agent="python3") + while year < int(thisyear) + 1: month = 0 while month < 13: @@ -59,7 +61,11 @@ if variables.trackgames == True: else: dates = filedate if status == "new": - thedict = {"name":placename,"dates":[dates]} + try: + lookup = placename + ", " + variables.disambig[placename] + except: + lookup = placename + thedict = {"name":placename,"dates":[dates],"lat":geo.geocode(lookup).latitude,"long":geo.geocode(lookup).longitude} places.append(thedict) else: twodict = {"name":placename,"dates":[dates]} @@ -74,59 +80,24 @@ if variables.trackgames == True: theplaces = sorted(places,key=lambda d: d["name"]) - if os.path.isfile("places.py"): - import places - newplaces = [] - for place in theplaces: - for location in places.places: - if place["name"] == location["name"]: - break - else: - newplaces.append(place["name"]) - if len(newplaces) > 0: - with open("places.py","rb+") as editfile: - editfile.seek(-2, os.SEEK_END) - editfile.truncate() - appendfile = open("places.py","a") - for place in newplaces: - appendfile.write(",\n{\"name\":\"" + place + "\",\"lat\":0,\"long\":0}") - appendfile.write("]") - appendfile.close() - print("Add latitudes and longitudes in places.py before running again") - else: - alllats = [] - alllongs = [] - import places - for place in places.places: - if place["lat"] != 0: - alllats.append(place["lat"]) - if place["long"] != 0: - alllongs.append(place["long"]) - avglat = (max(alllats) + min(alllats))/2 - avglong = (max(alllongs) + min(alllongs))/2 - writefile = open("placebuild/index.html","w") - writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"") - if local: - writefile.write(variables.domain) - writefile.write(variables.placecss + "\"/>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '© <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n") - for place in theplaces: - for location in places.places: - if location["name"] == place["name"]: - thelat = location["lat"] - thelong = location["long"] - saniname = re.sub(" ","",(re.sub("’","",(re.sub("-","",place["name"]))))) - writefile.write(" const " + saniname + " = L.marker([" + str(thelat) + ", " + str(thelong) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>") - for date in place["dates"]: - writefile.write("<li><code>" + date + "</code></li>") - writefile.write("</ul>');\n") - writefile.write(" </script>\n </body>\n</html>") - else: - writefile = open("places.py","w") - writefile.write("places = [") - for place in theplaces: - writefile.write("{\"name\":\"" + place["name"] + "\",\"lat\":0,\"long\":0}") - if place != theplaces[-1]: - writefile.write(",\n") - writefile.write("]") - writefile.close() - print("Add latitudes and longitudes in places.py before running again") + alllats = [] + alllongs = [] + for place in theplaces: + if place["lat"] != 0: + alllats.append(place["lat"]) + if place["long"] != 0: + alllongs.append(place["long"]) + avglat = (max(alllats) + min(alllats))/2 + avglong = (max(alllongs) + min(alllongs))/2 + writefile = open("placebuild/index.html","w") + writefile.write("<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <base target=\"_top\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> \n <title>Map</title>\n <link rel=\"stylesheet\" href=\"") + if local: + writefile.write(variables.domain) + writefile.write(variables.placecss + "\"/>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.css\" integrity=\"sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=\" crossorigin=\"\"/>\n <script src=\"https://unpkg.com/leaflet@1.9.4/dist/leaflet.js\" integrity=\"sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=\" crossorigin=\"\"></script>\n <style>\n #map {\n width: 90vw;\n height: 90vh;\n margin: auto;\n }\n </style>\n </head>\n <body>\n <div id=\"map\" style=\"width: 90vw; height: 90vh;\"></div>\n <script>\n const map = L.map('map').fitBounds([[" + str(min(alllats)) + ", " + str(min(alllongs)) + "],[" + str(max(alllats)) + ", " + str(max(alllongs)) + "]]);\n const tiles = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {\n attribution: '© <a href=\"http://www.openstreetmap.org/copyright\">OpenStreetMap</a>'\n }).addTo(map);\n") + for place in theplaces: + saniname = re.sub(" ","",(re.sub("’","",(re.sub("-","",place["name"]))))) + writefile.write(" const " + saniname + " = L.marker([" + str(place["lat"]) + ", " + str(place["long"]) + "]).addTo(map).bindTooltip('<h2>" + place["name"] + "</h2><ul>") + for date in place["dates"]: + writefile.write("<li><code>" + date + "</code></li>") + writefile.write("</ul>');\n") + writefile.write(" </script>\n </body>\n</html>") diff --git a/variables-template.py b/variables-template.py index 360de53..e051c84 100644 --- a/variables-template.py +++ b/variables-template.py @@ -46,3 +46,5 @@ placeserverpath = "" placestartyear = 0 placecss = "" + +# disambig = {"london":"england","belfast":"northern ireland"}