Add google books provider
This commit is contained in:
parent
cf18eae3b1
commit
e48339e245
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.venv
|
.venv
|
||||||
data
|
data
|
||||||
**/__pycache__
|
**/__pycache__
|
||||||
|
.google-books-key
|
@ -9,6 +9,13 @@ app = Flask(__name__)
|
|||||||
app.config["DATABASE"] = "data/books.sqlite"
|
app.config["DATABASE"] = "data/books.sqlite"
|
||||||
app.config["SECRET_KEY"] = secrets.token_hex()
|
app.config["SECRET_KEY"] = secrets.token_hex()
|
||||||
|
|
||||||
|
if not os.path.exists(".google-books-key"):
|
||||||
|
print("No Google books api key found")
|
||||||
|
app.config["GOOGLE_BOOKS_KEY"] = None
|
||||||
|
else:
|
||||||
|
with open(".google-books-key") as f:
|
||||||
|
app.config["GOOGLE_BOOKS_KEY"] = f.read().strip()
|
||||||
|
|
||||||
app.register_blueprint(app_bp)
|
app.register_blueprint(app_bp)
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,16 +35,26 @@ def submit_isbn():
|
|||||||
return "Pas le bon format de QR code"
|
return "Pas le bon format de QR code"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
book.load()
|
book = isbn_db.get_book(book.isbn)
|
||||||
except KeyError:
|
assert isbn_db.add_book(book) == "duplicate" # duplicate
|
||||||
isbn_db.add_book(book)
|
|
||||||
announce_book(book)
|
|
||||||
return "Pas trouvé, à rajouter manuellement"
|
|
||||||
|
|
||||||
print("Got ", book)
|
|
||||||
if isbn_db.add_book(book) == "duplicate":
|
|
||||||
announce_book(isbn_db.get_book(book.isbn), msg_type="update_book")
|
announce_book(isbn_db.get_book(book.isbn), msg_type="update_book")
|
||||||
return f"{book.title} ajouté (plusieurs occurrences)"
|
return f"{book.title} ajouté (plusieurs occurrences)"
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
book.load(loader="google_books", config=current_app.config)
|
||||||
|
print("Got ", book, "[Google]")
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
book.load(loader="openlibrary", config=current_app.config)
|
||||||
|
print("Got ", book, "[OpenLibrary]")
|
||||||
|
except KeyError:
|
||||||
|
isbn_db.add_book(book)
|
||||||
|
announce_book(book)
|
||||||
|
return "Pas trouvé, à rajouter manuellement"
|
||||||
|
|
||||||
|
isbn_db.add_book(book)
|
||||||
announce_book(book)
|
announce_book(book)
|
||||||
return f"{book.title} ajouté"
|
return f"{book.title} ajouté"
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ class Book:
|
|||||||
self.author = None
|
self.author = None
|
||||||
self.count = -1
|
self.count = -1
|
||||||
|
|
||||||
def _openlibrary_load(self):
|
def _openlibrary_load(self, _):
|
||||||
r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json")
|
r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json")
|
||||||
|
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
@ -42,6 +42,36 @@ class Book:
|
|||||||
if "publish_date" in isbn_data["details"]:
|
if "publish_date" in isbn_data["details"]:
|
||||||
self.publish_date = isbn_data["details"]["publish_date"]
|
self.publish_date = isbn_data["details"]["publish_date"]
|
||||||
|
|
||||||
|
|
||||||
|
def _google_books_load(self, config):
|
||||||
|
if config["GOOGLE_BOOKS_KEY"] is None:
|
||||||
|
key_param = ""
|
||||||
|
else:
|
||||||
|
key_param = f"&key={config['GOOGLE_BOOKS_KEY']}"
|
||||||
|
|
||||||
|
r = requests.get(f"https://www.googleapis.com/books/v1/volumes?q=isbn:{self.isbn}{key_param}")
|
||||||
|
|
||||||
|
if r.status_code != 200:
|
||||||
|
raise requests.RequestException(f"Got an error on the request. Status code: {r.status_code}")
|
||||||
|
|
||||||
|
data = json.loads(r.content)
|
||||||
|
|
||||||
|
if data["totalItems"] == 0:
|
||||||
|
raise KeyError("Not found in Google Books")
|
||||||
|
|
||||||
|
item = data["items"][0]
|
||||||
|
|
||||||
|
self.title = item["volumeInfo"]["title"]
|
||||||
|
|
||||||
|
if "publisher" in item["volumeInfo"]:
|
||||||
|
self.publisher = item["volumeInfo"]["publisher"]
|
||||||
|
|
||||||
|
if "publishedDate" in item["volumeInfo"]:
|
||||||
|
self.publish_date = item["volumeInfo"]["publishedDate"]
|
||||||
|
|
||||||
|
if "authors" in item["volumeInfo"]:
|
||||||
|
self.author = item["volumeInfo"]["authors"][0]
|
||||||
|
|
||||||
|
|
||||||
def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1):
|
def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1):
|
||||||
self.title = title
|
self.title = title
|
||||||
@ -50,9 +80,13 @@ class Book:
|
|||||||
self.author = author
|
self.author = author
|
||||||
self.count = count
|
self.count = count
|
||||||
|
|
||||||
def load(self, loader="openlibrary"):
|
def load(self, config, loader="openlibrary"):
|
||||||
if loader == "openlibrary":
|
if loader == "openlibrary":
|
||||||
return self._openlibrary_load()
|
return self._openlibrary_load(config)
|
||||||
|
elif loader == "google_books":
|
||||||
|
return self._google_books_load(config)
|
||||||
|
else:
|
||||||
|
raise ValueError("Invalid loader")
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.title is None:
|
if self.title is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user