Skip to content

Instantly share code, notes, and snippets.

@bjesus
Last active February 9, 2024 12:46
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bjesus/f8db49e1434433f78e5200dc403d58a3 to your computer and use it in GitHub Desktop.
Save bjesus/f8db49e1434433f78e5200dc403d58a3 to your computer and use it in GitHub Desktop.
Weather widget for waybar
"custom/weather": {
"format": "{}",
"tooltip": true,
"interval": 3600,
"exec": "waybar-wttr.py",
"return-type": "json"
},
#!/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))
@bjesus
Copy link
Author

bjesus commented Apr 17, 2023

@fhmbrg please use the issues in the repo fo this, so others can benefit from it too.

the syntax is --main-indicator "FeelsLikeC" --dateformat "%d.%m.%Y". I should however change it to use --date-format instead of --dateformat, will update it later

@benupsavs
Copy link

This awesome, hackable Python version is public domain now, right?

@bjesus
Copy link
Author

bjesus commented May 7, 2023

Sure - it always was - but I don't know why one would prefer using this and not the Rust based implementation (which is MIT licensed, so practically also do whatever you want with it).

@AtlasC0R3
Copy link

I personally prefer using this over the Rust solution, as it's easier (at least, to folks familiar with Python) to hack together, or configure, things the way we want to.

@veggiedev
Copy link

is the location grabbed automatically, or do we need to specify it? @fhmbrg has put a number in location, where do i get that number for my specific location? Thanks in advance

@benupsavs
Copy link

@veggiedev you can check the wttr docs to see what they allow. Visit their help page and check under Supported location types.

@veggiedev
Copy link

@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

@daxisunder
Copy link

Can it show both "feelslikeC" and "tempC", if so, please, how to do that?

@benupsavs
Copy link

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.

@bjesus
Copy link
Author

bjesus commented Dec 28, 2023

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