An open API service indexing awesome lists of open source software.

https://github.com/ebrahimakbari/api_price

RESTful API designed to fetch and manage real-time price data
https://github.com/ebrahimakbari/api_price

api celery django-rest-framework docker-compose scraping-websites

Last synced: 2 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# API_PRICE

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python Version](https://img.shields.io/badge/python-3.8%2B-blue.svg)](https://www.python.org/downloads/)

## 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](https://khodro45.com/api/v1/pricing/dailycars/)

[Motorcycles](https://bama.ir/mad/api/price/hierarchy)

[Assets](https://call3.tgju.org/ajax.json)

[Mobiles-List](https://api.digikala.com/v1/categories/mobile-phone/search/?page=1)

[Console-List](https://api.digikala.com/v1/categories/station-gaming-consoles/search/?page={page})

[Gadget-List](https://api.digikala.com/v1/categories/wearable-gadget/search/?page={page})

[HeadPhone-List](https://api.digikala.com/v1/categories/headphone/search/?page={page})

[PC-List](https://api.digikala.com/v1/categories/notebook-netbook-ultrabook/search/?page=20184035)

[Personal-List](https://api.digikala.com/v1/categories/sexual-hygiene-health/search/?page=73186661)

[Digital-devices-Detail](https://api.digikala.com/v2/product/19347047/)

## 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**

```bash
git clone
cd
```

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:

```env
# 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:

```bash
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:

```bash
docker-compose exec app python manage.py migrate
```

5. **Create a Superuser**
To access the Django admin panel, create a superuser:

```bash
docker-compose exec app python manage.py createsuperuser
```

Follow the prompts to set up your admin account.

## Usage

Run the development server:
```bash
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:

```bash
# 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
```bash
GET /api/v1/assets/
```
Example: `GET /api/v1/assets/zinc`
Response:
```json
{
"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
```bash
GET /api/v1/assets/{asset_id}/history/?start_date=2025-01-01&end_date=2025-09-24
```
Response:
```json
[
{
"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](LICENSE) file for details.

## Contact

Ebrahim Akbari - [Email](mailto:y560mia3@gmail.com)
Project Link: [https://github.com/Ebrahimakbari/API_PRICE](https://github.com/Ebrahimakbari/API_PRICE)