Coverage for src/cvxcli/weather.py: 100%
12 statements
« prev ^ index » next coverage.py v7.6.9, created at 2025-08-27 06:22 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2025-08-27 06:22 +0000
1# Copyright 2023 Stanford University Convex Optimization Group
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""Module for retrieving weather data from the Open-Meteo API.
17This module provides a command-line interface for fetching current weather data
18for a specified location and metric using the Open-Meteo API.
19"""
21import fire # type: ignore
22import requests # type: ignore
25def cli(metric: str, latitude: float = 37.4419, longitude: float = -122.143) -> None:
26 """Get the current weather for a given metric.
28 Parameters
29 ----------
30 metric : str
31 The metric to get the current weather for.
32 Use time, temperature, windspeed, winddirection or weathercode
33 For details: https://open-meteo.com/en/docs
34 latitude : float, optional
35 The latitude to get the current weather for, by default 37.4419
36 longitude : float, optional
37 The longitude to get the current weather for, by default -122.143
38 """
39 url = "https://api.open-meteo.com/v1/forecast"
40 url = f"{url}?latitude={str(latitude)}&longitude={str(longitude)}¤t_weather=true"
41 r = requests.get(url)
43 if r.status_code == 200:
44 if metric in r.json()["current_weather"]:
45 x = r.json()["current_weather"][metric]
46 return x
47 else:
48 raise ValueError("Metric not supported!")
49 else:
50 raise ConnectionError("Open-Meteo is down!")
53def main(): # pragma: no cover
54 """Run the CLI using Fire."""
55 fire.Fire(cli)