Managing my books library with Python

Automating my books library and to-read list

11 Aug 2022

Looking for an API

Google Books

No Python library identified.
Might need to come back to it for low-level integration.

17 Feb 2023 seems like this can retrieve book cover & information from ISBN number with a simple request (no authentication required):

https://www.googleapis.com/books/v1/volumes?q=isbn:140022652X

{
  "kind": "books#volumes",
  "totalItems": 1,
  "items": [
    {
      "kind": "books#volume",
      "id": "HouRzQEACAAJ",
      "etag": "JyDWqsS3jCQ",
      "selfLink": "https://www.googleapis.com/books/v1/volumes/HouRzQEACAAJ",
      "volumeInfo": {
        "title": "Technology Quotient",
        "authors": [
          "Tony Hughes",
          "Justin Michael"
        ],
        "publishedDate": "2021-06-22",
        "description": "Follows the narrative arc of a multifaceted approach to understanding any/all automation systems that are popular today and will be valid toward 2030.",
        "industryIdentifiers": [
          {
            "type": "ISBN_10",
            "identifier": "140022652X"
          },
          {
            "type": "ISBN_13",
            "identifier": "9781400226528"
          }
        ],
        "readingModes": {
          "text": false,
          "image": false
        },
        "pageCount": 256,
        "printType": "BOOK",
        "categories": [
          "Business & Economics"
        ],
        "maturityRating": "NOT_MATURE",
        "allowAnonLogging": false,
        "contentVersion": "preview-1.0.0",
        "panelizationSummary": {
          "containsEpubBubbles": false,
          "containsImageBubbles": false
        },
        "imageLinks": {
          "smallThumbnail": "http://books.google.com/books/content?id=HouRzQEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
          "thumbnail": "http://books.google.com/books/content?id=HouRzQEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
        },
        "language": "en",
        "previewLink": "http://books.google.de/books?id=HouRzQEACAAJ&dq=isbn:140022652X&hl=&cd=1&source=gbs_api",
        "infoLink": "http://books.google.de/books?id=HouRzQEACAAJ&dq=isbn:140022652X&hl=&source=gbs_api",
        "canonicalVolumeLink": "https://books.google.com/books/about/Technology_Quotient.html?hl=&id=HouRzQEACAAJ"
      },
      "saleInfo": {
        "country": "DE",
        "saleability": "NOT_FOR_SALE",
        "isEbook": false
      },
      "accessInfo": {
        "country": "DE",
        "viewability": "NO_PAGES",
        "embeddable": false,
        "publicDomain": false,
        "textToSpeechPermission": "ALLOWED",
        "epub": {
          "isAvailable": false
        },
        "pdf": {
          "isAvailable": false
        },
        "webReaderLink": "http://play.google.com/books/reader?id=HouRzQEACAAJ&hl=&source=gbs_api",
        "accessViewStatus": "NONE",
        "quoteSharingAllowed": false
      },
      "searchInfo": {
        "textSnippet": "Become Superhuman In How You Work With Technology: Learn how to upskill your technology quotient (TQ) by understanding how the most powerful sales technologies operate and can be configured for maximum benefit."
      }
    }
  ]
}

working code to download book covers

10 Apr 2023

def download_cover(title):
    global GOOGLE_API_KEY
    global cover_folder

    base_url = "https://www.googleapis.com/books/v1/volumes"
    params = {
        'q': f'intitle:"{title}"',
        'key': GOOGLE_API_KEY,
        'maxResults': 1
    }

    response = requests.get(base_url, params=params)
    data = json.loads(response.text)

    if 'items' in data:
        book_data = data['items'][0]
        if 'imageLinks' in book_data['volumeInfo']:
            cover_url = book_data['volumeInfo']['imageLinks'].get('thumbnail')
            if cover_url:
                cover_response = requests.get(cover_url, stream=True)
                cover_file_name = f"{cover_folder}/{title}.jpg"
                with open(cover_file_name, 'wb') as f:
                    for chunk in cover_response.iter_content(chunk_size=8192):
                        f.write(chunk)
                print(f"Cover image downloaded for '{title}' as {cover_file_name}")
                return True
            else:
                print(f"No cover image found for '{title}'.")
                return False
        else:
            print(f"No cover image found for '{title}'.")
            return False
    else:
        print(f"No book found with the title '{title}'.")
        return False

working code to download book data

12 Apr 2023

# CLASSES

class Book:
    def __init__(self, pages, release_year, author, tagline, description, google_link, category, ISBN_10, ISBN_13, language, title):
        self.pages = pages
        self.release_year = release_year
        self.author = author
        self.tagline = tagline
        self.description = description
        self.google_link = google_link
        self.category = category
        self.ISBN_10 = ISBN_10
        self.ISBN_13 = ISBN_13
        self.language = language
        self.title = title

    def __str__(self):
        return f'Title: {self.title}\nAuthor: {self.author}\nPages: {self.pages}\nRelease Year: {self.release_year}\nTagline: {self.tagline}\nDescription: {self.description}\nGoogle Link: {self.google_link}\nCategory: {self.category}\nISBN-10: {self.ISBN_10}\nISBN-13: {self.ISBN_13}\nLanguage: {self.language}'

# FUNCTIONS

def get_book_data(title, author):
    global GOOGLE_API_KEY

    base_url = "https://www.googleapis.com/books/v1/volumes"
    params = {
        'q': f'intitle:"{title}" inauthor:"{author}"',
        'key': GOOGLE_API_KEY,
        'maxResults': 20
    }

    response = requests.get(base_url, params=params)
    data = response.json()

    if "items" not in data:
        return None

    # Sort items by published date
    items = sorted(data["items"], key=lambda item: item["volumeInfo"].get("publishedDate", "9999"))

    for item in items:
        volume_info = item["volumeInfo"]

        # Retrieve ISBN-10 and ISBN-13
        ISBN_10, ISBN_13 = None, None
        for identifier in volume_info.get("industryIdentifiers", []):
            if identifier["type"] == "ISBN_10":
                ISBN_10 = identifier["identifier"]
            elif identifier["type"] == "ISBN_13":
                ISBN_13 = identifier["identifier"]

        # If ISBN-10 is not found, continue with the next book in the list
        if ISBN_10 is None:
            continue

        # Retrieve other book information
        pages = volume_info.get("pageCount", None)
        release_year = volume_info.get("publishedDate", None)
        if '-' in release_year:
            release_year = release_year.split('-')[0]
        author = ", ".join(volume_info.get("authors", []))
        tagline = volume_info.get("subtitle", None)
        description = volume_info.get("description", None)
        google_link = volume_info.get("canonicalVolumeLink", None)
        category = ", ".join(volume_info.get("categories", []))
        language = volume_info.get("language", None)
        title = volume_info.get("title", None)

        # Create and return a Book object with an ISBN-10 number
        return Book(pages, release_year, author, tagline, description, google_link, category, ISBN_10, ISBN_13, language, title)

    # If no book with an ISBN-10 number is found, return None
    return None

Output ⬇︎

Title: Foundation
Author: Isaac Asimov
Pages: 227
Release Year: 1951
Tagline: None
Description: The first of a series of tales set so far in the future that Earth is all but forgotten by humans who live throughout the galaxy.
Google Link: https://books.google.com/books/about/Foundation.html?hl=&id=gFpQswEACAAJ
Category: Life on other planets
ISBN-10: 038505047X
ISBN-13: 9780385050470
Language: en

GoodReads

API discontinued in Dec '20: https://www.goodreads.com/api/documentation

OpenLibrary

27 Feb 2023

ISBN

isbntools

isbntools provides several useful methods and functions to validate, clean, transform, hyphenate and get metadata for ISBN strings.

https://github.com/xlcnd/isbntools

isbnplus

https://isbnplus.com/api/

ISBNdb

https://isbndb.com/

Paid-for: $10/month minimum.

ISBNfind.org

https://isbnfind.org/

Barely any result.

Worldcat

https://www.worldcat.org/wcpa/content/affiliate/default.jsp

Amazon

https://developer-docs.amazon.com/sp-api/docs/catalog-items-api-v2022-04-01-reference

links

social