A Python library for scraping book information from databazeknih.cz
Project description
py-db-knih
A Python library for scraping book information from databazeknih.cz, the Czech book database.
This is a Python port of the original TypeScript implementation, designed with unit-testable code and clean architecture.
Features
- Search Books: Search for books by title, author, or any text
- Detailed Book Information: Get comprehensive book details including:
- Plot summary
- Genres
- Publication year and publisher
- User ratings and reviews
- Cover image
- Page count, ISBN, original language
- Unit Testable: All parsing logic is thoroughly tested
- Clean Architecture: Modular design with separate services for different functionalities
Installation
Install from PyPI:
pip install py-db-knih
That's it! The package and all its dependencies will be installed automatically.
Usage
Basic Usage
from db_knih_api import db_knih
# Search for books
search_results = db_knih.search("harry potter")
print(f"Found {len(search_results)} books")
for book in search_results[:3]: # Show first 3 results
print(f"- {book.name} ({book.year}) by {book.author}")
print(f" ID: {book.id}, Clean name: {book.cleanName}")
# Get detailed information for a book
if search_results:
first_book = search_results[0]
book_link = f"{first_book.cleanName}-{first_book.id}"
detailed_info = db_knih.get_book_info(book_link)
if detailed_info:
print(f"Plot: {detailed_info.plot}")
print(f"Genres: {', '.join(detailed_info.genres) if detailed_info.genres else 'N/A'}")
print(f"Rating: {detailed_info.rating}%")
print(f"Pages: {detailed_info.pages}")
Advanced Usage
from db_knih_api import BookService, SearchService, Fetcher
# Use services independently
search_service = SearchService()
book_service = BookService()
# Or with custom fetcher (useful for testing)
custom_fetcher = Fetcher()
search_service = SearchService(custom_fetcher)
book_service = BookService(custom_fetcher)
Data Models
SearchInfo
Basic book information from search results:
name: Book titlecleanName: URL-friendly book nameid: Unique book IDyear: Publication yearauthor: Author name
BookInfo
Detailed book information:
plot: Book summary/plotgenres: List of genresyear: Publication yearpublisher: Publisher namerating: Average user rating (percentage)numberOfRatings: Number of user ratingsreviews: List of user reviews (up to 5)cover: Cover image URLpages: Number of pagesoriginalLanguage: Original languageisbn: ISBN number
Review
User review information:
text: Review textrating: User's rating (1-5 stars)username: Reviewer's usernamedate: Review date
Testing
If you want to run the tests (for development), you can clone the repository and run:
# Clone the repository
git clone https://github.com/ROGR3/py-db-knih.git
cd py-db-knih
# Install in development mode
pip install -e .
# Run all tests
python -m pytest tests/ -v
# Run specific test file
python -m pytest tests/test_book_service.py -v
Test Coverage
The tests cover:
- Fetcher: HTTP requests, URL generation, error handling
- BookService: HTML parsing, data extraction, regex patterns
- SearchService: Search result parsing, book route extraction
- Main API: Integration between services
Architecture
The code is organized into several modules:
models.py: Data classes for type safetyfetcher.py: HTTP client with proper headers and error handlingbook_service.py: Detailed book information extractionsearch_service.py: Book search functionality__init__.py: Main API class that combines services
Error Handling
The library handles various error conditions gracefully:
- Network errors return 'Error' string
- Missing HTML elements return
Noneor empty lists - Invalid data is safely converted (e.g., non-numeric strings to 0)
- All methods are designed to not raise exceptions
Dependencies
requests: HTTP clientbeautifulsoup4: HTML parsinglxml: Fast XML/HTML parserpytest: Testing frameworkpytest-mock: Mocking utilities for tests
Example Output
🔍 DB Knih API - Quick Start
========================================
📚 Searching for 'harry potter'...
✅ Found 30 books!
1. Harry Potter
📅 Year: 2025
✍️ Author: J. K. Rowling
🆔 ID: 572730
2. Harry Potter a Fénixův řád
📅 Year: 2004
✍️ Author: J. K. Rowling
🆔 ID: 13
📖 Getting detailed info for 'Harry Potter'...
✅ Detailed information:
🏷️ Genres: Literatura naučná, Obrazové publikace, Pro děti a mládež
⭐ Rating: 68.0%
👥 Ratings: 10.0
📄 Pages: 32.0
🌍 Language: český
📚 ISBN: 9788000077703
License
MIT License - see the original TypeScript project for details.
Contributing
- Fork the repository: https://github.com/ROGR3/py-db-knih
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Notes
- This library is for educational and personal use only
- Please respect the website's terms of service and robots.txt
- Consider adding delays between requests to be respectful to the server
- The website structure may change, which could break the parsing logic
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_db_knih-1.0.3.tar.gz.
File metadata
- Download URL: py_db_knih-1.0.3.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9e4d9f9637aba7010a64a39ffe5401387976c2be869ed5b73221418e7cdbf99
|
|
| MD5 |
a5e3e7822ac652ef970888627f471571
|
|
| BLAKE2b-256 |
542f427a2a30d26651288fa4c24814027249358747b56e7766420d3ebaacf225
|
File details
Details for the file py_db_knih-1.0.3-py3-none-any.whl.
File metadata
- Download URL: py_db_knih-1.0.3-py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2422a4e86314515ffae3f228ba4a95c3902a98daf547a4caa0d94342007a0d5b
|
|
| MD5 |
50f79930f6fb3284ae35288ec0442f95
|
|
| BLAKE2b-256 |
e02793f3c4e32016bc3b8af6e6ed0bd90b29d4f69ed75df4a5c0bd9331577596
|