Add function for listing books read in a year

This commit is contained in:
mez 2025-08-12 21:20:59 +01:00
parent 160f503304
commit 7d566c5b85

View file

@ -111,7 +111,6 @@ for file in concernedfiles:
for action in node.children: for action in node.children:
if action.heading == "read": if action.heading == "read":
for book in action.children: for book in action.children:
if "work" not in book.tags:
bookid = re.sub(" and rated .*","",book.heading) bookid = re.sub(" and rated .*","",book.heading)
if bookid not in bookids: if bookid not in bookids:
status = "new" status = "new"
@ -120,16 +119,23 @@ for file in concernedfiles:
status = "existing" status = "existing"
readdate = dateobj readdate = dateobj
if status == "new": if status == "new":
thedict = {"id":bookid,"readdates":[readdate],"progressdates":[],"obtaineddate":""} thedict = {"id":bookid,"readdates":[readdate],"progressdates":[],"obtaineddate":"","work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict) books.append(thedict)
else: else:
twodict = {"id":bookid,"readdates":[readdate]} twodict = {"id":bookid,"readdates":[readdate]}
if "work" in book.tags:
twodict["work"] = True
else:
twodict["work"] = False
for origbook in books: for origbook in books:
if twodict["id"] == origbook["id"]: if twodict["id"] == origbook["id"]:
origbook["readdates"].extend(twodict["readdates"]) origbook["readdates"].extend(twodict["readdates"])
if twodict["work"]:
origbook["work"] = True
if action.heading == "progress": if action.heading == "progress":
for book in action.children: for book in action.children:
if "work" not in book.tags:
bookid = book.heading bookid = book.heading
if bookid not in bookids: if bookid not in bookids:
status = "new" status = "new"
@ -138,16 +144,23 @@ for file in concernedfiles:
status = "existing" status = "existing"
progressdate = dateobj progressdate = dateobj
if status == "new": if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[progressdate],"obtaineddate":""} thedict = {"id":bookid,"readdates":[],"progressdates":[progressdate],"obtaineddate":"","work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict) books.append(thedict)
else: else:
twodict = {"id":bookid,"progressdates":[progressdate]} twodict = {"id":bookid,"progressdates":[progressdate]}
if "work" in book.tags:
twodict["work"] = True
else:
twodict["work"] = False
for origbook in books: for origbook in books:
if twodict["id"] == origbook["id"]: if twodict["id"] == origbook["id"]:
origbook["progressdates"].extend(twodict["progressdates"]) origbook["progressdates"].extend(twodict["progressdates"])
if twodict["work"]:
origbook["work"] = True
if action.heading == "obtained": if action.heading == "obtained":
for book in action.children: for book in action.children:
if "work" not in book.tags:
bookid = re.sub(" \(.*\)","",book.heading) bookid = re.sub(" \(.*\)","",book.heading)
if bookid not in bookids: if bookid not in bookids:
status = "new" status = "new"
@ -156,13 +169,21 @@ for file in concernedfiles:
status = "existing" status = "existing"
obtaineddate = dateobj obtaineddate = dateobj
if status == "new": if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[],"obtaineddate":obtaineddate} thedict = {"id":bookid,"readdates":[],"progressdates":[],"obtaineddate":obtaineddate,"work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict) books.append(thedict)
else: else:
twodict = {"id":bookid,"obtaineddate":[obtaineddate]} twodict = {"id":bookid,"obtaineddate":[obtaineddate]}
if "work" in book.tags:
twodict["work"] = True
else:
twodict["work"] = False
for origbook in books: for origbook in books:
if twodict["id"] == origbook["id"]: if twodict["id"] == origbook["id"]:
origbook["obtaineddate"] = twodict["obtaineddate"] origbook["obtaineddate"] = twodict["obtaineddate"]
if twodict["work"]:
origbook["work"] = True
if node.heading == "films": if node.heading == "films":
for action in node.children: for action in node.children:
if action.heading == "watched": if action.heading == "watched":
@ -1150,6 +1171,7 @@ def bookfeed():
eachdate = enddate eachdate = enddate
while eachdate >= startdate: while eachdate >= startdate:
for book in books: for book in books:
if not book["work"]:
if eachdate in book["readdates"]: if eachdate in book["readdates"]:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Read"}) yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Read"})
try: try:
@ -1169,6 +1191,30 @@ def bookfeed():
feedwrite.write(" </channel>\n</rss>") feedwrite.write(" </channel>\n</rss>")
feedwrite.close() feedwrite.close()
def thisyearbooks(theyear=datetime.today().year):
booksthisyear = []
workbooks = 0
for book in books:
for date in book["readdates"]:
if date.year == theyear:
if book["work"]:
workbooks += 1
else:
bookdict = {"author":book["author"],"title":book["title"]}
for thedate in book["readdates"]:
if thedate.year == theyear:
bookdict["date"] = thedate
booksthisyear.append(bookdict)
booksthisyear = sorted(booksthisyear,key=lambda d: d["date"])
index = 1
for book in booksthisyear:
print(str(index) + ". " + book["author"] + ": " + book["title"])
index += 1
if workbooks > 1:
print("+ " + str(workbooks) + " books read for work")
elif workbooks == 1:
print("+ 1 book read for work")
if __name__ == "__main__": if __name__ == "__main__":
bookfeed() bookfeed()