Merge branch 'development' of helene.linode:/mnt/repos/books.alexwlchan.net into...
[books.alexwlchan.net] / scripts / add_book.py
index 8a05345..aa2f7fa 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 import datetime
 import os
@@ -68,10 +68,10 @@ def get_review_info():
     today = datetime.datetime.now()
 
     if date_read == "today":
-        date_read = today.strftime("%Y-%m-%d")
+        date_read = today.date()
     elif date_read == "yesterday":
         yesterday = today - datetime.timedelta(days=1)
-        date_read = yesterday.strftime("%Y-%m-%d")
+        date_read = yesterday.date()
     else:
         date_read_question2 = [
             inquirer.Text("date_read", message="When did you finish reading it?")
@@ -79,6 +79,13 @@ def get_review_info():
 
         date_read = inquirer.prompt(date_read_question2)["date_read"]
 
+        if re.match(r"^\d{4}-\d{2}-\d{2}$", date_read.strip()):
+            date_read = datetime.datetime.strptime(date_read, "%Y-%m-%d").date()
+        elif re.match(r"^\d{1,2} [A-Z][a-z]+ \d{4}$", date_read.strip()):
+            date_read = datetime.datetime.strptime(date_read, "%d %B %Y").date()
+        else:
+            sys.exit("Unrecognised date: {date_read}")
+
     other_questions = [
         inquirer.List(
             "rating",
@@ -105,7 +112,7 @@ def get_review_info():
             ),
         ]
 
-        did_not_finish = (inquirer.prompt(questions)["did_you_finish"] == "no")
+        did_not_finish = inquirer.prompt(questions)["did_you_finish"] == "no"
 
     return {
         "date_read": date_read,
@@ -116,12 +123,26 @@ def get_review_info():
 
 
 def save_cover(slug, cover_image_url):
-    filename, _ = urlretrieve(cover_image_url)
+    filename, headers = urlretrieve(cover_image_url)
+
+    if headers["Content-Type"] == "image/jpeg":
+        extension = ".jpg"
+    elif headers["Content-Type"] == "image/png":
+        extension = ".png"
+    elif headers["Content-Type"] == "image/gif":
+        extension = ".gif"
+    else:
+        print(headers)
+        assert 0
+
+        url_path = hyperlink.URL.from_text(cover_image_url).path
+        extension = os.path.splitext(url_path[-1])[-1]
 
-    url_path = hyperlink.URL.from_text(cover_image_url).path
-    extension = os.path.splitext(url_path[-1])[-1]
     cover_name = f"{slug}{extension}"
-    os.rename(filename, f"src/covers/{cover_name}")
+    dst_path = f"src/covers/{cover_name}"
+
+    if not os.path.exists(dst_path):
+        os.rename(filename, f"src/covers/{cover_name}")
 
     return cover_name
 
@@ -158,8 +179,13 @@ if __name__ == "__main__":
         if review_info["did_not_finish"]:
             new_entry["review"]["did_not_finish"] = True
 
-        year = review_info["date_read"][:4]
+        year = review_info["date_read"].year
         out_dir = f"reviews/{year}"
+    elif book_info["entry_type"] == "plans":
+        new_entry["plan"] = {
+            "date_added": datetime.datetime.now().date(),
+        }
+        out_dir = "plans"
     else:
         out_dir = book_info["entry_type"]
 
@@ -171,3 +197,7 @@ if __name__ == "__main__":
         out_file.write(b"\n")
 
     subprocess.check_call(["open", out_path])
+
+    from render_html import main
+
+    main()