2024-04-03 16:28:25 +02:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
class Book:
|
|
|
|
def __init__(self, isbn):
|
|
|
|
if isbn is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2024-04-03 17:54:20 +02:00
|
|
|
isbn = str(isbn).replace("-", "")
|
2024-04-03 16:28:25 +02:00
|
|
|
int(isbn)
|
2024-04-19 11:30:55 +02:00
|
|
|
except ValueError: # this can be ISBN-10
|
|
|
|
pass
|
|
|
|
# raise ValueError("ISBN must be an int")
|
2024-04-03 16:28:25 +02:00
|
|
|
|
|
|
|
self.isbn = isbn
|
|
|
|
self.title = None
|
2024-11-01 21:55:45 +01:00
|
|
|
self.owner = None
|
|
|
|
self.status = 0
|
2024-04-03 16:28:25 +02:00
|
|
|
self.author = None
|
2024-05-01 19:57:01 +02:00
|
|
|
self.category = None
|
2024-11-01 21:55:45 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status_text(self) -> str:
|
|
|
|
return [
|
|
|
|
"À lire",
|
|
|
|
"En cours",
|
|
|
|
"Lu"
|
|
|
|
][self.status]
|
|
|
|
|
2024-04-03 16:28:25 +02:00
|
|
|
|
2024-04-08 12:18:49 +02:00
|
|
|
def _openlibrary_load(self, _):
|
2024-04-03 16:28:25 +02:00
|
|
|
r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json")
|
|
|
|
|
|
|
|
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 f"ISBN:{self.isbn}" not in data:
|
|
|
|
raise KeyError("Not found in OpenLibrary")
|
|
|
|
|
|
|
|
isbn_data = data[f"ISBN:{self.isbn}"]
|
|
|
|
|
|
|
|
self.title = isbn_data["details"]["title"]
|
2024-04-03 16:54:54 +02:00
|
|
|
|
2024-04-03 16:28:25 +02:00
|
|
|
if "authors" in isbn_data["details"] and len(isbn_data["details"]["authors"]) > 0:
|
|
|
|
self.author = isbn_data["details"]["authors"][0]["name"]
|
|
|
|
|
2024-04-08 12:18:49 +02:00
|
|
|
|
|
|
|
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 "authors" in item["volumeInfo"]:
|
|
|
|
self.author = item["volumeInfo"]["authors"][0]
|
|
|
|
|
2024-04-03 16:28:25 +02:00
|
|
|
|
2024-11-01 21:55:45 +01:00
|
|
|
def _manual_load(self, title, author=None, category=None, status=0, owner=None):
|
2024-04-03 16:28:25 +02:00
|
|
|
self.title = title
|
|
|
|
self.author = author
|
2024-05-01 19:57:01 +02:00
|
|
|
self.category = category
|
2024-11-01 21:55:45 +01:00
|
|
|
self.status = status
|
|
|
|
self.owner = owner
|
2024-04-03 16:28:25 +02:00
|
|
|
|
2024-04-08 12:18:49 +02:00
|
|
|
def load(self, config, loader="openlibrary"):
|
2024-04-03 16:28:25 +02:00
|
|
|
if loader == "openlibrary":
|
2024-04-08 12:18:49 +02:00
|
|
|
return self._openlibrary_load(config)
|
|
|
|
elif loader == "google_books":
|
|
|
|
return self._google_books_load(config)
|
|
|
|
else:
|
|
|
|
raise ValueError("Invalid loader")
|
2024-04-03 16:28:25 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.title is None:
|
|
|
|
return f"<Book: ISBN:{self.isbn}>"
|
2024-04-05 17:47:15 +02:00
|
|
|
return f"<Book: {self.title}>"
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
"isbn": self.isbn,
|
|
|
|
"title": self.title,
|
2024-11-01 21:55:45 +01:00
|
|
|
"owner": self.owner,
|
|
|
|
"status": self.status,
|
|
|
|
"status_text": self.status_text,
|
2024-04-05 17:47:15 +02:00
|
|
|
"author": self.author,
|
2024-11-01 21:55:45 +01:00
|
|
|
"category": self.category
|
2024-10-08 14:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def replace(self, pattern, replacement):
|
|
|
|
"""Replace element in all fields"""
|
|
|
|
def rep(el):
|
|
|
|
if isinstance(el, str):
|
|
|
|
return el.replace(pattern, replacement)
|
|
|
|
return el
|
|
|
|
|
|
|
|
self.isbn = rep(self.isbn)
|
|
|
|
self.title = rep(self.title)
|
2024-11-01 21:55:45 +01:00
|
|
|
self.owner = rep(self.owner)
|
|
|
|
self.status = rep(self.status)
|
2024-10-08 14:37:12 +02:00
|
|
|
self.author = rep(self.author)
|
2024-11-01 21:55:45 +01:00
|
|
|
self.category = rep(self.category)
|