mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Change from only weatherstack to both (#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 <cclauss@me.com>
This commit is contained in:
parent
eb17fcf8f5
commit
a23dd7ecbe
|
@ -1,30 +1,50 @@
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
APPID = "" # <-- Put your OpenWeatherMap appid here!
|
# Put your API key(s) here
|
||||||
URL_BASE = "https://api.openweathermap.org/data/2.5/"
|
OPENWEATHERMAP_API_KEY = ""
|
||||||
|
WEATHERSTACK_API_KEY = ""
|
||||||
|
|
||||||
|
# Define the URL for the APIs with placeholders
|
||||||
|
OPENWEATHERMAP_URL_BASE = "https://api.openweathermap.org/data/2.5/weather"
|
||||||
|
WEATHERSTACK_URL_BASE = "http://api.weatherstack.com/current"
|
||||||
|
|
||||||
|
|
||||||
def current_weather(q: str = "Chicago", appid: str = APPID) -> dict:
|
def current_weather(location: str) -> list[dict]:
|
||||||
"""https://openweathermap.org/api"""
|
"""
|
||||||
return requests.get(URL_BASE + "weather", params=locals()).json()
|
>>> current_weather("location")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
def weather_forecast(q: str = "Kolkata, India", appid: str = APPID) -> dict:
|
ValueError: No API keys provided or no valid data returned.
|
||||||
"""https://openweathermap.org/forecast5"""
|
"""
|
||||||
return requests.get(URL_BASE + "forecast", params=locals()).json()
|
weather_data = []
|
||||||
|
if OPENWEATHERMAP_API_KEY:
|
||||||
|
params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY}
|
||||||
def weather_onecall(lat: float = 55.68, lon: float = 12.57, appid: str = APPID) -> dict:
|
response_openweathermap = requests.get(
|
||||||
"""https://openweathermap.org/api/one-call-api"""
|
OPENWEATHERMAP_URL_BASE, params=params_openweathermap
|
||||||
return requests.get(URL_BASE + "onecall", params=locals()).json()
|
)
|
||||||
|
weather_data.append({"OpenWeatherMap": response_openweathermap.json()})
|
||||||
|
if WEATHERSTACK_API_KEY:
|
||||||
|
params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY}
|
||||||
|
response_weatherstack = requests.get(
|
||||||
|
WEATHERSTACK_URL_BASE, params=params_weatherstack
|
||||||
|
)
|
||||||
|
weather_data.append({"Weatherstack": response_weatherstack.json()})
|
||||||
|
if not weather_data:
|
||||||
|
raise ValueError("No API keys provided or no valid data returned.")
|
||||||
|
return weather_data
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
while True:
|
location = "to be determined..."
|
||||||
location = input("Enter a location:").strip()
|
while location:
|
||||||
|
location = input("Enter a location (city name or latitude,longitude): ").strip()
|
||||||
if location:
|
if location:
|
||||||
pprint(current_weather(location))
|
try:
|
||||||
else:
|
weather_data = current_weather(location)
|
||||||
break
|
for forecast in weather_data:
|
||||||
|
pprint(forecast)
|
||||||
|
except ValueError as e:
|
||||||
|
print(repr(e))
|
||||||
|
location = ""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user