mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-07-23 20:34:46 +02:00
polybar-weather: show daily forecast instead of hourly forecast
This commit is contained in:
parent
6a5bc52e26
commit
da6f9ff7aa
1 changed files with 22 additions and 37 deletions
|
@ -25,8 +25,8 @@ def get_location():
|
||||||
return (data["lat"], data["lon"])
|
return (data["lat"], data["lon"])
|
||||||
|
|
||||||
|
|
||||||
def get_weather(apikey, latitude, longitude, endpoint, count=1):
|
def get_weather(apikey, latitude, longitude, endpoint):
|
||||||
"""Return forecasts data from openweathermap."""
|
"""Return data from openweathermap."""
|
||||||
logger.debug("query openweathermap for %s, %s", latitude, longitude)
|
logger.debug("query openweathermap for %s, %s", latitude, longitude)
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
f"https://api.openweathermap.org/data/2.5/{endpoint}",
|
f"https://api.openweathermap.org/data/2.5/{endpoint}",
|
||||||
|
@ -35,7 +35,7 @@ def get_weather(apikey, latitude, longitude, endpoint, count=1):
|
||||||
"lat": latitude,
|
"lat": latitude,
|
||||||
"lon": longitude,
|
"lon": longitude,
|
||||||
"units": "metric",
|
"units": "metric",
|
||||||
"cnt": count,
|
"cnt": 1,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
@ -44,11 +44,11 @@ def get_weather(apikey, latitude, longitude, endpoint, count=1):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def format_weather(data):
|
def format_weather(data, show_temperature=True):
|
||||||
"""Translate OWM icon to WeatherIcons."""
|
"""Translate OWM icon to WeatherIcons."""
|
||||||
# https://erikflowers.github.io/weather-icons/
|
# https://erikflowers.github.io/weather-icons/
|
||||||
icon = data["weather"][0]["icon"]
|
icon = data["weather"][0]["icon"]
|
||||||
temperature = data["main"]["temp"]
|
temperature = data["main"]["temp"] if show_temperature else 0
|
||||||
if icon == "01d" and temperature > 32:
|
if icon == "01d" and temperature > 32:
|
||||||
icon = ""
|
icon = ""
|
||||||
else:
|
else:
|
||||||
|
@ -72,15 +72,10 @@ def format_weather(data):
|
||||||
"50d": "", # Fog - day
|
"50d": "", # Fog - day
|
||||||
"50n": "🌫", # Fog - night
|
"50n": "🌫", # Fog - night
|
||||||
}.get(icon, "")
|
}.get(icon, "")
|
||||||
return "".join(
|
output = ["%{Tx}", icon, "%{T-}"]
|
||||||
[
|
if show_temperature:
|
||||||
"%{Tx}",
|
output += [" ", str(int(round(temperature))), "°C"]
|
||||||
icon,
|
return "".join(output)
|
||||||
"%{T-} ",
|
|
||||||
str(int(round(temperature))),
|
|
||||||
"°C",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def update_status(status, output):
|
def update_status(status, output):
|
||||||
|
@ -109,9 +104,6 @@ if __name__ == "__main__":
|
||||||
default=os.environ.get("OWM_API_KEY"),
|
default=os.environ.get("OWM_API_KEY"),
|
||||||
help="OpenWeatherMap API key",
|
help="OpenWeatherMap API key",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
"--forecasts", default=2, type=int, help="Number of forecasts to fetch"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--font-index", default=3, type=int, help="Font Awesome 1-index"
|
"--font-index", default=3, type=int, help="Font Awesome 1-index"
|
||||||
)
|
)
|
||||||
|
@ -156,33 +148,26 @@ if __name__ == "__main__":
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
location = get_location()
|
location = get_location()
|
||||||
|
|
||||||
# Grab current weather and forecast
|
# Grab current weather and daily forecast
|
||||||
current_weather = get_weather(options.owm_api_key, *location, "weather")
|
current_weather = get_weather(options.owm_api_key, *location, "weather")
|
||||||
if options.forecasts:
|
daily_weather = get_weather(options.owm_api_key, *location, "forecast/daily")
|
||||||
forecast_weather = get_weather(
|
daily_weather = daily_weather["list"][0]
|
||||||
options.owm_api_key, *location, "forecast", count=options.forecasts + 1
|
daily_weather_ts = time.strftime(
|
||||||
)["list"]
|
"%Y-%m-%d %H:%M %Z", time.gmtime(daily_weather["dt"])
|
||||||
if forecast_weather[0]["dt"] - time.time() < 7200:
|
)
|
||||||
logger.debug("discard first forecast, too soon")
|
|
||||||
forecast_weather = forecast_weather[1:]
|
|
||||||
else:
|
|
||||||
forecast_weather.pop()
|
|
||||||
logger.info(f"next forecast: {forecast_weather[0]['dt_txt']} UTC")
|
|
||||||
else:
|
|
||||||
forecast_weather = []
|
|
||||||
description = current_weather["weather"][0]["description"]
|
description = current_weather["weather"][0]["description"]
|
||||||
city = current_weather["name"]
|
city = current_weather["name"]
|
||||||
logger.info(f"current weather at {city}: {description}")
|
logger.info(f"current weather at {city}: {description}")
|
||||||
|
logger.info(f"daily forecast: {daily_weather_ts}")
|
||||||
|
|
||||||
# Format output
|
# Format output
|
||||||
conditions = [
|
conditions = [format_weather(current_weather)]
|
||||||
format_weather(data) for data in [current_weather] + forecast_weather
|
conditions += [
|
||||||
|
format_weather(daily_weather, False),
|
||||||
|
"{}—{}°C".format(
|
||||||
|
round(daily_weather["temp"]["min"]), round(daily_weather["temp"]["max"])
|
||||||
|
),
|
||||||
]
|
]
|
||||||
while len(conditions) >= 2:
|
|
||||||
if conditions[-1] == conditions[-2]:
|
|
||||||
conditions.pop()
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
conditions.insert(0, f"%{{F#888}}%{{Tx}}%{{T-}} {city}%{{F-}}")
|
conditions.insert(0, f"%{{F#888}}%{{Tx}}%{{T-}} {city}%{{F-}}")
|
||||||
output = " ".join(conditions).replace("%{Tx}", "%%{T%d}" % options.font_index)
|
output = " ".join(conditions).replace("%{Tx}", "%%{T%d}" % options.font_index)
|
||||||
logger.debug("output: %s", output)
|
logger.debug("output: %s", output)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue