This Python script is not maintained and was replaced by the more robust Rust version below:
Last active
February 9, 2024 12:46
-
-
Save bjesus/f8db49e1434433f78e5200dc403d58a3 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 time.replace("00", "").zfill(2) | |
def format_temp(temp): | |
return (hour['FeelsLikeC']+"Β°").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']+"Β°" | |
data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_C']}Β°</b>\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']}Β° β¬οΈ {day['mintempC']}Β° " | |
data['tooltip'] += f"π {day['astronomy'][0]['sunrise']} π {day['astronomy'][0]['sunset']}\n" | |
for hour in day['hourly']: | |
if i == 0: | |
if int(format_time(hour['time'])) < datetime.now().hour-2: | |
continue | |
data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeC'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n" | |
print(json.dumps(data)) |
@veggiedev you can check the wttr docs to see what they allow. Visit their help page and check under
Supported location types
.
Thank you!
@benupsavs
Can it show both "feelslikeC" and "tempC", if so, please, how to do that?
Can it show both "feelslikeC" and "tempC", if so, please, how to do that?
@daxisunder Just copy and paste line 95 and change one of them to be tempC
, and also update the description accordingly.
The rust version allows you to use to use --custom-indicator
to show multiple things as the main indicator.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@veggiedev you can check the wttr docs to see what they allow. Visit their help page and check under
Supported location types
.