1
0
Fork 0

update waybar weather script

This commit is contained in:
Peter Domínguez 2025-06-05 13:34:36 -04:00
parent 2b59f0f44d
commit 3e0bcb3bef
2 changed files with 52 additions and 38 deletions

View file

@ -1,9 +1,26 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import subprocess
from pyquery import PyQuery # install using `pip install pyquery`
import json
################################### CONFIGURATION ###################################
# set your location_id
# to get your location_id, go to https://weather.com & search for your location.
# once you choose your location, you can see the location_id in the URL(64 chars long hex string)
# like this: https://weather.com/en-IN/weather/today/l/c3e96d6cc4965fc54f88296b54449571c4107c73b9638c16aafc83575b4ddf2e
# once you get the location_id, you can replace the below location_id with your own location_id
location_id = "e42bb25a58c2e689ec85e632d82e69d7e46c82defb9ad7a5551b6c3a70fbc282" # TODO
# celcius or fahrenheit
unit = "metric" # metric or imperial
# forcase type
forecast_type = "Daily" # Hourly or Daily
########################################## MAIN ##################################
# weather icons
weather_icons = {
"sunnyDay": "󰖨",
@ -18,33 +35,23 @@ weather_icons = {
"default": "",
}
# get location_id
# to get your own location_id, go to https://weather.com & search your location.
# once you choose your location, you can see the location_id in the URL(64 chars long hex string)
# like this: https://weather.com/en-IN/weather/today/l/c3e96d6cc4965fc54f88296b54449571c4107c73b9638c16aafc83575b4ddf2e
location_id = "e42bb25a58c2e689ec85e632d82e69d7e46c82defb9ad7a5551b6c3a70fbc282" # TODO
# location_id = "8139363e05edb302e2d8be35101e400084eadcecdfce5507e77d832ac0fa57ae"
# priv_env_cmd = 'cat $PRIV_ENV_FILE | grep weather_location | cut -d "=" -f 2'
# location_id = subprocess.run(
# priv_env_cmd, shell=True, capture_output=True).stdout.decode('utf8').strip()
# get html page
url_fetch = "https://weather.com/en-IN/weather/today/l/" + location_id
html_data = PyQuery(url=url_fetch)
_l = "en-IN" if unit == "metric" else "en-US"
url = f"https://weather.com/{_l}/weather/today/l/{location_id}"
# get html data
html_data = PyQuery(url=url)
# current temperature
temp = html_data("span[data-testid='TemperatureValue']").eq(0).text()
# print(temp)
# current status phrase
status = html_data("div[data-testid='wxPhrase']").text()
status = f"{status[:16]}.." if len(status) > 17 else status
# print(status)
# status code
status_code = html_data("#regionHeader").attr("class").split(" ")[2].split("-")[2]
# print(status_code)
status_code_class = html_data("#regionHeader").attr("class")
status_code = str(status_code_class).split(" ")[2].split("-")[2]
# status icon
icon = (
@ -52,59 +59,64 @@ icon = (
if status_code in weather_icons
else weather_icons["default"]
)
# print(icon)
# temperature feels like
temp_feel = html_data(
"div[data-testid='FeelsLikeSection'] > span > span[data-testid='TemperatureValue']"
).text()
temp_feel_text = f"Feels like {temp_feel}C"
# print(temp_feel_text)
temp_feel_text = f"Feels like {temp_feel}{'c' if unit == 'metric' else 'f'}"
# min-max temperature
temp_min = (
html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
.eq(0)
.eq(1)
.text()
)
temp_max = (
html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
.eq(1)
.eq(0)
.text()
)
temp_min_max = f"{temp_min}\t\t{temp_max}"
# print(temp_min_max)
# wind speed
wind_speed = str(html_data("span[data-testid='Wind']").text())
wind_text = f"{wind_speed}"
# print(wind_text)
wind_text = f"{wind_speed}"
# humidity
humidity = html_data("span[data-testid='PercentageValue']").text()
humidity_text = f"{humidity}"
# print(humidity_text)
# visibility
visbility = html_data("span[data-testid='VisibilityValue']").text()
visbility_text = f"{visbility}"
# print(visbility_text)
# air quality index
air_quality_index = html_data("text[data-testid='DonutChartValue']").text()
# print(air_quality_index)
# hourly rain prediction
prediction = html_data("section[aria-label='Hourly Forecast']")(
# rain prediction
r_prediction_text = html_data(f"section[aria-label='{forecast_type} Forecast']")(
"div[data-testid='SegmentPrecipPercentage'] > span"
).text()
prediction = prediction.replace("Chance of Rain", "")
prediction = f"\n\n  (hourly) {prediction}" if len(prediction) > 0 else prediction
# print(prediction)
r_prediction = str(r_prediction_text).replace("Chance of Rain", "")
r_prediction = f"  ({forecast_type}) {r_prediction}" if len(r_prediction) > 0 else r_prediction
# temperature prediction
t_prediction_text = html_data(f"section[aria-label='{forecast_type} Forecast']")(
"div[data-testid='SegmentHighTemp'] > span"
).text()
t_prediction = str(t_prediction_text).replace(" /", "/")
t_prediction = f" ({forecast_type}) {t_prediction}" if len(t_prediction) > 0 else t_prediction
#pretty print all data
# print(f"temp: {temp}\nstatus: {status}\nstatus_code: {status_code}\nicon: {icon}\
# \ntemp_feel_text: {temp_feel_text}\ntemp_min_max: {temp_min_max}\nwind_text: {wind_text}\
# \nhumidity_text: {humidity_text}\nvisbility_text: {visbility_text}\nair_quality_index: {air_quality_index}\
# \nprediction: \n{r_prediction}\n{t_prediction}")
# tooltip text
tooltip_text = str.format(
"\t\t{}\t\t\n{}\n{}\n{}\n\n{}\n{}\n{}{}",
"\t\t{}\t\t\n{}\n{}\n{}\n\n{}\n{}\n{}\n\n{}\n{}",
f'<span size="xx-large">{temp}C 🇩🇴</span>',
f"<big>{icon}</big>",
f"<big>{status}</big>",
@ -112,7 +124,8 @@ tooltip_text = str.format(
f"<big>{temp_min_max}</big>",
f"{wind_text}\t{humidity_text}",
f"{visbility_text}\tAQI {air_quality_index}",
f"<i>{prediction}</i>",
f"<i>{r_prediction}</i>",
f"<i>{t_prediction}</i>"
)
# print waybar module data
@ -123,3 +136,4 @@ out_data = {
"class": status_code,
}
print(json.dumps(out_data))

View file

@ -11,7 +11,7 @@ selecttag-format "  %T (%n)"
feedlist-format "  (%U) %t"
articlelist-format "  %D %6L %?T?|%-17T| ?%t"
urls-source "freshrss"
freshrss-url "https://rss.peterdominguez.art/api/greader.php"
freshrss-url "https://rss.nvim.pro/api/greader.php"
freshrss-login "buggerman"
freshrss-passwordfile "/home/peter/.secrets/newsboat"
freshrss-flag-star "s"