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,58 +111,79 @@ for file in concernedfiles:
for action in node.children:
if action.heading == "read":
for book in action.children:
if "work" not in book.tags:
bookid = re.sub(" and rated .*","",book.heading)
if bookid not in bookids:
status = "new"
bookids.append(bookid)
bookid = re.sub(" and rated .*","",book.heading)
if bookid not in bookids:
status = "new"
bookids.append(bookid)
else:
status = "existing"
readdate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[readdate],"progressdates":[],"obtaineddate":"","work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict)
else:
twodict = {"id":bookid,"readdates":[readdate]}
if "work" in book.tags:
twodict["work"] = True
else:
status = "existing"
readdate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[readdate],"progressdates":[],"obtaineddate":""}
books.append(thedict)
else:
twodict = {"id":bookid,"readdates":[readdate]}
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["readdates"].extend(twodict["readdates"])
twodict["work"] = False
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["readdates"].extend(twodict["readdates"])
if twodict["work"]:
origbook["work"] = True
if action.heading == "progress":
for book in action.children:
if "work" not in book.tags:
bookid = book.heading
if bookid not in bookids:
status = "new"
bookids.append(bookid)
bookid = book.heading
if bookid not in bookids:
status = "new"
bookids.append(bookid)
else:
status = "existing"
progressdate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[progressdate],"obtaineddate":"","work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict)
else:
twodict = {"id":bookid,"progressdates":[progressdate]}
if "work" in book.tags:
twodict["work"] = True
else:
status = "existing"
progressdate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[progressdate],"obtaineddate":""}
books.append(thedict)
else:
twodict = {"id":bookid,"progressdates":[progressdate]}
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["progressdates"].extend(twodict["progressdates"])
twodict["work"] = False
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["progressdates"].extend(twodict["progressdates"])
if twodict["work"]:
origbook["work"] = True
if action.heading == "obtained":
for book in action.children:
if "work" not in book.tags:
bookid = re.sub(" \(.*\)","",book.heading)
if bookid not in bookids:
status = "new"
bookids.append(bookid)
bookid = re.sub(" \(.*\)","",book.heading)
if bookid not in bookids:
status = "new"
bookids.append(bookid)
else:
status = "existing"
obtaineddate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[],"obtaineddate":obtaineddate,"work":False}
if "work" in book.tags:
thedict["work"] = True
books.append(thedict)
else:
twodict = {"id":bookid,"obtaineddate":[obtaineddate]}
if "work" in book.tags:
twodict["work"] = True
else:
status = "existing"
obtaineddate = dateobj
if status == "new":
thedict = {"id":bookid,"readdates":[],"progressdates":[],"obtaineddate":obtaineddate}
books.append(thedict)
else:
twodict = {"id":bookid,"obtaineddate":[obtaineddate]}
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["obtaineddate"] = twodict["obtaineddate"]
twodict["work"] = False
for origbook in books:
if twodict["id"] == origbook["id"]:
origbook["obtaineddate"] = twodict["obtaineddate"]
if twodict["work"]:
origbook["work"] = True
if node.heading == "films":
for action in node.children:
if action.heading == "watched":
@ -1150,15 +1171,16 @@ def bookfeed():
eachdate = enddate
while eachdate >= startdate:
for book in books:
if eachdate in book["readdates"]:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Read"})
try:
if book["progressdates"][0] == eachdate:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Reading"})
except:
pass
if book["obtaineddate"] == eachdate:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Added"})
if not book["work"]:
if eachdate in book["readdates"]:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Read"})
try:
if book["progressdates"][0] == eachdate:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Reading"})
except:
pass
if book["obtaineddate"] == eachdate:
yearlist.append({"date":eachdate,"author":book["author"],"title":book["title"].replace("&","&"),"action":"Added"})
eachdate -= timedelta(days=1)
checkdate = enddate
for event in yearlist:
@ -1169,6 +1191,30 @@ def bookfeed():
feedwrite.write(" </channel>\n</rss>")
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__":
bookfeed()