summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lass/2configs/radio/news.nix25
-rw-r--r--lass/2configs/radio/weather_for_ips.py33
2 files changed, 46 insertions, 12 deletions
diff --git a/lass/2configs/radio/news.nix b/lass/2configs/radio/news.nix
index 4f88197cb..6d73f4bb7 100644
--- a/lass/2configs/radio/news.nix
+++ b/lass/2configs/radio/news.nix
@@ -1,29 +1,27 @@
{ config, lib, pkgs, ... }:
let
+ weather_for_ips = pkgs.writers.writePython3Bin "weather_for_ips" {
+ libraries = [ pkgs.python3Packages.geoip2 ];
+ } ./weather_for_ips.py;
+
weather_report = pkgs.writers.writeDashBin "weather_report" ''
set -efu
export PATH="${lib.makeBinPath [
pkgs.iproute2
pkgs.coreutils
- pkgs.jq
pkgs.curl
pkgs.gnugrep
pkgs.gnused
]}"
+ curl -z /tmp/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb http://c.r/GeoLite2-City.mmdb
+ MAXMIND_GEOIP_DB="/tmp/GeoLite2-City.mmdb"; export MAXMIND_GEOIP_DB
+ OPENWEATHER_API_KEY=$(cat "$CREDENTIALS_DIRECTORY/openweather_api"); export OPENWEATHER_API_KEY
ss -Hno state established 'sport = :8000' |
grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' |
- sed '/127.0.0.1/d;/:/d' |
- while read -r ip; do
- curl -sSL "https://wttr.in/@$ip?format=j1"
- done | jq -rs 'unique_by(.nearest_area[0].areaName[0].value) |
- map((.nearest_area[0] |
- "Weather report for \(.areaName[0].value), \(.country[0].value).")
- + (.current_condition[0] |
- " Currently it is \(.weatherDesc[0].value) outside with a temperature of \(.temp_C) degrees."
- )
- ) | unique | .[]'
- '
+ sed '/127.0.0.1/d;/::1/d' |
+ ${weather_for_ips}/bin/weather_for_ips
'';
+
send_to_radio = pkgs.writers.writeDashBin "send_to_radio" ''
${pkgs.vorbisTools}/bin/oggenc - |
${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live
@@ -72,6 +70,9 @@ in
startAt = "*:00:00";
serviceConfig = {
User = "radio-news";
+ LoadCredential = [
+ "openweather_api:${toString <secrets>}/openweather_api_key"
+ ];
};
};
diff --git a/lass/2configs/radio/weather_for_ips.py b/lass/2configs/radio/weather_for_ips.py
new file mode 100644
index 000000000..8d9a2e7bc
--- /dev/null
+++ b/lass/2configs/radio/weather_for_ips.py
@@ -0,0 +1,33 @@
+import geoip2.database
+import fileinput
+import json
+import requests
+import os
+
+
+geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB'])
+seen = {}
+output = []
+for ip in fileinput.input():
+ location = geoip.city(ip.strip())
+ if location.city.geoname_id not in seen:
+ seen[location.city.geoname_id] = True
+ weather_api_key = os.environ['OPENWEATHER_API_KEY']
+ url = (
+ f'https://api.openweathermap.org/data/2.5/onecall'
+ f'?lat={location.location.latitude}'
+ f'&lon={location.location.longitude}'
+ f'&appid={weather_api_key}'
+ f'&units=metric'
+ )
+ resp = requests.get(url)
+ weather = json.loads(resp.text)['current']
+ output.append(
+ f'Weather report for {location.city.name}'
+ f', {location.country.name}. '
+ f'Currently it is {weather["weather"][0]["description"]} outside '
+ f'with a temperature of {weather["temp"]} degrees '
+ f'and a windspeed of {weather["wind_speed"]} meter per second. '
+ )
+
+print('\n'.join(output))