Skip to content

Instantly share code, notes, and snippets.

@bjesus
Last active Sep 16, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@bjesus bjesus commented Sep 13, 2020

screenshot_2020-09-13_21-45-04_877480664

@crstian19

This comment has been minimized.

Copy link

@crstian19 crstian19 commented Jan 3, 2021

Thank you dude! Love you <3

@etrigan63

This comment has been minimized.

Copy link

@etrigan63 etrigan63 commented Apr 28, 2021

What font do I use for this? I am missing the sunrise/sunset icons.

@bjesus

This comment has been minimized.

@tmpm697

This comment has been minimized.

Copy link

@tmpm697 tmpm697 commented Jul 22, 2021

@bjesus : this script causes font adjustment to my current font, is there how to fix it? if it just dues to font and have must to use noto-fonts-emoji, that's sad.

@bjesus

This comment has been minimized.

Copy link
Owner Author

@bjesus bjesus commented Jul 22, 2021

My script doesn't change anything about your fonts. Installing and uninstalling Noto Emoji (or any other emoji font) is outside the scope of this script and you should follow a method that's relevant to your operating system.

@tmpm697

This comment has been minimized.

Copy link

@tmpm697 tmpm697 commented Jul 22, 2021

yeah, I found that weather icons cause the problems. And fixed that on my pc.

@Zeioth

This comment has been minimized.

Copy link

@Zeioth Zeioth commented Sep 16, 2021

Weird, I can run the command manually, but not from waybar.

Here is my config file.
https://pastebin.com/TMRE7ZSM

FIXED: I had to put waybar-wttr.py in ~/.local/bin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment