/
ssmih
/
Python
Обзор
Документация
Войти
/
ssmih
/
Python
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
web_programming/current_weather.py
57 строк
2 KB
Christian Clauss
Lukazlim: Replace dependency `requests` with `httpx` (#12744)
14 май 2025, 04:42
Не верифицирован
14 май 2025, 04:42
a2fa32c
Код
Авторство
О чём код?
# /// script # requires-python = ">=3.13" # dependencies = [ # "httpx", # ] # /// import httpx # Put your API key(s) here 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(location: str) -> list[dict]: """ >>> current_weather("location") Traceback (most recent call last): ... ValueError: No API keys provided or no valid data returned. """ weather_data = [] if OPENWEATHERMAP_API_KEY: params_openweathermap = {"q": location, "appid": OPENWEATHERMAP_API_KEY} response_openweathermap = httpx.get( OPENWEATHERMAP_URL_BASE, params=params_openweathermap, timeout=10 ) weather_data.append({"OpenWeatherMap": response_openweathermap.json()}) if WEATHERSTACK_API_KEY: params_weatherstack = {"query": location, "access_key": WEATHERSTACK_API_KEY} response_weatherstack = httpx.get( WEATHERSTACK_URL_BASE, params=params_weatherstack, timeout=10 ) 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__": from pprint import pprint location = "to be determined..." while location: location = input("Enter a location (city name or latitude,longitude): ").strip() if location: try: weather_data = current_weather(location) for forecast in weather_data: pprint(forecast) except ValueError as e: print(repr(e)) location = ""