import orgparse,os,re,sys,variables from datetime import datetime from geopy.geocoders import Nominatim try: if sys.argv[1] == "local": local = True else: local = False except: local = False if variables.trackplaces == True: thisyear = datetime.now().strftime("%Y") year = variables.placestartyear concernedfiles = [] geo = Nominatim(user_agent="python3") while year < int(thisyear) + 1: month = 0 while month < 13: if month < 10: strmonth = "0" + str(month) else: strmonth = str(month) recdir = str(year) + "/" + strmonth + "/" fullpath = variables.orgpath + recdir if os.path.exists(fullpath): for file in sorted(os.listdir(fullpath)): filename = fullpath + str(file) if filename.endswith(".org"): concernedfiles.append(filename) month = month + 1 year = year + 1 places = [] placenames = [] for file in concernedfiles: filedate = file[-14:-4] dateobj = datetime.strptime(filedate,"%Y-%m-%d") parsefile = orgparse.load(file) try: for node in parsefile.children: if node.heading == "places": for action in node.children: if action.heading == "visited": for place in action.children: placename = re.sub(" <.*>","",re.sub("<.*> ","",place.heading)) if placename not in placenames: status = "new" placenames.append(placename) else: status = "existing" if "<" in place.heading: dates = re.sub(" [A-Z][a-z][a-z]","",re.sub("--"," to ",re.sub(">","",re.sub("<","",(re.findall("\<.*\>",place.heading)[0]))))) else: dates = filedate if status == "new": 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]} for origplace in places: if twodict["name"] == origplace["name"]: origplace["dates"].extend(twodict["dates"]) except: pass for place in places: place["dates"] = list(dict.fromkeys(place["dates"])) theplaces = sorted(places,key=lambda d: d["name"]) 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