isbn-sort/isbn_sort/book.py

105 lines
3.3 KiB
Python
Raw Normal View History

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
self.publisher = None
self.publish_date = None
self.author = None
2024-04-03 16:54:54 +02:00
self.count = -1
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"]
if "publishers" in isbn_data["details"] and len(isbn_data["details"]["publishers"]) > 0:
self.publisher = isbn_data["details"]["publishers"][0]
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"]
if "publish_date" in isbn_data["details"]:
self.publish_date = isbn_data["details"]["publish_date"]
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 "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]
2024-04-03 16:28:25 +02:00
2024-04-03 16:54:54 +02:00
def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1):
2024-04-03 16:28:25 +02:00
self.title = title
self.publisher = publisher
self.publish_date = publish_date
self.author = author
2024-04-03 16:54:54 +02:00
self.count = count
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,
"publisher": self.publisher,
"publish_date": self.publish_date,
"author": self.author,
"count": self.count
}