|
1 | 1 | import requests
|
2 | 2 |
|
3 |
| -APPID = "" # <-- Put your OpenWeatherMap appid here! |
4 |
| -URL_BASE = "https://api.openweathermap.org/data/2.5/" |
5 |
| - |
6 |
| - |
7 |
| -def current_weather(q: str = "Chicago", appid: str = APPID) -> dict: |
8 |
| - """https://openweathermap.org/api""" |
9 |
| - return requests.get(URL_BASE + "weather", params=locals()).json() |
10 |
| - |
11 |
| - |
12 |
| -def weather_forecast(q: str = "Kolkata, India", appid: str = APPID) -> dict: |
13 |
| - """https://openweathermap.org/forecast5""" |
14 |
| - return requests.get(URL_BASE + "forecast", params=locals()).json() |
15 |
| - |
16 |
| - |
17 |
| -def weather_onecall(lat: float = 55.68, lon: float = 12.57, appid: str = APPID) -> dict: |
18 |
| - """https://openweathermap.org/api/one-call-api""" |
19 |
| - return requests.get(URL_BASE + "onecall", params=locals()).json() |
| 3 | +# Put your API key(s) here |
| 4 | +OPENWEATHERMAP_API_KEY = "" |
| 5 | +WEATHERSTACK_API_KEY = "" |
| 6 | + |
| 7 | +# Define the URL for the APIs with placeholders |
| 8 | +OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather" |
| 9 | +WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current" |
| 10 | + |
| 11 | + |
| 12 | +def current_weather(location: str) -> list[dict]: |
| 13 | + """ |
| 14 | + >>> current_weather("location") |
| 15 | + Traceback (most recent call last): |
| 16 | + ... |
| 17 | + ValueError: No API keys provided or no valid data returned. |
| 18 | + """ |
| 19 | + weather_data = [] |
| 20 | + if OPENWEATHERMAP_API_KEY: |
| 21 | + params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} |
| 22 | + response_openweathermap = requests.get( |
| 23 | + OPENWEATHERMAP_URL_BASE, params=params_openweathermap |
| 24 | + ) |
| 25 | + weather_data.append({"OpenWeatherMap": response_openweathermap.json()}) |
| 26 | + if WEATHERSTACK_API_KEY: |
| 27 | + params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} |
| 28 | + response_weatherstack = requests.get( |
| 29 | + WEATHERSTACK_URL_BASE, params=params_weatherstack |
| 30 | + ) |
| 31 | + weather_data.append({"Weatherstack": response_weatherstack.json()}) |
| 32 | + if not weather_data: |
| 33 | + raise ValueError("No API keys provided or no valid data returned.") |
| 34 | + return weather_data |
20 | 35 |
|
21 | 36 |
|
22 | 37 | if __name__ == "__main__":
|
23 | 38 | from pprint import pprint
|
24 | 39 |
|
25 |
| - while True: |
26 |
| - location = input("Enter a location:").strip() |
| 40 | + location = "to be determined..." |
| 41 | + while location: |
| 42 | + location = input("Enter a location (city name or latitude,longitude): ").strip() |
27 | 43 | if location:
|
28 |
| - pprint(current_weather(location)) |
29 |
| - else: |
30 |
| - break |
| 44 | + try: |
| 45 | + weather_data = current_weather(location) |
| 46 | + for forecast in weather_data: |
| 47 | + pprint(forecast) |
| 48 | + except ValueError as e: |
| 49 | + print(repr(e)) |
| 50 | + location = "" |
0 commit comments