Skip to content

RESTful API designed to fetch and manage real-time price data

License

Notifications You must be signed in to change notification settings

Ebrahimakbari/API_PRICE

Repository files navigation

API_PRICE

License: MIT Python Version

Description

A public API to track the daily prices of cars, motorcycles, electronic-devices(like: mobiles, laptops, gadgets, ...) and financial assets like currencies and precious metals. This project periodically scrapes data from external sources, stores it, and exposes it through a clean, documented RESTful API.

The entire application is containerized with Docker for easy setup, development, and deployment.

Target Scraping APIs

Cars

Motorcycles

Assets

Mobiles-List

Console-List

Gadget-List

HeadPhone-List

PC-List

Personal-List

Digital-devices-Detail

Features

  • RESTful API: Provides endpoints for cars, motorcycles, digital-devices(like: mobiles, laptops, gadgets, ...) and financial assets.
  • User Authentication: Secure JWT-based authentication for user registration and login.
  • Automated Data Scraping: Celery workers and Celery Beat periodically fetch the latest price data from external sources.
  • Containerized Environment: Fully containerized with Docker and Docker Compose for consistent environments and easy deployment.
  • Production-Ready: Includes Nginx as a reverse proxy and Gunicorn as the WSGI application server.
  • API Documentation: Automatic, interactive API documentation available via Swagger UI and Redoc, generated by drf-spectacular.
  • [cite_start]Custom User Model: A flexible custom user model for managing application users[cite: 1].
  • Rate Limiting: API throttling is in place to prevent abuse from anonymous and authenticated users.

Technology Stack

  • Backend: Django, Django REST Framework
  • Database: PostgreSQL
  • Asynchronous Tasks: Celery, Redis (as message broker)
  • Containerization: Docker, Docker Compose
  • Web Server / Gateway: Nginx, Gunicorn
  • API Documentation: drf-spectacular

API Endpoints

The base URL for the API is /api/v1/.

Endpoint Method Description
/accounts/register/ POST Register a new user.
/accounts/token/ POST Obtain JWT access and refresh tokens.
/accounts/token/refresh/ POST Refresh an access token.
/api/v1/car-brands/ GET List all car brands.
/api/v1/cars/ GET List all cars with their price history.
/api/v1/motorcycle-brands/ GET List all motorcycle brands.
/api/v1/motorcycles/ GET List all motorcycles with their price history.
/api/v1/assets/ GET List all currencies.
/api/v1/assets/?category=precious_metals GET List all precious metals.
... ... ...

API Documentation:

  • Swagger UI: http://localhost/api/docs/
  • Redoc: http://localhost/api/redoc/

Setup and Installation

Prerequisites

  • Docker
  • Docker Compose

Steps

  1. Clone the Repository

    git clone <your-repository-url>
    cd <repository-directory>
  2. Create Environment File Create a file named .env in the project root and populate it with the necessary environment variables. Use the following template:

    # Django Core
    SECRET_KEY=your-super-secret-key-here
    
    # PostgreSQL Database
    POSTGRES_DB=price_api_db
    POSTGRES_USER=user
    POSTGRES_PASSWORD=password
    POSTGRES_HOST=db
    POSTGRES_PORT=5432
    
    # Celery (using Redis)
    CELERY_BROKER_URL=redis://redis:6379/0
    CELERY_RESULT_BACKEND=redis://redis:6379/0
    
    # Bama Scraper API URL
    BASE_API_URL_CAR=
    BASE_API_URL_MOTORCYCLE=
    BASE_API_URL_ASSETS=
    LIST_API_URL_MOBILE=
    LIST_API_URL_PC=
    DETAIL_API_URL_MOBILE=
    DETAIL_API_URL_PC=
    
    # Email (Optional, for future features)
    EMAIL_HOST_USER=your-email@gmail.com
    EMAIL_HOST_PASSWORD=your-gmail-app-password
    DEFAULT_FROM_EMAIL=your-email@gmail.com
  3. Build and Run with Docker Compose From the project root, run the following command to build the images and start the containers:

    docker-compose up --build -d

    This will start the nginx, app, db, redis, celery_worker, and celery_beat services.

  4. Apply Database Migrations Once the containers are running, apply the Django database migrations:

    docker-compose exec app python manage.py migrate
  5. Create a Superuser To access the Django admin panel, create a superuser:

    docker-compose exec app python manage.py createsuperuser

    Follow the prompts to set up your admin account.

Usage

Run the development server:

uvicorn main:app --reload
  • API: The API will be accessible at http://localhost/.
  • Django Admin: Access the admin panel at http://localhost/admin/.
  • Flower (Celery Monitor): Monitor Celery tasks at http://localhost:5555/.

The API will be available at http://localhost:8000. Access the interactive API documentation at http://localhost:8000/docs.

The scrapers are scheduled to run automatically via Celery Beat. However, you can trigger them manually for testing:

# Scrape car prices
docker-compose exec celery_worker celery -A core call cars.tasks.scrape_car_prices

# Scrape motorcycle prices
docker-compose exec celery_worker celery -A core call motorcycles.tasks.scrape_motorcycle_prices

# Scrape asset prices
docker-compose exec celery_worker celery -A core call assets.tasks.scrape_assets_prices

Example Requests

Get Current Price

GET /api/v1/assets/

Example: GET /api/v1/assets/zinc
Response:

    {
        "symbol": "zinc",
        "name_fa": "روی",
        "name_en": "Zinc",
        "category": "METAL",
        "latest_price": {
            "price": "2575.6000",
            "high": "2575.6000",
            "low": "2575.6000",
            "timestamp": "2021-06-28T15:00:00+04:30"
        }

Get Historical Prices

GET /api/v1/assets/{asset_id}/history/?start_date=2025-01-01&end_date=2025-09-24

Response:

[
    {
        "price": "2575.6000",
        "high": "2575.6000",
        "low": "2575.6000",
        "timestamp": "2021-06-28T15:00:00+04:30"
    }
]

Configuration

All sensitive data and environment-specific settings are managed through environment variables loaded from the .env file. Key variables include SECRET_KEY, database credentials (POSTGRES_*), and Celery broker URLs (CELERY_*).

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the project.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

Ebrahim Akbari - Email
Project Link: https://github.com/Ebrahimakbari/API_PRICE

About

RESTful API designed to fetch and manage real-time price data

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published