blog.notmyidea.org/isbn_downloader.py
Alexis Métaireau 9df3b183b6 Enhanced UI & UX; Added New ISBN Plugin.
- Added the ability to display book cover for the category "Lectures" if ISBN cover is available.
- Moved author's name into a small tag for better hierarchy and readability.
- Implemented a feature to indicate link sizes depending on the number of articles associated with a given tag.
- Implemented a mini footer element displaying an RSS feed icon.
- Improved category display using description dictionary.
- Added a new plugin "isbn_downloader" to fetch ISBN information when needed.
- Included the count of articles for each category.
- Implemented changes for better layout and readability of tags and categories.
- Adjusted the layout of the webpage, improving the overall look of the page.
- Included "requests" in the requirements.txt for supplanting dependencies required by the new plugin and/or features.
2023-09-29 18:30:09 +02:00

57 lines
1.7 KiB
Python

import logging
import os
from pathlib import Path
import requests
from pelican import log, signals
log = logging.getLogger(__name__)
def check_or_download_image(isbn, output_dir):
log.info(f"Downloading cover for {isbn=}")
output_dir = Path(output_dir)
# remote_url = f"https://images.isbndb.com/covers/41/83/{isbn}.jpg"
image_relative_path = f"images/isbn-covers/{isbn}.jpg"
image_path = output_dir / image_relative_path
if image_path.exists():
return image_relative_path
else:
try:
remote_url = f"https://www.googleapis.com/books/v1/volumes?q=isbn:{isbn}"
api_resp = requests.get(remote_url)
api_resp.raise_for_status()
returned = api_resp.json()
if returned["totalItems"] > 0:
thumbnail_url = returned["items"][0]["volumeInfo"]["imageLinks"][
"thumbnail"
]
response = requests.get(thumbnail_url, stream=True)
os.makedirs(os.path.dirname(image_path), exist_ok=True)
with open(image_path, "wb") as fp:
for chunk in response.iter_content(chunk_size=8192):
fp.write(chunk)
return image_relative_path
except:
log.error(
f"No ISBN cover found for {isbn=}. Look at https://isbnsearch.org/isbn/{isbn}"
)
return
def run_for_each_article(generator):
for article in generator.articles:
isbn = getattr(article, "isbn", None)
if isbn:
isbn = isbn.replace("-", "")
article.isbn_cover = check_or_download_image(isbn, generator.path)
def register():
signals.article_generator_finalized.connect(run_for_each_article)