Skip to content

Instantly share code, notes, and snippets.

@thuck
Forked from bjesus/README.md
Last active May 25, 2021 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuck/f91ee4fb811d654550cd62272f9626b3 to your computer and use it in GitHub Desktop.
Save thuck/f91ee4fb811d654550cd62272f9626b3 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': '❄️'
}
def format_chances(hour):
return ", ".join(f"{k.replace('chanceof', '').capitalize()}: {v}%"
for k,v in hour.items() if "chanceof" in k and int(v) > 40)
def parse_weather(weather):
time_filter = datetime.now().hour-2
current = weather['current_condition'][0]
data = {
'text': (
f"{current['FeelsLikeC']}°"
f"{WEATHER_CODES[current['weatherCode']]}"),
'tooltip': (
f"<b>{current['weatherDesc'][0]['value']} {current['temp_C']}°</b>"
f"\nFeels like: {current['FeelsLikeC']}°"
f"\nWind: {current['windspeedKmph']}Km/h"
f"\nHumidity: {current['humidity']}%\n")
}
for i, day in enumerate(weather['weather']):
date = day['date']
max_temp = day['maxtempC']
min_temp = day['mintempC']
sunrise = day['astronomy'][0]['sunrise'].replace(' ','')
sunset = day['astronomy'][0]['sunset'].replace(' ','')
header = {
0: f"<b><u>{date} - Today</u></b>",
1: f"<b><u>{date} - Tomorrow</u></b>"
}
data['tooltip'] += (
f"\n{header.get(i, f'<b><u>{date}</u></b>')}"
f" <span foreground='#ff6961'>{max_temp}°</span>"
f"/<span foreground='#aec6cf'>{min_temp}°</span>"
f" {sunrise} "
f"/{sunset} \n")
data['tooltip'] += '\n'.join(
f"<span foreground='#cfcfc4'>{hour['time'].zfill(4)[:2]}:</span>"
f" {hour['FeelsLikeC']}°"
f"{WEATHER_CODES[hour['weatherCode']]}"
f" <span foreground='#cfcfc4'>{hour['weatherDesc'][0]['value']}</span>"
f" <span foreground='#aec6cf'>|{format_chances(hour)}|</span>"
for hour in day['hourly'] if not (i == 0 and int(hour['time'].zfill(4)[:2]) < time_filter)
) + '\n'
print(json.dumps(data))
if __name__ == "__main__":
weather = requests.get("https://wttr.in/?format=j1").json()
parse_weather(weather)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment