Merge branch 'development' of helene.linode:/mnt/repos/books.alexwlchan.net into...
[books.alexwlchan.net] / scripts / add_book.py
index cd59c10..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?")
@@ -123,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