77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import openmeteo_requests
|
|
import requests_cache
|
|
from retry_requests import retry
|
|
import requests
|
|
import sys
|
|
|
|
conditions = {
|
|
0: "",
|
|
1: "",
|
|
2: "",
|
|
3: "",
|
|
45: "",
|
|
48: "",
|
|
51: "",
|
|
53: "",
|
|
55: "",
|
|
61: "",
|
|
63: "",
|
|
65: "",
|
|
71: "",
|
|
73: "",
|
|
75: "",
|
|
95: "",
|
|
96: "",
|
|
99: "",
|
|
}
|
|
|
|
latitude, longitude = eval(requests.get("https://ipinfo.io/loc").text)
|
|
city = requests.get("https://ipinfo.io/city").text.strip()
|
|
|
|
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
|
|
retry_session = retry(cache_session, retries=5, backoff_factor = 0.2)
|
|
openmeteo = openmeteo_requests.Client(session = retry_session)
|
|
|
|
url = "https://api.open-meteo.com/v1/forecast"
|
|
params = {
|
|
"latitude": latitude,
|
|
"longitude": longitude,
|
|
"current": ["is_day", "weather_code", "temperature_2m"],
|
|
"temperature_unit": "fahrenheit",
|
|
"wind_speed_unit": "mph",
|
|
"precipitation_unit": "inch"
|
|
}
|
|
|
|
responses = openmeteo.weather_api(url, params=params)
|
|
response = responses[0]
|
|
|
|
current = response.Current()
|
|
current_is_day = bool(current.Variables(0).Value())
|
|
current_weather_code = int(current.Variables(1).Value())
|
|
current_temperature = int(current.Variables(2).Value())
|
|
|
|
print(f"{city} {conditions[current_weather_code]} {current_temperature}°F")
|
|
|
|
|
|
#try:
|
|
#location_info = weather_json["properties"]["relativeLocation"]["properties"]
|
|
#except KeyError:
|
|
#if weather_json["status"] == 404:
|
|
#print("NOAA only supports U.S. locations")
|
|
|
|
#city = location_info["city"]
|
|
#state = location_info["state"]
|
|
|
|
#forecast_url = weather_json["properties"]["forecast"]
|
|
#forecast_json = requests.get(forecast_url).json()
|
|
|
|
#current_forecast = forecast_json["properties"]["periods"][0]
|
|
#temperature = current_forecast["temperature"]
|
|
#temperature_units = current_forecast["temperatureUnit"]
|
|
#short_forecast = current_forecast["shortForecast"]
|
|
|
|
#try:
|
|
#print(f"{city}, {state} {conditions[short_forecast]} {temperature}°{temperature_units}")
|
|
#except KeyError:
|
|
#print(f"{city}, {state} {short_forecast} {temperature}°{temperature_units}")
|