💄 style(waybar): format weather python script

This commit is contained in:
Sergio Laín 2024-01-01 14:04:29 +01:00
parent da2da38379
commit fd1ea55907
No known key found for this signature in database
GPG key ID: 14C9B8080681777B
3 changed files with 81 additions and 71 deletions

View file

@ -12,7 +12,7 @@ return {
{ "dq", desc = "Delete All Macros" }, { "dq", desc = "Delete All Macros" },
}, },
opts = { opts = {
useNerdFontsIcons = true, useNerdFontsIcons = false,
slots = { "a", "b", "c", "d" }, slots = { "a", "b", "c", "d" },
mapping = { mapping = {
startStopRecording = "q", startStopRecording = "q",

View file

@ -55,7 +55,7 @@
}, },
"custom/weather": { "custom/weather": {
"exec": "python3 ~/.config/waybar/scripts/weather.py", "exec": "python3 ~/.config/waybar/scripts/weather.py",
"restart-interval": 100, "restart-interval": 300,
"return-type": "json", "return-type": "json",
"on-click": "kitty --hold --class center-float wttr Alicante", "on-click": "kitty --hold --class center-float wttr Alicante",
"on-click-right": "kitty --hold --class center-float wttr -v2 Alicante", "on-click-right": "kitty --hold --class center-float wttr -v2 Alicante",

View file

@ -1,58 +1,59 @@
#!/usr/bin/env python #!/usr/bin/env python
import json import json
import requests
from datetime import datetime from datetime import datetime
import requests
WEATHER_CODES = { WEATHER_CODES = {
'113': '🌈', "113": "🌈",
'116': '⛅️', "116": "⛅️",
'119': '☁️', "119": "☁️",
'122': '☁️', "122": "☁️",
'143': '🌫', "143": "🌫",
'176': '🌦', "176": "🌦",
'179': '🌧', "179": "🌧",
'182': '🌧', "182": "🌧",
'185': '🌧', "185": "🌧",
'200': '', "200": "",
'227': '🌨', "227": "🌨",
'230': '❄️', "230": "❄️",
'248': '🌫', "248": "🌫",
'260': '🌫', "260": "🌫",
'263': '🌦', "263": "🌦",
'266': '🌦', "266": "🌦",
'281': '🌧', "281": "🌧",
'284': '🌧', "284": "🌧",
'293': '🌦', "293": "🌦",
'296': '🌦', "296": "🌦",
'299': '🌧', "299": "🌧",
'302': '🌧', "302": "🌧",
'305': '🌧', "305": "🌧",
'308': '🌧', "308": "🌧",
'311': '🌧', "311": "🌧",
'314': '🌧', "314": "🌧",
'317': '🌧', "317": "🌧",
'320': '🌨', "320": "🌨",
'323': '🌨', "323": "🌨",
'326': '🌨', "326": "🌨",
'329': '❄️', "329": "❄️",
'332': '❄️', "332": "❄️",
'335': '❄️', "335": "❄️",
'338': '❄️', "338": "❄️",
'350': '🌧', "350": "🌧",
'353': '🌦', "353": "🌦",
'356': '🌧', "356": "🌧",
'359': '🌧', "359": "🌧",
'362': '🌧', "362": "🌧",
'365': '🌧', "365": "🌧",
'368': '🌨', "368": "🌨",
'371': '❄️', "371": "❄️",
'374': '🌧', "374": "🌧",
'377': '🌧', "377": "🌧",
'386': '', "386": "",
'389': '🌩', "389": "🌩",
'392': '', "392": "",
'395': '❄️' "395": "❄️",
} }
data = {} data = {}
@ -66,7 +67,7 @@ def format_time(time):
def format_temp(temp): def format_temp(temp):
return (hour['FeelsLikeC']+"°").ljust(3) return (hour["FeelsLikeC"] + "°").ljust(3)
def format_chances(hour): def format_chances(hour):
@ -78,38 +79,47 @@ def format_chances(hour):
"chanceofsnow": "Snow", "chanceofsnow": "Snow",
"chanceofsunshine": "Sunshine", "chanceofsunshine": "Sunshine",
"chanceofthunder": "Thunder", "chanceofthunder": "Thunder",
"chanceofwindy": "Wind" "chanceofwindy": "Wind",
} }
conditions = [] conditions = []
for event in chances.keys(): for event in chances.keys():
if int(hour[event]) > 0: if int(hour[event]) > 0:
conditions.append(chances[event]+" "+hour[event]+"%") conditions.append(chances[event] + " " + hour[event] + "%")
return ", ".join(conditions) return ", ".join(conditions)
data['text'] = WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \ data["text"] = (
" "+weather['current_condition'][0]['FeelsLikeC']+"°C" WEATHER_CODES[weather["current_condition"][0]["weatherCode"]]
+ " "
+ weather["current_condition"][0]["temp_C"]
+ "°C"
)
data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_C']}°C</b>\n" data[
data['tooltip'] += f"Feels like: {weather['current_condition'][0]['FeelsLikeC']}°C\n" "tooltip"
data['tooltip'] += f"Wind: {weather['current_condition'][0]['windspeedKmph']}Km/h\n" ] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_C']}°C</b>\n"
data['tooltip'] += f"Humidity: {weather['current_condition'][0]['humidity']}%\n" data["tooltip"] += f"Feels like: {weather['current_condition'][0]['FeelsLikeC']}°C\n"
for i, day in enumerate(weather['weather']): data["tooltip"] += f"Wind: {weather['current_condition'][0]['windspeedKmph']}Km/h\n"
data['tooltip'] += f"\n<b>" 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: if i == 0:
data['tooltip'] += "Today, " data["tooltip"] += "Today, "
if i == 1: if i == 1:
data['tooltip'] += "Tomorrow, " data["tooltip"] += "Tomorrow, "
data['tooltip'] += f"{day['date']}</b>\n" data["tooltip"] += f"{day['date']}</b>\n"
data['tooltip'] += f"⬆️ {day['maxtempC']}° ⬇️ {day['mintempC']}° " data["tooltip"] += f"⬆️ {day['maxtempC']}° ⬇️ {day['mintempC']}° "
data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n" data[
for hour in day['hourly']: "tooltip"
] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
for hour in day["hourly"]:
if i == 0: if i == 0:
if int(format_time(hour['time'])) < datetime.now().hour-2: if int(format_time(hour["time"])) < datetime.now().hour - 2:
continue continue
data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeC'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n" 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)) print(json.dumps(data))