Skip to content

mikebc23/weather-cli

Repository files navigation

Weather CLI

A simple, fast, and feature-rich command-line weather tool with multiple data sources and output formats.

Features

  • 🌍 Multiple location input types: City names, ZIP codes, coordinates (decimal & DMS), auto-detection
  • πŸ“Š Multiple output formats: Simple, visual, raw JSON
  • πŸ“ Unit systems: Metric and Imperial units
  • πŸƒβ€β™‚οΈ Fast and lightweight: Minimal dependencies, quick responses
  • πŸ’Ύ Smart caching: 5-minute cache to reduce API calls
  • 🌐 Multiple data sources: Open-Meteo (implemented), wttr.in and NWS (planned)
  • βš™οΈ Configurable: Config file and command-line options

Installation

# Clone the repository
git clone https://github.com/mikebc23/weather-cli.git
cd weather-cli

# Install in development mode
pip install -e .

# Or install from source
pip install .

Quick Start

# Current location (auto-detect via IP)
weather

# City names
weather "New York"
weather "San JosΓ©, Costa Rica"
weather "Tokyo"

# Coordinates
weather "40.7128,-74.0060"           # NYC
weather "9.9281,-84.0907"            # San JosΓ©, CR
weather --lat=51.5074 --lon=-0.1278  # London

# Different output formats
weather "Tokyo" --format=visual
weather "London" --format=simple --units=imperial
weather "NYC" --format=raw        # JSON output

Output Formats

Simple (default)

San JosΓ©, CR: 22Β°C, Partly Cloudy
Feels like: 25Β°C | Humidity: 65% | Wind: 8 km/h

Visual

San JosΓ©, CR
     \   /     22Β°C
      .-.      Partly Cloudy
   ― (   ) ―   β†— 8 km/h
      `-'      65% humidity
     /   \

Raw JSON

{
  "location": {
    "name": "San JosΓ©, Costa Rica",
    "latitude": 9.9281,
    "longitude": -84.0907,
    "country": "Costa Rica"
  },
  "current": {
    "temperature_2m": 22.5,
    "relative_humidity_2m": 65,
    "apparent_temperature": 25.1,
    "wind_speed_10m": 8.2,
    "condition": "Partly cloudy",
    "pressure_msl": 1013.2,
    "cloud_cover": 25,
    "uv_index": 3.2
  },
  "units": {
    "temperature": "Β°C",
    "wind_speed": "km/h",
    "pressure": "hPa"
  },
  "source": "open-meteo",
  "timestamp": "2025-07-14T15:30:00Z",
  "cache_hit": false
}

Command Line Options

weather [location] [options]

Positional Arguments:
  location              Location (city, ZIP code, coordinates, or auto-detect)

Location Options:
  --lat LAT             Latitude (use with --lon)
  --lon LON             Longitude (use with --lat)

Format Options:
  --format FORMAT       Output format: simple, visual, raw (default: simple)
  --units UNITS         Unit system: metric, imperial (default: metric)

Data Source Options:
  --source SOURCE       Weather source: open-meteo, wttr, nws (default: open-meteo)
  --timeout TIMEOUT     HTTP timeout in seconds (default: 10)

Cache Options:
  --no-cache            Disable cache usage
  --clear-cache         Clear cache and exit

Other Options:
  --config CONFIG       Path to configuration file
  --debug               Enable debug output
  --version             Show version
  --help                Show help message

Configuration

The tool uses a configuration file at ~/.weather.conf (JSON format):

{
  "units": "metric",
  "format": "simple",
  "source": "open-meteo",
  "cache_duration": 300,
  "timeout": 10
}

Supported Location Formats

Format Example Description
Auto-detect weather Uses IP geolocation
City name "New York" City, country, or address
ZIP code "10001" US ZIP codes (5 or 9 digits)
Decimal coordinates "40.7128,-74.0060" Latitude,longitude
Explicit coordinates --lat=40.7128 --lon=-74.0060 Separate lat/lon flags
DMS coordinates "40Β°42'46.0\"N 74Β°00'21.6\"W" Degrees, minutes, seconds

Project Structure

weather-cli/
β”œβ”€β”€ weather/                 # Main package
β”‚   β”œβ”€β”€ main.py              # CLI entry point
β”‚   β”œβ”€β”€ config.py            # Configuration management
β”‚   β”œβ”€β”€ cache.py             # Caching system
β”‚   β”œβ”€β”€ location.py          # Location services (geocoding)
β”‚   β”œβ”€β”€ sources/             # Weather data sources
β”‚   β”‚   β”œβ”€β”€ base.py          # Abstract base class
β”‚   β”‚   β”œβ”€β”€ open_meteo.py    # Open-Meteo API
β”‚   β”‚   β”œβ”€β”€ wttr.py          # wttr.in service
β”‚   β”‚   └── nws.py           # National Weather Service
β”‚   β”œβ”€β”€ formatters/          # Output formatters
β”‚   β”‚   β”œβ”€β”€ base.py          # Abstract formatter
β”‚   β”‚   β”œβ”€β”€ simple.py        # Simple text output
β”‚   β”‚   β”œβ”€β”€ visual.py        # Visual ASCII art format
β”‚   β”‚   └── raw.py           # Raw JSON output
β”‚   └── utils/               # Utility functions
β”‚       β”œβ”€β”€ http.py          # HTTP client wrapper
β”‚       β”œβ”€β”€ units.py         # Unit conversions
β”‚       └── exceptions.py    # Custom exceptions
β”œβ”€β”€ tests/                   # Test suite
β”‚   β”œβ”€β”€ conftest.py          # pytest fixtures
β”‚   └── test_location.py     # Location tests
β”œβ”€β”€ setup.py                 # Package setup
β”œβ”€β”€ requirements.txt         # Dependencies
└── README.md                # This file

Data Sources

Open-Meteo (Primary)

  • Status: Implemented and working
  • Coverage: Global
  • Features: Current weather, forecasts, no API key required
  • Rate limit: Generous free tier
  • URL: https://open-meteo.com/

wttr.in (Planned)

  • Status: Placeholder implemented
  • Coverage: Global
  • Features: Simple curl-based service, ASCII art built-in
  • Rate limit: Fair use

National Weather Service (Planned)

  • Status: Placeholder implemented
  • Coverage: United States only
  • Features: Official US government weather data
  • Rate limit: None (government API)

Dependencies

  • requests>=2.25.0 - HTTP requests
  • urllib3>=1.26.0 - HTTP client utilities

Development dependencies:

  • pytest - Testing framework

Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=weather

# Run specific test file
pytest tests/test_location.py -v

Examples

# Different locations
weather                                   # Auto-detect
weather "Paris, France"                   # City name
weather "90210"                           # ZIP code
weather "35.6762,139.6503"                # Tokyo coordinates

# Different formats and units
weather "London" --format=visual --units=imperial
weather "Moscow" --format=simple --units=metric
weather "Sydney" --format=raw             # JSON output

# Using explicit coordinates
weather --lat=55.7558 --lon=37.6173       # Moscow
weather --lat=-33.8688 --lon=151.2093     # Sydney

# Configuration and caching
weather "NYC" --no-cache                  # Skip cache
weather --clear-cache                     # Clear cache
weather "NYC" --timeout=5                 # Custom timeout

Error Handling

The tool provides clear error messages for common issues:

  • Location not found: "Location error: Place not found: Invalid City"
  • Invalid coordinates: "Location error: Invalid latitude: 91.0. Must be between -90 and 90"
  • Network issues: "Weather data error: Failed to get weather from Open-Meteo: Connection timeout"
  • Invalid format: "Error: Unknown format: badformat"

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for your changes
  5. Run tests (pytest)
  6. Commit your changes (git commit -am 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

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

Acknowledgments

About

Simple command-line tool to check the weather

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •