mirror of
https://github.com/roles-ansible/ansible_role_sway.git
synced 2024-08-16 13:49:49 +02:00
203 lines
5.9 KiB
Django/Jinja
203 lines
5.9 KiB
Django/Jinja
#!/usr/bin/env python
|
||
"""
|
||
wttr.in waybar widget
|
||
{{ ansible_managed }}
|
||
"""
|
||
import json
|
||
import sys
|
||
from datetime import datetime
|
||
import requests
|
||
import time
|
||
|
||
localization = {
|
||
"en": {
|
||
"feels_like": "Feels like",
|
||
"wind": "Wind",
|
||
"humidity": "Humidity",
|
||
"today": "Today",
|
||
"tomorrow": "Tomorrow",
|
||
"weatherDesc": "weatherDesc",
|
||
"chanceoffog": "Fog",
|
||
"chanceoffrost": "Frost",
|
||
"chanceofovercast": "Overcast",
|
||
"chanceofrain": "Rain",
|
||
"chanceofsnow": "Snow",
|
||
"chanceofsunshine": "Sunshine",
|
||
"chanceofthunder": "Thunder",
|
||
"chanceofwindy": "Wind",
|
||
"weatherfor": "Weather for"
|
||
},
|
||
"de": {
|
||
"feels_like": "Gefühlt wie",
|
||
"wind": "Wind",
|
||
"humidity": "Luftfeuchtigkeit",
|
||
"today": "Heute",
|
||
"tomorrow": "Morgen",
|
||
"weatherDesc": "lang_de",
|
||
"chanceoffog": "Nebel",
|
||
"chanceoffrost": "Frost",
|
||
"chanceofovercast": "Bewölkt",
|
||
"chanceofrain": "Regen",
|
||
"chanceofsnow": "Schnee",
|
||
"chanceofsunshine": "Sonne",
|
||
"chanceofthunder": "Gewitter",
|
||
"chanceofwindy": "Wind",
|
||
"weatherfor": "Wetter für"
|
||
},
|
||
"el": {
|
||
"feels_like": "Πραγματική αίσθηση",
|
||
"wind": "Άνεμος",
|
||
"humidity": "Υγρασία",
|
||
"today": "Σήμερα",
|
||
"tomorrow": "Αύριο",
|
||
"day_after_tomorrow": "Μεθαύριο",
|
||
"weatherDesc": "lang_el",
|
||
"chanceoffog": "Ομίχλη",
|
||
"chanceoffrost": "Παγετός",
|
||
"chanceofovercast": "Πυκνή νέφωση",
|
||
"chanceofrain": "Βροχή",
|
||
"chanceofsnow": "Χιόνι",
|
||
"chanceofsunshine": "Λιακάδα",
|
||
"chanceofthunder": "Κεραυνοί",
|
||
"chanceofwindy": "Άνεμος",
|
||
"weatherfor": "Καιρός για"
|
||
}
|
||
}
|
||
|
||
LANG = "de"
|
||
text = localization[LANG]
|
||
|
||
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 = {}
|
||
|
||
for i in range(23):
|
||
try:
|
||
weather_raw = requests.get(f"https://{LANG}.wttr.in/?format=j1", timeout=42)
|
||
if weather_raw.status_code == 200:
|
||
weather = weather_raw.json()
|
||
break
|
||
except requests.ConnectionError:
|
||
if i == 22:
|
||
sys.exit()
|
||
time.sleep(1*i*i+1)
|
||
|
||
def format_time(time):
|
||
"""
|
||
Make sure times get properly formated...
|
||
"""
|
||
return time.replace("00", "").zfill(2)
|
||
|
||
|
||
def format_temp(temp):
|
||
"""
|
||
Make sure temperature get properly formated...
|
||
"""
|
||
return (f"{temp}°").ljust(3)
|
||
|
||
def format_chances(format_hour):
|
||
"""
|
||
Make sure weather wil be properly ranslated
|
||
"""
|
||
chances = [
|
||
"chanceoffog",
|
||
"chanceoffrost",
|
||
"chanceofovercast",
|
||
"chanceofrain",
|
||
"chanceofsnow",
|
||
"chanceofsunshine",
|
||
"chanceofthunder",
|
||
"chanceofwindy"
|
||
]
|
||
|
||
probs = {text[e]: int(prb) for e, prb in format_hour.items() if e in chances and int(prb) > 0}
|
||
sorted_probs = {e: probs[e] for e in sorted(probs, key=probs.get, reverse=True)}
|
||
conditions = [f"{event} {prb}%" for event, prb in sorted_probs.items()]
|
||
return ", ".join(conditions)
|
||
|
||
data['text'] = f"{weather['nearest_area'][0]['areaName'][0]['value']}: "
|
||
data['text'] += f"{WEATHER_CODES[weather['current_condition'][0]['weatherCode']]} "
|
||
data['text'] += f"{weather['current_condition'][0]['FeelsLikeC']}°C"
|
||
|
||
WEATHER_DESC = text['weatherDesc']
|
||
data['tooltip'] = f"<b>{text['weatherfor']} "
|
||
data['tooltip'] += f"{weather['nearest_area'][0]['areaName'][0]['value']}, "
|
||
data['tooltip'] += f"{weather['nearest_area'][0]['country'][0]['value']}:</b>\n\n"
|
||
data['tooltip'] += f"<b>{weather['current_condition'][0][WEATHER_DESC][0]['value']}"
|
||
data['tooltip'] += f"{weather['current_condition'][0]['temp_C']}°</b>\n"
|
||
data['tooltip'] += f"{text['feels_like']}: "
|
||
data['tooltip'] += f"{weather['current_condition'][0]['FeelsLikeC']}°\n"
|
||
data['tooltip'] += f"{text['wind']}: "
|
||
data['tooltip'] += f"{weather['current_condition'][0]['windspeedKmph']}Km/h\n"
|
||
data['tooltip'] += f"{text['humidity']}: {weather['current_condition'][0]['humidity']}%\n"
|
||
for i, day in enumerate(weather['weather']):
|
||
data['tooltip'] += "\n<b>"
|
||
if i == 0:
|
||
data['tooltip'] += f"{text['today']}, "
|
||
if i == 1:
|
||
data['tooltip'] += f"{text['tomorrow']}, "
|
||
if i == 2 and 'day_after_tomorrow' in text:
|
||
data['tooltip'] += f"{text['day_after_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'])} "
|
||
data['tooltip'] += f"{WEATHER_CODES[hour['weatherCode']]} "
|
||
data['tooltip'] += f"{format_temp(hour['FeelsLikeC'])} "
|
||
data['tooltip'] += f"{hour[WEATHER_DESC][0]['value']}, {format_chances(hour)}\n"
|
||
|
||
|
||
print(json.dumps(data))
|