import os from pathlib import Path from random import randint from datetime import datetime thisyear = datetime.now().strftime("%Y") home = str(Path.home()) stats = [] rates = [] rating = [] rated = [] def rate(line): if "and rated" in line: rated.append(1) if "★★★★★" in line: rating.append(5) elif "★★★★" in line: rating.append(4) elif "★★★" in line: rating.append(3) elif "★★" in line: rating.append(2) elif "★" in line: rating.append(1) def iterate(category,action,year=thisyear,date=False): basedir = home + "/Documents/drive/org/journal/" + str(year) count = 0 print("* " + category) print("** " + action) month = 1 while True: if month < 13: if month < 10: monthname = basedir + "/0" + str(month) else: monthname = basedir + "/" + str(month) if os.path.exists(monthname): for file in sorted(os.listdir(monthname)): filename = monthname + "/" + str(file) if filename.endswith(".org"): with open(filename, "r") as file: lines = file.readlines() for num, line in enumerate (lines, 0): if str("** " + str(action)) in line: num += 1 if num + 1 < len(lines): if "*** " in lines[num]: while True: if "*** " in lines[num]: if date == True: print(lines[num].strip() + " " + lines[0].strip()) else: print(lines[num].strip()) rate(lines[num]) count += 1 else: break if num + 1 < len(lines): num += 1 else: break else: break else: break else: if num < len(lines): if "*** " in lines[num]: if date == True: print(lines[num].strip() + " " + lines[0].strip()) else: print(lines[num].strip()) rate(lines[num]) count += 1 else: break else: break month += 1 stats.append(count) if (sum(rated)) != 0: avg = round((sum(rating)/sum(rated)),2) else: avg = "n/a" rates.append(avg) rating.clear() rated.clear() def yeargrab(year=thisyear): iterate("books","read",year) iterate("films","watched",year) print("* stats") print("** books read: " + str(stats[0]) + ", average rating: " + str(rates[0])) print("** films watched: " + str(stats[1]) + ", average rating: " + str(rates[1])) if __name__ == "__main__": yeargrab()