Handle cover image URLs with query parameters
authorAlex Chan <alex@alexwlchan.net>
Sun, 2 Feb 2020 19:44:24 +0000 (19:44 +0000)
committerAlex Chan <alex@alexwlchan.net>
Sun, 2 Feb 2020 19:44:24 +0000 (19:44 +0000)
requirements.in
requirements.txt
scripts/add_book.py
src/covers/clouds-cannot-cover-us.jpg [new file with mode: 0644]
src/plans/clouds-cannot-cover-us.md [new file with mode: 0644]

index f2f0f6f..70b9797 100644 (file)
@@ -1,4 +1,5 @@
 attrs
+hyperlink
 inquirer
 jinja2
 Markdown
index f83a622..bb0caed 100644 (file)
@@ -6,6 +6,8 @@
 #
 attrs==19.3.0
 blessings==1.7            # via inquirer
+hyperlink==19.0.0
+idna==2.8                 # via hyperlink
 inquirer==2.6.3
 jinja2==2.11.1
 markdown==3.1.1
index 2cf6da7..8351c64 100755 (executable)
@@ -7,6 +7,7 @@ import subprocess
 from urllib.request import urlretrieve
 
 import frontmatter
+import hyperlink
 import inquirer
 from unidecode import unidecode
 
@@ -93,15 +94,23 @@ def get_review_info():
     return {"date_read": date_read, "rating": rating, "format": answers["format"]}
 
 
+def save_cover(slug, cover_image_url):
+    filename, _ = urlretrieve(cover_image_url)
+
+    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}")
+
+    return cover_name
+
+
 if __name__ == "__main__":
     book_info = get_book_info()
 
     slug = slugify(book_info["title"])
 
-    filename, _ = urlretrieve(book_info["cover_image_url"])
-    extension = os.path.splitext(book_info["cover_image_url"])[-1]
-    cover_name = f"{slug}{extension}"
-    os.rename(filename, f"src/covers/{cover_name}")
+    cover_name = save_cover(slug=slug, cover_image_url=book_info["cover_image_url"])
 
     new_entry = {
         "book": {
diff --git a/src/covers/clouds-cannot-cover-us.jpg b/src/covers/clouds-cannot-cover-us.jpg
new file mode 100644 (file)
index 0000000..753ca35
Binary files /dev/null and b/src/covers/clouds-cannot-cover-us.jpg differ
diff --git a/src/plans/clouds-cannot-cover-us.md b/src/plans/clouds-cannot-cover-us.md
new file mode 100644 (file)
index 0000000..5d6d16a
--- /dev/null
@@ -0,0 +1,8 @@
+---
+book:
+  author: Jay Hulme
+  cover_image: clouds-cannot-cover-us.jpg
+  isbn13: '978-1-912745-10-4 '
+  publication_year: '2019'
+  title: Clouds Cannot Cover Us
+---