From 99ea2cae65fe1a8765ea69ac83d2116c4a4a354c Mon Sep 17 00:00:00 2001 From: Mez Date: Sun, 24 Aug 2025 21:29:17 +0100 Subject: [PATCH] Add pages listing books read in previous years --- generate.py | 58 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/generate.py b/generate.py index 9dc6c47..fead97a 100644 --- a/generate.py +++ b/generate.py @@ -1199,23 +1199,30 @@ def bookfeed(): feedwrite.close() def thisyearbooks(theyear=datetime.today().year): - bookindex = open("build/books/index.html","w") - booksthisyear = [] - workbooks = 0 for book in books: for date in book["readdates"]: if date.year == theyear: - if book["work"]: - workbooks += 1 - else: + if not book["work"]: 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"]) + return booksthisyear +def thisyearworkbooks(theyear=datetime.today().year): + workbooks = 0 + for book in books: + for date in book["readdates"]: + if date.year == theyear: + if book["work"]: + workbooks += 1 + return workbooks + +def booksfrontpage(): + bookindex = open("build/books/index.html","w") bookindex.write("\n\n \n \n \n praze • Books\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n
\n
\n

Currently reading

\n
    \n") for book in books: if not book["work"]: @@ -1225,27 +1232,50 @@ def thisyearbooks(theyear=datetime.today().year): bookindex.write("
  • " + book["title"] + " " + str(book["progress"]) + "%
  • \n") else: bookindex.write("
  • " + book["author"] + " – " + book["title"] + " " + str(book["progress"]) + "%
  • \n") - bookindex.write("
\n
\n
\n

Books read in " + str(theyear) + "

\n") - if len(booksthisyear) > 0: + bookindex.write("\n
\n
\n

Books read in " + str(datetime.today().year) + "

\n") + if len(thisyearbooks()) > 0: bookindex.write("
    \n") - for book in booksthisyear: + for book in thisyearbooks(): if book["author"] == "": bookindex.write("
  1. " + book["title"] + "
  2. \n") else: bookindex.write("
  3. " + book["author"] + " – " + book["title"] + "
  4. \n") bookindex.write("
\n") - if workbooks > 1: - bookindex.write("

+ " + str(workbooks) + " work-related books

\n") - elif workbooks == 1: + if thisyearworkbooks() > 1: + bookindex.write("

+ " + str(thisyearworkbooks()) + " work-related books

\n") + elif thisyearworkbooks() == 1: bookindex.write("

+ 1 work-related book

\n") - if len(booksthisyear) == 0 and workbooks == 0: + if len(thisyearbooks()) == 0 and thisyearworkbooks() == 0: bookindex.write("

None yet!

\n") bookindex.write("
\n
\n \n
\n \n\n") bookindex.close() +def previousyearbooks(): + theyear = datetime.today().year - 1 + while theyear > 1993: + if len(thisyearbooks(theyear)) > 0: + if not os.path.isdir("build/books/" + str(theyear)): + os.mkdir("build/books/" + str(theyear)) + booksyear = open("build/books/" + str(theyear) + "/index.html","w") + booksyear.write("\n\n \n \n \n praze • Books\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n
\n
\n

Books read in " + str(theyear) + "

\n
    \n") + for book in thisyearbooks(theyear): + if book["author"] == "": + booksyear.write("
  1. " + book["title"] + "
  2. \n") + else: + booksyear.write("
  3. " + book["author"] + " – " + book["title"] + "
  4. \n") + booksyear.write("
\n") + if thisyearworkbooks(theyear) > 1: + booksyear.write("

+ " + str(thisyearworkbooks(theyear)) + " work-related books

\n") + elif thisyearworkbooks(theyear) == 1: + booksyear.write("

+ 1 work-related book

\n") + booksyear.write("
\n
\n \n
\n \n\n") + booksyear.close() + theyear -= 1 + if __name__ == "__main__": bookfeed() - thisyearbooks() + booksfrontpage() + previousyearbooks() def filmfeed(): if not os.path.isdir("build/films"):