Skip to content

Repository files navigation

Weather Fetcher API

Python FastAPI OpenWeatherMap CLI REST API Web UI Data Visualization Security Tests Code Quality License Contributions Welcome

A comprehensive Python weather data fetcher CLI tool with REST API and web dashboard. Retrieve current weather, historical weather data, and weather forecasts from OpenWeatherMap API. Features multi-mode CLI, RESTful API endpoints, data visualization, encrypted API key management, and responsive web UI for seamless weather data integration.

Features

  • Multiple Weather Modes:

    • Current Weather Data
    • Historical Weather Data
    • Forecasted Weather Data
  • Flexible Operation Modes:

    • CLI Interface with interactive and batch modes
    • REST API with FastAPI
    • Modern Web UI
  • Runtime Mode Switching: Switch between different modes using parameters at runtime

  • Secure API Key Management: Encrypted storage of API keys with no exposed secrets

  • Data Visualization: Built-in matplotlib/seaborn visualization for weather data

  • Configurable: YAML configuration and environment variable support

  • Modular Architecture: Clean separation of concerns with reusable components

Installation

Prerequisites

Setup

  1. Clone the repository:
git clone <repository-url>
cd new_implementation
  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure API key:
cp .env.example .env
# Edit .env and add your OpenWeatherMap API key

Configuration

Environment Variables

Create a .env file in the project root with the following variables:

OPENWEATHER_API_KEY=your_api_key_here
LATITUDE=20.2910
LONGITUDE=85.8456
DEFAULT_UNITS=metric
DEFAULT_LANGUAGE=en
LOG_LEVEL=INFO

Configuration File

You can also use config/config.yaml for more complex configurations:

api:
  base_url: "https://api.openweathermap.org/data/2.5"
  current_weather_url: "/weather"
  forecast_url: "/onecall"
  historical_url: "/onecall/timemachine"

location:
  latitude: 20.2910
  longitude: 85.8456
  timezone: "Asia/Kolkata"

units:
  standard:
    temperature: "K"
    speed: "m/s"
  metric:
    temperature: "°C"
    speed: "m/s"
  imperial:
    temperature: "°F"
    speed: "mph"

languages:
  english: "en"
  hindi: "hi"

exclusion_options:
  - "current"
  - "minutely"
  - "hourly"
  - "daily"
  - "alerts"

Usage

CLI Mode

Interactive Mode

python -m src.main --mode interactive

Batch Mode - Current Weather

python -m src.main --mode current --location "New York" --units metric

Batch Mode - Historical Weather

python -m src.main --mode historical --days-back 1 --units metric --visualize

Batch Mode - Forecasted Weather

python -m src.main --mode forecast --days 5 --exclude minutely,hourly --units metric --visualize

REST API Mode

Start the API server:

python -m src.api.main

The API will be available at http://localhost:8000

API Endpoints

  • GET /api/weather/current - Get current weather

    • Query params: q (location), units, lang
  • GET /api/weather/historical - Get historical weather

    • Query params: lat, lon, dt (timestamp), units, lang
  • GET /api/weather/forecast - Get weather forecast

    • Query params: lat, lon, exclude, units, lang
  • GET /docs - Interactive API documentation (Swagger UI)

Web UI Mode

Start the web application:

python -m src.api.main --ui

Access the web UI at http://localhost:8000

Runtime Mode Switching

You can switch modes at runtime using parameters:

From CLI:

# Start in interactive mode, then execute specific commands
python -m src.main --mode interactive
# Inside interactive mode, you can switch modes by typing:
> mode current
> mode historical
> mode forecast

From API:

# Switch mode by calling different endpoints
curl http://localhost:8000/api/weather/current?q=London
curl http://localhost:8000/api/weather/forecast?lat=20.29&lon=85.84

From Config: Override default mode using environment variables:

WEATHER_MODE=historical python -m src.main --mode batch

Output

Console Output

Weather data is printed to the console in a formatted JSON structure.

File Output

Weather data is automatically saved to outputs/ directory with timestamped filenames:

  • CurrentWeatherData_<timestamp>.json
  • HistoricalWeatherData_<timestamp>.json
  • ForecastedWeatherData_<timestamp>.json

Visualization

When --visualize flag is used, matplotlib/seaborn charts are generated showing temperature trends.

Project Structure

weather_fetcher_api/
├── src/
│   ├── weather_fetchers/       # Weather data fetching classes
│   │   ├── base.py             # Abstract base class
│   │   ├── current.py          # Current weather fetcher
│   │   ├── historical.py       # Historical weather fetcher
│   │   └── forecast.py         # Forecast weather fetcher
│   ├── utils/                  # Utility modules
│   │   ├── config.py          # Configuration loader
│   │   ├── security.py        # API key encryption/decryption
│   │   ├── datetime_utils.py  # Date/time utilities
│   │   ├── storage.py         # Data storage utilities
│   │   ├── visualization.py   # Visualization utilities
│   │   ├── logger.py          # Logging configuration
│   │   └── exceptions.py       # Custom exceptions
│   ├── api/                   # REST API
│   │   ├── main.py            # FastAPI application
│   │   └── routes.py          # API endpoints
│   └── main.py                # CLI entry point
├── frontend/                  # Web UI
│   ├── templates/             # HTML templates
│   └── static/                # CSS and JS
│       ├── css/
│       └── js/
├── tests/                     # Unit and integration tests
├── examples/                  # Usage examples
├── config/                    # Configuration files
├── outputs/                   # Output data (not tracked in git)
├── .env.example              # Environment variables template
├── .gitignore                # Git ignore rules
├── requirements.txt          # Python dependencies
├── README.md                 # This file
├── agents.md                 # Development guidelines
└── CHANGELOG.md              # Version history

Development

Running Tests

pytest tests/

With coverage:

pytest --cov=src tests/

Code Formatting

black src/ tests/

Linting

flake8 src/ tests/

Type Checking

mypy src/

Contributing

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

License

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

Acknowledgments

  • OpenWeatherMap API for providing weather data
  • FastAPI for the excellent web framework
  • Matplotlib and Seaborn for visualization capabilities

Support

For issues, questions, or contributions, please open an issue on the GitHub repository.

About

A comprehensive Python weather data fetcher CLI tool with REST API and web dashboard. Retrieve current weather, historical weather data, and weather forecasts from OpenWeatherMap API. Features multi-mode CLI, RESTful API endpoints, data visualization, encrypted API key management, and responsive web UI for seamless weather data integration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages