This Python script is not maintained and was replaced by the more robust Rust version below:
-
-
Save bjesus/f8db49e1434433f78e5200dc403d58a3 to your computer and use it in GitHub Desktop.
"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)) |
Thanks for the widget, @bjesus! Could you modify it to also show the wind direction with a little arrow in the waybar?
Hey @mortenivar, you should be able to use winddir16Point
and replace it with a unicode character like 🡰 🡲 🡱 🡳 🡴 🡵 🡶 🡷
.
Hi @bjesus,
I will try that. Thank you very much.
@bjesus - thanks for your script, I am loving it.
Made some changes to add the areaName
and region
to the tooltip because wttr.in sometimes geolocated a very wrong location. You did not specify any license for the snippet, Would you mind if I checked in the modified script in my github. - of course I will give credits.
Hey @truschival - please go ahead! It's yours. Happy to hear you found it useful.
Only problem I'm having right now is that https://wttr.in/?format=j1 is giving a different result than https://wttr.in/ for current condition, so the current temperature is outdated. For example, right now it's 26C but the widget reports 43C, which would have been accurate a couple of hours ago I believe.
Maybe your "interval" is too long. By "default" is 6000. Have you tried with a short interval?
Hey, thanks for the script!
For anyone interested, I played a bit with it and here's the result.
I've made a couple of changes:
* I've added localization support. If you want text to appear in a different language all you have to do is * add a new entry to [the localization dictionary](https://github.com/PavlosMelissinos/dotfiles/blob/166bced1541e47632f34f4f4de081fecc882eb64/.config/waybar/waybar-wttr.py#L11-L45); make sure that the keys match those from the existing entries and * set [`lang`](https://github.com/PavlosMelissinos/dotfiles/blob/166bced1541e47632f34f4f4de081fecc882eb64/.config/waybar/waybar-wttr.py#L47) to that locale. The locale name [has to be supported by wttr.in](https://wttr.in/:translation) * Weather events appear in descending order of probability, e.g. `Fog 32%, Overcast 82%, Rain 24%` now appears as `Overcast 82%, Fog 32%, Rain 24%`
Thanks, works perfectly in es
For any American heathens that prefer fahrenheit and anyone that likes to specify a location. I updated the script to parse arguments for location and temperature unit: https://github.com/khaneliman/dotfiles/blob/main/dots/linux/hyprland/home/.config/waybar/scripts/weather.py
Here's a version that:
- Has a cache to keep the last good forecast. It will use that when it fails to load a forecast.
- Can return wind speed in Mph.
https://github.com/benupsavs/waybar-weather/blob/master/weather.py
Thanks for this script works like a charm. Only a little cosmetical change is needed to make it the perfect fit. How can I change the date output from YYYY-MM-DD into DD-MM-YYYY?
@fhmbrg You need to change data['tooltip'] += f"{day['date']}</b>\n"
to data['tooltip'] += f"{datetime.strptime(day['date'], '%Y-%m-%d').strftime('%d-%m-%Y')}</b>\n"
What this does is take day['date'] which has the YYYY-MM-DD format, convert it to a datetime object (via strptime, string "parse" time) and then pass the result to strftime (string "format" time) to get the final formatting.
edit: fixed nesting in fstring
@PavlosMelissinos thanks for your reply you've made my day.
Is it possible to show the time in 24-hour format instead of pm/am ?
Hey y'all, this gist got a little out of hand with so many fixes and version floating around. I therefore converted the whole script to Rust and made it much more configurable via CLI arguments - you can now set your date format, exact location, AM/PM mode, and more. I thought it's nicer to have this one binary that doesn't depend on Python. check it out here:
https://github.com/bjesus/wttrbar
If I missed your use case please submit an issue there 🙏
Thank you so much!
Thank you! Much appreciated! 🙏🏻😀
Hey y'all, this gist got a little out of hand with so many fixes and version floating around. I therefore converted the whole script to Rust and made it much more configurable via CLI arguments - you can now set your date format, exact location, AM/PM mode, and more. I thought it's nicer to have this one binary that doesn't depend on Python. check it out here:
https://github.com/bjesus/wttrbar
If I missed your use case please submit an issue there pray
I installed the AUR package and replaced my old weather module (python variant) with the following entry, assuming that the date is in dd.mm.yyyy format and the time format is 24h, since I did not specify --ampm:
But date is shown as YYYY-MM-DD and time format is am/pm.
"custom/weather": {
"format": "{}",
"tooltip": true,
"interval": 3600,
"exec": "wttrbar --location=52382 --main-indicator=FeelsLikeC --date-format=%d.%m.%Y",
"return-type": "json"
},
I also tried enclosing e. g. FeelsLikeC in single quotes and without "=" in front of it. How to I set these parameters?
@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
This awesome, hackable Python version is public domain now, right?
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).
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.
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
@veggiedev you can check the wttr docs to see what they allow. Visit their help page and check under Supported location types
.
@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.
@bjesus I was still using this python version (very often it was not working for unknown reason - it was just not showing the weather in some specific refresh-cycle, next hour after refresh it was OK).
Today I was copying this plugin and config to my new Arch sway machine and the script was working fine from console, but swaybar
was complaining about some JSON parsing - weird.
I was googling how to fix this problem and ... I encounter your Rust version which I was not even aware of!
I don't have to say how great news is this!
Your great weather applet is now in rock-solid rust version! Yay! 🎉
Much thank you for this. Now I have it migrated and works like a charm! 🚀 :)
Thanks @manio , such a great message to receive! ❤️ it's nice to have so many people contributing to wttrbar now (14 contributers so far!) and the the strictness of Rust makes it easier to keep it all structured in a sane way.
Hey, thanks for the script!
For anyone interested, I played a bit with it and here's the result.
I've made a couple of changes:
lang
to that locale. The locale name has to be supported by wttr.inFog 32%, Overcast 82%, Rain 24%
now appears asOvercast 82%, Fog 32%, Rain 24%