Add api key for gbooks import

This commit is contained in:
2026-03-16 20:42:14 +01:00
parent 0083e3d896
commit b06ceb0847
2 changed files with 9 additions and 48 deletions

View File

@@ -274,20 +274,23 @@ def search_books(
@book.command("import")
@click.argument("isbn")
@click.option("--owner", required=True, help="Username of book owner")
@click.option("--owner", help="Username of book owner")
@click.option("--place", help="Location place")
@click.option("--bookshelf", help="Bookshelf name")
@click.option("--shelf", type=int, help="Shelf number")
def import_book(
isbn: str,
owner: str,
owner: str | None = None,
place: str | None = None,
bookshelf: str | None = None,
shelf: int | None = None,
) -> None:
"""Import book data from ISBN using Google Books API."""
app = get_app()
user_id = ensure_user_exists(app, owner)
if owner:
user_id = ensure_user_exists(app, owner)
else:
user_id = None
with app.app_context():
try:

View File

@@ -1,53 +1,10 @@
from os import environ
from datetime import date, datetime
from typing import Any
import requests
from pydantic import BaseModel, field_validator
# {
# "title": "Concilio de Sombras (Sombras de Magia 2)",
# "authors": [
# "Victoria Schwab"
# ],
# "publisher": "Urano World",
# "publishedDate": "2023-11-14",
# "description": "Four months have passed since the shadow stone fell into Kell's possession. Four months since his path was crossed with Delilah Bard. Four months since Rhy was wounded and the Dane twins fell, and the stone was cast with Holland's dying body through the rift, and into Black London. In many ways, things have almost returned to normal, though Rhy is more sober, and Kell is now plagued by his guilt. Restless, and having been given up smuggling, Kell is visited by dreams of ominous magical events, waking only to think of Lila, who disappeared from the docks like she always meant to do. As Red London finalizes preparations for the Element Games-an extravagant international competition of magic, meant to entertain and keep the ties between neighboring countries healthy- a certain pirate ship draws closer, carrying old friends back into port. But while Red London is caught up in the pageantry and thrills of the Games, another London is coming back to life, and those who were thought to be forever gone have returned. After all, a shadow that was gone in the night reappears in the morning, and so it seems Black London has risen again-and so to keep magic's balance, another London must fall.",
# "industryIdentifiers": [
# {
# "type": "ISBN_10",
# "identifier": "8419030503"
# },
# {
# "type": "ISBN_13",
# "identifier": "9788419030504"
# }
# ],
# "readingModes": {
# "text": false,
# "image": false
# },
# "pageCount": 0,
# "printType": "BOOK",
# "categories": [
# "Fiction"
# ],
# "maturityRating": "NOT_MATURE",
# "allowAnonLogging": false,
# "contentVersion": "preview-1.0.0",
# "panelizationSummary": {
# "containsEpubBubbles": false,
# "containsImageBubbles": false
# },
# "imageLinks": {
# "smallThumbnail": "http://books.google.com/books/content?id=GM4G0AEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
# "thumbnail": "http://books.google.com/books/content?id=GM4G0AEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
# },
# "language": "es",
# "previewLink": "http://books.google.es/books?id=GM4G0AEACAAJ&dq=isbn:9788419030504&hl=&cd=1&source=gbs_api",
# "infoLink": "http://books.google.es/books?id=GM4G0AEACAAJ&dq=isbn:9788419030504&hl=&source=gbs_api",
# "canonicalVolumeLink": "https://books.google.com/books/about/Concilio_de_Sombras_Sombras_de_Magia_2.html?hl=&id=GM4G0AEACAAJ"
# }
class GoogleBook(BaseModel):
title: str
@@ -83,8 +40,9 @@ class GoogleBook(BaseModel):
def fetch_google_book_data(isbn: str) -> GoogleBook:
api_key = environ.get("GOOGLE_BOOKS_API_KEY")
req = requests.get(
"https://www.googleapis.com/books/v1/volumes", params={"q": f"isbn:{isbn}"}
"https://www.googleapis.com/books/v1/volumes", params={"q": f"isbn:{isbn}", "key": api_key}
)
req.raise_for_status()
data = req.json()