Skip to content

Commit

Permalink
weather-python-script: init
Browse files Browse the repository at this point in the history
needed for Hyprland-Dots JohnRTitor/Hyprland-Dots@9f47376
  • Loading branch information
JohnRTitor committed Aug 25, 2024
1 parent c51d90f commit 65aa525
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 12 deletions.
1 change: 1 addition & 0 deletions flake/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ in {
formatter = pkgs.alejandra;
packages = {
fhs-shell = pkgs.callPackage ../pkgs/fhs-shell.nix {};
weather-python-script = pkgs.callPackage ../pkgs/weather-python-script.nix {};
};
};
}
153 changes: 153 additions & 0 deletions pkgs/weather-python-script.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
writeScriptBin,
python3,
locationId ? "ed1703727a892f7858b986a1be4bba78f3249e1eb96813a01022ecee01e664d6",
...
}: let
python-packages = python3.withPackages (
ps:
with ps; [
requests
sh # subprocess module
pyquery # needed for hyprland-dots Weather script
]
);
in
writeScriptBin "weather-python-script" ''
#!${python-packages.interpreter}
import subprocess
from pyquery import PyQuery # install using `pip install pyquery`
import json
import os
# original code https://gist.github.com/Surendrajat/ff3876fd2166dd86fb71180f4e9342d7
# weather icons
weather_icons = {
"sunnyDay": "󰖙",
"clearNight": "󰖔",
"cloudyFoggyDay": "",
"cloudyFoggyNight": "",
"rainyDay": "",
"rainyNight": "",
"snowyIcyDay": "",
"snowyIcyNight": "",
"severe": "",
"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 = "${locationId}" # TODO
# NOTE to change to deg F, change the URL to your preffered location after weather.com
# Default is English-Philippines with Busan, South Korea as location_id
# get html page
url = "https://weather.com/en-PH/weather/today/l/" + location_id
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 icon
icon = (
weather_icons[status_code]
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)
# min-max temperature
temp_min = (
html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
.eq(1)
.text()
)
temp_max = (
html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
.eq(0)
.text()
)
temp_min_max = f" {temp_min}\t\t {temp_max}"
# print(temp_min_max)
# wind speed
wind_speed = html_data("span[data-testid='Wind']").text().split("\n")[1]
wind_text = f" {wind_speed}"
# print(wind_text)
# 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']")(
"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)
# tooltip text
tooltip_text = str.format(
"\t\t{}\t\t\n{}\n{}\n{}\n\n{}\n{}\n{}{}",
f'<span size="xx-large">{temp}</span>',
f"<big> {icon}</big>",
f"<b>{status}</b>",
f"<small>{temp_feel_text}</small>",
f"<b>{temp_min_max}</b>",
f"{wind_text}\t{humidity_text}",
f"{visbility_text}\tAQI {air_quality_index}",
f"<i> {prediction}</i>",
)
# print waybar module data
out_data = {
"text": f"{icon} {temp}",
"alt": status,
"tooltip": tooltip_text,
"class": status_code,
}
print(json.dumps(out_data))
simple_weather =f"{icon} {status}\n" + \
f" {temp} ({temp_feel_text})\n" + \
f"{wind_text} \n" + \
f"{humidity_text} \n" + \
f"{visbility_text} AQI{air_quality_index}\n"
try:
with open(os.path.expanduser("$HOME/.cache/.weather_cache"), "w") as file:
file.write(simple_weather)
except:
pass
''
17 changes: 5 additions & 12 deletions system/hyprland/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@
# this config file contains package, portal and services declaration
# made specifically for hyprland
{
self,
config,
lib,
pkgs,
pkgs-edge,
inputs,
...
}: let
hyprlandFlake = false;
hyprlandFlake = true;
pkgs-hyprland =
if hyprlandFlake
then inputs.hyprland.packages.${pkgs.system}
else pkgs-edge;
python-packages = pkgs.python3.withPackages (
ps:
with ps; [
requests
sh # subprocess module
pyquery # needed for hyprland-dots Weather script
]
);
in {
imports = [
./session.nix
Expand All @@ -32,14 +25,14 @@ in {
enable = true;
package =
(pkgs-hyprland.hyprland.override {
stdenv = pkgs.clangStdenv;
#stdenv = pkgs.clangStdenv;
})
.overrideAttrs
(prevAttrs: {
patches =
(prevAttrs.patches or [])
++ [
./enable-lto.patch
#./enable-lto.patch
./add-env-vars-to-export.patch
];
});
Expand Down Expand Up @@ -149,7 +142,7 @@ in {
ags # widgets pipup
])
++ [
python-packages # needed for Weather.py from dotfiles
self.packages.${pkgs.system}.weather-python-script # weather script
# inputs.hyprcursor.packages.${pkgs.system}.hyprcursor
# inputs.pyprland.packages.${pkgs.system}.pyprland
# inputs.ags.packages.${pkgs.system}.ags
Expand Down

0 comments on commit 65aa525

Please sign in to comment.