From 2886ca7192e4bf9b3e0f7e746c87810071c67861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tr=C3=A9meur?= Date: Fri, 21 Jun 2024 10:25:27 +0100 Subject: [PATCH] Get latitude and longitude automatically --- .gitignore | 1 - README.org | 7 ++-- generateplaces.py | 89 +++++++++++++++---------------------------- variables-template.py | 2 + 4 files changed, 36 insertions(+), 63 deletions(-) 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("\n\n \n \n \n \n Map\n \n \n \n \n \n \n
\n \n \n") - 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("\n\n \n \n \n \n Map\n \n \n \n \n \n \n
\n \n \n") 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"}