This project provides async scraping, robust storage, RESTful APIs, and AI-driven analytics for e-commerce products (books example).
- Async Crawler: Collects and normalizes product data (name, price, rating, description, category, upc, availability, stock_count), and persists raw HTML for reprocessing.
- SQLAlchemy ORM & Alembic Migrations: Persistent relational schema with Postgres pgvector-ready setup, working with Docker for local and CI.
- REST API: FastAPI-based, Pydantic-validated endpoints with:
- Paginated listing with filtering/search
- Product detail view
- Recommendations endpoint (with embeddings)
- Trends analytics
- AI Module:
- Embedding generation using
sentence-transformers - LLM-based summaries for products
- Both AI features have explicit code for bulk generation and updating
- Embedding generation using
- Testing: Project contains pytest support and configuration
- Deployment: Containerized via Docker & Compose; Postgres+FastAPI bundled
- CI: Ready for simple workflow in .github/workflows
app/ingestion/: Crawling & data normalization logic (async, aiohttp/bs4)app/models/: SQLAlchemy models (product.pyshowsBookandBookAIDetailswith vector+summary)app/ai/:embeddings.py: Embedding generation and storagellm_summariser.py: For LLM-driven summaryrecommender.py: For similarity-based recommendations
app/api/: FastAPI endpoints and routersalembic/: Migrations- Docker, Compose, Pytest, Ruff, MyPy, and Coverage configs
- Python 3.13+
- Docker & Docker Compose
- PostgreSQL server
git clone https://github.com/agrawal-vatsal/bookshop.git
cd bookshop
cp .env.example .env
# Following lines for local development only
python3 -m venv .venv
source .venv/bin/activate # Activate virtual environment
pip install uv
uv syncAdd the OpenAI API keys to the .env file. The key is required for generating summaries.
All app and DB services will spin up:
docker compose up --build
# FastAPI at http://localhost:8000
# PostgreSQL at localhost:5432 (user: bookshopuser, pass: bookshoppassword, db: bookshopdb)docker-compose exec web bash
Run all the following commands inside the web shell.
Migrations are in alembic/ directory:
To migrate the database schema to the latest version, run:
alembic upgrade headThis will create all needed tables: books and book_ai_details (with embedding vector columns).
To collect book data from websites:
python -m app.ingestion.crawlerThis will save raw HTML and produce normalized records (books) to seed the database.
All the files will be saved in app/data/raw_html directory, including raw HTML files.
After crawling and normalizing the data:
python -m app.ingestion.seedThis will insert the normalized product data into the database.
To generate and store embeddings for all books that don't have them:
python -m app.ai.embeddingsThis uses generate_and_store_embeddings() function to create vector embeddings for each product using sentence-transformers, and stores them in the book_ai_details.embedding column.
To create summaries for all books:
python -m app.ai.llm_summariserThis will query an LLM for each product that lacks a summary and update the database with concise descriptions.
Returns a paginated list of books with filtering options.
Query Parameters:
skip: Number of records to skip (default: 0)limit: Maximum number of records to return (default: 20, max: 100)min_price: Filter by minimum price (optional)max_price: Filter by maximum price (optional)min_rating: Filter by minimum rating (1-5, optional)category: Filter by book category (optional)q: Search query string (optional)
Response:
{
"books": [
{
"id": int,
"name": str,
"price": float,
"rating": int,
"category": str,
"upc": str,
"availability": str,
"stock_count": int,
}
],
"total": int
}Returns detailed information about a specific book.
Path Parameters:
book_id: The ID of the book to retrieve
Response:
{
"id": int,
"name": str,
"price": float,
"rating": int,
"category": str,
"upc": str,
"availability": str,
"stock_count": int,
"description": str,
"summary": str | null, // AI-generated summary if available
}Error Responses:
404 Not Found: Book with the specified ID doesn't exist
Returns pricing, rating, or category analysis of the book catalog.
Response: Trend analysis data including pricing patterns and rating distributions.
Returns books similar to the specified book using vector embeddings.
Path Parameters:
book_id: The ID of the book to find recommendations for
Response: A list of recommended books sorted by similarity score.
Error Responses:
404 Not Found: Book with the specified ID doesn't exist
All endpoints are type-safe and validated with Pydantic models.
pytestThe project is configured with coverage.py to track test coverage. To run tests with coverage and generate a report:
# Run tests with coverage
coverage run -m pytest
# Generate a coverage report
coverage report
# Generate an HTML coverage report
coverage htmlThe HTML report will be available in the htmlcov directory and provides a detailed breakdown of coverage by file and line.
Coverage settings are configured in pyproject.toml with:
- Branch coverage enabled
- Minimum coverage threshold of 80%
- Missing line reporting
- Lint:
ruff . - Type check:
mypy .
.env.example is provided; set `OpenAI API keys.
For someone testing this application, follow these steps in order:
- Start the environment:
docker compose up --build - Apply migrations:
alembic upgrade head - Crawl data:
python -m app.ingestion.crawler - Seed database:
python -m app.ingestion.seed - Generate embeddings:
python -m app.ai.embeddings - Generate summaries:
python -m app.ai.llm_summariser
- Docker issues?
Ensure Docker is running and you have sufficient resources allocated (CPU, memory). - LLM API errors?
Set proper keys/URL in.env. - Database connection issues?
Stop the local Postgres server if running, and ensure Docker is configured correctly. This is needed as both point to the port 5432.
For questions, issues, or support: raise a GitHub issue or contact the maintainer.