Skip to content

Commit a489408

Browse files
nababuddinpre-commit-ci[bot]cclauss
authored andcommitted
Change from only weatherstack to both (TheAlgorithms#10882)
* Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update current_weather.py * Update current_weather.py * Update current_weather.py * Update current_weather.py * import requests --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent c8d28a7 commit a489408

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

web_programming/current_weather.py

+42-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
import requests
22

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
2035

2136

2237
if __name__ == "__main__":
2338
from pprint import pprint
2439

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()
2743
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

Comments
 (0)