import os, orgparse, re from pathlib import Path from datetime import datetime home = str(Path.home()) thisyear = datetime.now().strftime("%Y") basedir = home + "/Documents/drive/org/journal/" year = 2016 concernedfiles = [] 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 = basedir + 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 games = [] gamenames = [] holding = [] 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 == "games": for action in node.children: for game in action.children: name = re.sub(" \(.*\)","",game.heading) if game.heading not in gamenames: status = "new" gamenames.append(game.heading) else: status = "existing" console = (re.findall("\(.*\)",game.heading)[0])[1:-1] if status == "new": games.append({"id":game.heading,"name":name,"initialdate":dateobj,"console":console,game.parent.heading:dateobj,"recent":game.body,"lastupdate":dateobj}) else: thedict = {"id":game.heading,game.parent.heading:dateobj,"lastupdate":dateobj} if len(game.body) > 1: thedict.update({"recent":game.body}) holding.append(thedict) except: pass for gameupdate in holding: for origgame in games: if gameupdate["id"] == origgame["id"]: gamemerge = {**origgame, **gameupdate} games.remove(origgame) games.insert(0,gamemerge) games = sorted(games,key=lambda d: d["lastupdate"]) def theresults(): newgames = sorted(games,key=lambda d: d["id"]) for game in newgames: try: if (game["completed"]): print(game["id"] + ": completed " + (datetime.strftime(game["completed"],"%-d %B %Y"))) if len(game["recent"]) > 1: print("Notes: " + game["recent"]) except: try: if (game["beaten"]): print(game["id"] + ": beaten " + (datetime.strftime(game["beaten"],"%-d %B %Y"))) if len(game["recent"]) > 1: print("Notes: " + game["recent"]) except: try: if (game["gameplay"]): print(game["id"] + ": last played " + (datetime.strftime(game["gameplay"],"%-d %B %Y"))) if len(game["recent"]) > 1: print("Notes: " + game["recent"]) except: print(game["id"] + ": no data") print("") if __name__ == "__main__": theresults()