-
-
Save SirJson/0fc741f48bfc92ac90c0505d2600a543 to your computer and use it in GitHub Desktop.
Weather widget for waybar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"custom/weather": { | |
"format": "{}", | |
"tooltip": true, | |
"interval": 3600, | |
"exec": "waybar-wttr.py", | |
"return-type": "json" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
import requests | |
from datetime import datetime | |
WEATHER_CODES = { | |
'113': '☀️', | |
'116': '⛅️', | |
'119': '☁️', | |
'122': '☁️', | |
'143': '🌫', | |
'176': '🌦', | |
'179': '🌧', | |
'182': '🌧', | |
'185': '🌧', | |
'200': '⛈', | |
'227': '🌨', | |
'230': '❄️', | |
'248': '🌫', | |
'260': '🌫', | |
'263': '🌦', | |
'266': '🌦', | |
'281': '🌧', | |
'284': '🌧', | |
'293': '🌦', | |
'296': '🌦', | |
'299': '🌧', | |
'302': '🌧', | |
'305': '🌧', | |
'308': '🌧', | |
'311': '🌧', | |
'314': '🌧', | |
'317': '🌧', | |
'320': '🌨', | |
'323': '🌨', | |
'326': '🌨', | |
'329': '❄️', | |
'332': '❄️', | |
'335': '❄️', | |
'338': '❄️', | |
'350': '🌧', | |
'353': '🌦', | |
'356': '🌧', | |
'359': '🌧', | |
'362': '🌧', | |
'365': '🌧', | |
'368': '🌨', | |
'371': '❄️', | |
'374': '🌧', | |
'377': '🌧', | |
'386': '⛈', | |
'389': '🌩', | |
'392': '⛈', | |
'395': '❄️' | |
} | |
data = {} | |
weather = requests.get("https://wttr.in/?format=j1").json() | |
def format_time(time): | |
return f'{int(int(time) / 100)}:00' | |
def format_temp(temp): | |
return (hour['FeelsLikeC']+"°C").ljust(3) | |
def format_chances(hour): | |
chances = { | |
"chanceoffog": "Fog", | |
"chanceoffrost": "Frost", | |
"chanceofovercast": "Overcast", | |
"chanceofrain": "Rain", | |
"chanceofsnow": "Snow", | |
"chanceofsunshine": "Sunshine", | |
"chanceofthunder": "Thunder", | |
"chanceofwindy": "Wind" | |
} | |
conditions = [] | |
for event in chances.keys(): | |
if int(hour[event]) > 0: | |
conditions.append(chances[event]+" "+hour[event]+"%") | |
return ", ".join(conditions) | |
data['text'] = WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \ | |
" "+weather['current_condition'][0]['FeelsLikeC']+"°C " | |
data['tooltip'] = f"➜ <b>Now in {weather['nearest_area'][0]['region'][0]['value']}</b>\n" | |
data['tooltip'] += f"► {weather['current_condition'][0]['weatherDesc'][0]['value']} @ {weather['current_condition'][0]['temp_C']}°C\n" | |
data['tooltip'] += f"► Feels like: {weather['current_condition'][0]['FeelsLikeC']}°\n" | |
data['tooltip'] += f"► Wind: {weather['current_condition'][0]['windspeedKmph']}km/h\n" | |
data['tooltip'] += f"► Humidity: {weather['current_condition'][0]['humidity']}%\n" | |
for i, day in enumerate(weather['weather']): | |
data['tooltip'] += f"\n➜ <b>" | |
if i == 0: | |
data['tooltip'] += "Today, " | |
if i == 1: | |
data['tooltip'] += "Tomorrow, " | |
data['tooltip'] += f"{day['date']}</b>\n" | |
data['tooltip'] += f"► ⬆️ {day['maxtempC']}°C\t⬇️{day['mintempC']}°C\t" | |
data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']}\t🌇 {day['astronomy'][0]['sunset']}\n" | |
for hour in day['hourly']: | |
if i == 0: | |
if int(hour['time'].replace("00", "").zfill(2)) < datetime.now().hour-2: | |
continue | |
data['tooltip'] += f"<i>{format_time(hour['time'])}</i>\t • {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeC'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n" | |
print(json.dumps(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment