Skip to main content

A package for scraping and serving book data in to bookspointer.

Project description

bookspointer

A package for scraping and serving book data.

Installation

pip install bookspointer

Usage

Import and use the modules in your Python code:

from bookspointer.scraper import BookScraper
from bookspointer.server import BookAPI, AuthorAPI, TokenAPI
from bookspointer.sheet import AuthorSheetManager
from bookspointer.api import BookspointerAPI

from rich import print
import random

# Initialize the main classes
scraper = BookScraper()  # Categories to scrape by single page
book_api = BookAPI()
author_api = AuthorAPI()


def update_authors_from_bookspointer():
    """
    Fetches authors from a Google Sheet using AuthorSheetManager and updates them on the server.
    """
    # Fetch and update authors from Google Sheet to your server
    author_sheet = AuthorSheetManager().run()


def update_books_from_authors():
    """
    Fetches unscraped authors from the server, scrapes their books, adds the books to the server, and marks authors as scraped.
    """
    # Fetch unscraped authors and update their books
    authors = author_api.get_unscraped_authors()
    for author in authors:
        author_url = author.get('author_link')
        author_name = author.get('author_name', 'Unknown')
        author_id = author.get('author_id')
        if not author_url:
            continue
        books = scraper.get_book_list(author_url)
        for book in books:
            book_list = scraper.get_book_details(book, author_id)
            for book in book_list:
                add_book = book_api.create(book)
                print(
                    f"Added book: {book['title']} by {author_name} with ID: {add_book}")

            author_api.update(author.get('id'), {"is_scraped": "yes"})

        print(f"Finished scraping books for author: {author_name}")


def update_books_multi_page():
    print("Starting to update books from multi page...")
    try:
        with open('multi_page.txt', 'r') as f:
            total_books = f.readlines()
            print(f"Total books: {len(total_books)}")
    except FileNotFoundError:
        print("Error: 'multi_page.txt' file not found. Please ensure the file exists in the current directory.")
        return
    except Exception as e:
        print(f"Error reading 'multi_page.txt': {e}")
        return

    for book in total_books:
        author_id, link = book.split(',')
        book_info = {
            'link': link.strip(),
            'title': 'Unknown',
            'author': 'Unknown'
        }
        print(book_info)
        book_list = scraper.get_book_details(author_id=int(
            author_id), book_info=book_info, multi_page=True)
        for book in book_list:
            if book['content'] == '':
                print(f'This book may have login required')
                continue
            add_book = book_api.create(book)

    print("Starting to post books on Bookspointer...")
    # post_books_on_bookspointer()
    print("All books posted successfully.")


def post_books_on_bookspointer():
    """
    Posts all books that have not yet been posted to Bookspointer using random tokens for authentication.
    """
    books = book_api.get_all_books(is_posted=False)
    tokens = TokenAPI().get_all_tokens()
    for book in books:
        token = random.choice(tokens)
        bookspointer_api = BookspointerAPI(token)
        try:
            bookspointer_api.post_book(book)
        except Exception as e:
            print(f"Error posting book {book['title']}: {e}")
            continue


def main():
    print("Starting to update authors from Bookspointer...")
    update_authors_from_bookspointer()
    print("Authors updated successfully.")
    print("Starting to update books from authors...")
    update_books_from_authors()
    print("Books updated successfully.")
    print("Starting to post books on Bookspointer...")
    # post_books_on_bookspointer()
    print("All books posted successfully.")


if __name__ == "__main__":
    multi_page = input('Do you want to update books from multi page? (y/n)')
    if multi_page == 'y':
        multi_page = True
    else:
        multi_page = False
    if multi_page:
        update_books_multi_page()
    else:
        main()

Features

  • Scrape book data
  • Serve book data via API
  • Sync authors and books with Google Sheets

Project Links

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bookspointer-0.2.4.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bookspointer-0.2.4-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file bookspointer-0.2.4.tar.gz.

File metadata

  • Download URL: bookspointer-0.2.4.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for bookspointer-0.2.4.tar.gz
Algorithm Hash digest
SHA256 cfcfad5562b813404b9956dc4c2f76cdeefa60b0175b1008e4a27fc537d24667
MD5 f0a661e8a6e1751a48db3c1492bbb3f2
BLAKE2b-256 ea06a4f7c2fc7cac0023ca9dd46d82c29f6de93fffeacb620fd2c36c71d248ea

See more details on using hashes here.

File details

Details for the file bookspointer-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: bookspointer-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for bookspointer-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ceb086525a77f79b98f698d739eba9b10fadfe9df27c02f6070f0871af13564c
MD5 7ff92fefe26057088b5525c4a4cd6cf5
BLAKE2b-256 40eb9196fd2e27a6654b7df93a4f657dbdfcad21a1a5336c7d7e3ff4a40ea2f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page