From a7833d0d8140077b25c52f2fb0b030b38a323ba6 Mon Sep 17 00:00:00 2001 From: kat witch Date: Fri, 12 Feb 2021 04:20:01 +0000 Subject: [PATCH] Added weather module. --- .gitignore | 3 +- config/profiles/sway/default.nix | 9 +++++- config/profiles/sway/waybar.css.nix | 3 +- scripts/weather.nix | 18 +++++++++++ scripts/weather.py | 48 +++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 scripts/weather.nix create mode 100644 scripts/weather.py diff --git a/.gitignore b/.gitignore index 10341cca..93b655bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /result -/config/hosts/beltane/secrets.nix \ No newline at end of file +/config/hosts/beltane/secrets.nix +/secrets.nix \ No newline at end of file diff --git a/config/profiles/sway/default.nix b/config/profiles/sway/default.nix index 68554269..e40b6ff7 100644 --- a/config/profiles/sway/default.nix +++ b/config/profiles/sway/default.nix @@ -1,6 +1,7 @@ { config, pkgs, lib, ... }: let colors = import ./colors.nix; +secrets = import ../../../secrets.nix; in { config = lib.mkIf (lib.elem "sway" config.meta.deploy.profiles) { fonts.fonts = with pkgs; [ @@ -264,6 +265,7 @@ in { modules-right = [ "pulseaudio" "network" + "custom/weather" "cpu" "memory" "temperature" @@ -273,6 +275,11 @@ in { ]; modules = { + "custom/weather" = { + format = "{}"; + interval = 3600; + exec = "nix-shell --command 'python ${../../../scripts/weather.py} ${secrets.profiles.sway.city} ${secrets.profiles.sway.api_key}' ${../../../scripts/weather.nix}"; + }; cpu = { format = " {usage}%"; }; memory = { format = " {percentage}%"; }; temperature = { format = "﨎 {temperatureC}°C"; }; @@ -303,7 +310,7 @@ in { format-ethernet = " {ifname}: {ipaddr}/{cidr}"; format-linked = " {ifname} (No IP)"; format-disconnected = " Disconnected "; - format-alt = "{ifname}: {ipaddr}/{cidr}"; + format-alt = " {ifname}: {ipaddr}/{cidr}"; }; clock = { format = " {:%A, %F %T %Z}"; }; }; diff --git a/config/profiles/sway/waybar.css.nix b/config/profiles/sway/waybar.css.nix index 9267e1fd..062602b1 100644 --- a/config/profiles/sway/waybar.css.nix +++ b/config/profiles/sway/waybar.css.nix @@ -9,7 +9,7 @@ min-height: 14px } - #clock, #memory, #cpu, #temperature, #pulseaudio, #network, #mpd, #backlight, #battery { + #clock, #memory, #cpu, #temperature, #pulseaudio, #network, #mpd, #backlight, #battery, #custom-weather { margin-left: 8px; margin-right: 8px; padding-left: 8px; @@ -57,6 +57,7 @@ #mpd { border-color: #5af78e } #mpd.disconnected, #mpd.stopped { border-color: #282a36 } #network { border-color: ${colors.base16.color3} } + #custom-weather { border-color: ${colors.base16.color15} } #pulseaudio { border-color: ${colors.base16.color2} } #temperature { border-color: ${colors.base16.color4} } #battery { border-color: ${colors.base16.color6} } diff --git a/scripts/weather.nix b/scripts/weather.nix new file mode 100644 index 00000000..01ee3c61 --- /dev/null +++ b/scripts/weather.nix @@ -0,0 +1,18 @@ +{ pkgs ? import {} }: + +with pkgs; + +mkShell { + buildInputs = [ + pkgs.python3 + pkgs.python3.pkgs.requests + ]; + shellHook = '' + # Tells pip to put packages into $PIP_PREFIX instead of the usual locations. + # See https://pip.pypa.io/en/stable/user_guide/#environment-variables. + export PIP_PREFIX=$(pwd)/_build/pip_packages + export PYTHONPATH="$PIP_PREFIX/${pkgs.python3.sitePackages}:$PYTHONPATH" + export PATH="$PIP_PREFIX/bin:$PATH" + unset SOURCE_DATE_EPOCH + ''; +} diff --git a/scripts/weather.py b/scripts/weather.py new file mode 100644 index 00000000..e6a477ad --- /dev/null +++ b/scripts/weather.py @@ -0,0 +1,48 @@ +import requests +import json +import sys + +#city = "London" +#api_key = "08588263e133e79c17cef6d01a5c6da8" +city = sys.argv[1] +api_key = sys.argv[2] + +url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric" + +weather_icons = { + "Thunderstorm": "摒", + "Clouds": "摒", + "Drizzle": "", + "Rain": "", + "Snow": "流", + "Clear": "滛", + "Mist": "", + "Smoke": "", + "Haze": "", + "Dust": "", + "Fog": "", + "Sand": "", + "Ash": "", + "Squall": "", + "Tornado": "" +} + +def degrees_to_cardinal(d): + dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", + "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] + ix = int((d + 11.25)/22.5) + return dirs[ix % 16] + +response = requests.get(url) +data = json.loads(response.text) +#print(data) + +condition = data["weather"][0]["main"] +weather_icon = weather_icons[condition] +temperature = f"{data['main']['temp']}°C" +feels_like_temperature = f"{data['main']['feels_like']}°C" +humidity = f"{data['main']['humidity']}%" +wind = f"{data['wind']['speed']}m/s {degrees_to_cardinal(data['wind']['deg'])}" + +end_string = f"{weather_icon} {temperature} {feels_like_temperature} {humidity} {wind}" +print(end_string)