Skip to content

Abhinav-143x/Price-Tracker

Repository files navigation

Real-Time Price Tracker

Python Django Celery Channels Redis Docker

Track product prices on any website. Get live WebSocket alerts the moment a price drops. Built with Django + Celery for background scraping and Django Channels for real-time push.


Architecture

Browser ──REST──► Django API (DRF + Channels)
   ▲                    │ enqueue
   │ WebSocket push      ▼
   │              Celery Worker ──HTTP──► External URL (Amazon, Flipkart…)
   │                    │ save
   │                    ▼
Redis ◄──────── PostgreSQL (trackers + price_history)
(broker +
 channel layer)

Data flow:

  1. User creates a tracker via POST /api/v1/trackers/
  2. An immediate scrape task is enqueued → price saved to DB
  3. Celery Beat fires scrape_all_active_trackers every N seconds
  4. Each worker fetches the URL, parses the price, compares to last known
  5. If the price changed → saves a PriceHistory row + pushes a WebSocket message
  6. Any browser connected to ws://.../ws/trackers/<id>/ receives the update live

Quick Start

git clone https://github.com/YOUR_USERNAME/price-tracker.git
cd price-tracker
cp .env.example .env

docker compose up --build

In a second terminal:

docker compose exec api python manage.py migrate
docker compose exec api python manage.py createsuperuser

Open http://localhost:8000/api/docs/ — Swagger UI is live.

Open templates/trackers/demo.html in your browser to test WebSocket in real time.


Services

Service What it does
api Django + Channels via Uvicorn (HTTP + WebSocket)
worker Celery worker — runs scrape tasks
beat Celery Beat — schedules periodic scrapes
db PostgreSQL 15
redis Redis 7 — task broker + WebSocket channel layer

API Endpoints

Method Path Description
POST /api/v1/auth/token/ Get JWT tokens
POST /api/v1/auth/token/refresh/ Refresh access token
GET/POST /api/v1/trackers/ List or create trackers
GET/PATCH/DELETE /api/v1/trackers/<id>/ Get, update or delete
GET /api/v1/trackers/<id>/history/ Full price history
POST /api/v1/trackers/<id>/trigger/ Manually trigger a scrape
POST /api/v1/trackers/<id>/pause/ Pause / resume a tracker

WebSocket

ws://localhost:8000/ws/trackers/<tracker-uuid>/

Connect with a valid session (the AuthMiddlewareStack checks Django session auth). The server pushes JSON on every price check:

{
  "type": "price_update",
  "tracker_id": "...",
  "tracker_name": "RTX 4090",
  "event": "price_update",
  "new_price": "79999.00",
  "old_price": "89999.00",
  "currency": "INR",
  "drop_pct": 11.11,
  "should_alert": true,
  "raw_text": "₹79,999"
}

Creating a Tracker

# 1. Get a token
curl -X POST http://localhost:8000/api/v1/auth/token/ \
  -H "Content-Type: application/json" \
  -d '{"username": "you@example.com", "password": "yourpass"}'

# 2. Create a tracker
curl -X POST http://localhost:8000/api/v1/trackers/ \
  -H "Authorization: Bearer <access_token>" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://www.amazon.in/dp/B09V3KXJPB",
    "name": "RTX 4090",
    "alert_threshold_pct": 5,
    "check_interval": 300
  }'

# 3. Trigger a manual scrape
curl -X POST http://localhost:8000/api/v1/trackers/<id>/trigger/ \
  -H "Authorization: Bearer <access_token>"

Price Selector

Leave price_selector blank for auto-detection (works on Amazon India and Flipkart). For other sites, inspect the page and pass a CSS selector:

{ "price_selector": "span.a-price-whole" }

Tech Stack

Layer Technology
Framework Django 4.2 + DRF 3.15
WebSockets Django Channels 4.1 + channels-redis
Background jobs Celery 5.3 + Celery Beat
Message broker Redis 7
Database PostgreSQL 15
Scraping requests + BeautifulSoup4 + lxml
Container Docker + Docker Compose

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors