2022-02-19 23:19:23 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2022-02-07 16:32:13 +00:00
|
|
|
let
|
2022-05-19 17:16:09 +00:00
|
|
|
weather_for_ips = pkgs.writers.writePython3Bin "weather_for_ips" {
|
|
|
|
libraries = [ pkgs.python3Packages.geoip2 ];
|
|
|
|
} ./weather_for_ips.py;
|
|
|
|
|
2022-02-07 16:32:13 +00:00
|
|
|
weather_report = pkgs.writers.writeDashBin "weather_report" ''
|
2022-05-18 14:57:57 +00:00
|
|
|
set -efu
|
|
|
|
export PATH="${lib.makeBinPath [
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.curl
|
2022-05-22 18:51:14 +00:00
|
|
|
pkgs.iproute2
|
|
|
|
pkgs.jc
|
|
|
|
pkgs.jq
|
2022-05-18 14:57:57 +00:00
|
|
|
]}"
|
2022-05-19 17:16:09 +00:00
|
|
|
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
|
2022-05-22 18:51:14 +00:00
|
|
|
ss -no 'sport = :8000' |
|
|
|
|
jc --ss | jq -r '.[] |
|
|
|
|
select(
|
|
|
|
.local_address != "[::ffff:127.0.0.1]"
|
|
|
|
and .local_address != "[::1]"
|
|
|
|
) | .peer_address | gsub("[\\[\\]]"; "")
|
|
|
|
' |
|
2022-05-19 17:16:09 +00:00
|
|
|
${weather_for_ips}/bin/weather_for_ips
|
2022-02-07 16:32:13 +00:00
|
|
|
'';
|
2022-05-19 17:16:09 +00:00
|
|
|
|
2022-02-07 16:32:13 +00:00
|
|
|
send_to_radio = pkgs.writers.writeDashBin "send_to_radio" ''
|
2022-05-29 19:46:58 +00:00
|
|
|
${pkgs.vorbis-tools}/bin/oggenc - |
|
2022-02-07 16:32:13 +00:00
|
|
|
${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live
|
|
|
|
'';
|
|
|
|
|
2022-02-19 23:19:23 +00:00
|
|
|
gc_news = pkgs.writers.writeDashBin "gc_news" ''
|
|
|
|
set -xefu
|
|
|
|
${pkgs.coreutils}/bin/cat $HOME/news | ${pkgs.jq}/bin/jq -cs 'map(select((.to|fromdateiso8601) > now)) | .[]' > $HOME/bla-news.tmp
|
|
|
|
${pkgs.coreutils}/bin/mv $HOME/bla-news.tmp $HOME/news
|
|
|
|
'';
|
|
|
|
|
|
|
|
get_current_news = pkgs.writers.writeDashBin "get_current_news" ''
|
|
|
|
set -xefu
|
|
|
|
${pkgs.coreutils}/bin/cat $HOME/news | ${pkgs.jq}/bin/jq -rs 'map(select(((.to | fromdateiso8601) > now) and (.from|fromdateiso8601) < now) | .text) | .[]'
|
|
|
|
'';
|
|
|
|
|
2022-02-07 16:32:13 +00:00
|
|
|
newsshow = pkgs.writers.writeDashBin "newsshow" /* sh */ ''
|
2022-05-18 14:57:57 +00:00
|
|
|
cat << EOF
|
2022-02-07 16:32:13 +00:00
|
|
|
hello crabpeople!
|
2022-05-18 14:57:57 +00:00
|
|
|
$(${pkgs.ddate}/bin/ddate +'Today is %{%A, the %e of %B%}, %Y. %N%nCelebrate %H')
|
2022-02-07 16:32:13 +00:00
|
|
|
It is $(date --utc +%H) o clock UTC.
|
2022-02-19 23:19:23 +00:00
|
|
|
todays news:
|
|
|
|
$(get_current_news)
|
|
|
|
$(gc_news)
|
2022-05-18 14:57:57 +00:00
|
|
|
$(weather_report)
|
|
|
|
EOF
|
2022-02-07 16:32:13 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
systemd.services.newsshow = {
|
|
|
|
path = [
|
|
|
|
newsshow
|
|
|
|
send_to_radio
|
2022-02-19 23:19:23 +00:00
|
|
|
gc_news
|
|
|
|
get_current_news
|
2022-02-07 16:32:13 +00:00
|
|
|
weather_report
|
|
|
|
pkgs.curl
|
2022-02-08 17:03:58 +00:00
|
|
|
pkgs.retry
|
2022-02-07 16:32:13 +00:00
|
|
|
];
|
|
|
|
script = ''
|
|
|
|
set -efu
|
2022-02-08 17:03:58 +00:00
|
|
|
retry -t 5 -d 10 -- newsshow |
|
2022-02-19 23:19:23 +00:00
|
|
|
retry -t 5 -d 10 -- curl -fSsG http://tts.r/api/tts --data-urlencode 'text@-' |
|
2022-02-08 17:03:58 +00:00
|
|
|
retry -t 5 -d 10 -- send_to_radio
|
2022-02-07 16:32:13 +00:00
|
|
|
'';
|
|
|
|
startAt = "*:00:00";
|
2022-02-19 23:19:23 +00:00
|
|
|
serviceConfig = {
|
|
|
|
User = "radio-news";
|
2022-05-19 17:16:09 +00:00
|
|
|
LoadCredential = [
|
|
|
|
"openweather_api:${toString <secrets>}/openweather_api_key"
|
|
|
|
];
|
2022-02-19 23:19:23 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-05-25 12:59:08 +00:00
|
|
|
services.nginx.virtualHosts."radio-news.r" = {
|
|
|
|
locations."/" = {
|
|
|
|
proxyPass = "http://localhost:7999";
|
|
|
|
proxyWebsockets = true;
|
|
|
|
extraConfig = ''
|
|
|
|
add_header 'Access-Control-Allow-Origin' '*';
|
|
|
|
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2022-02-19 23:19:23 +00:00
|
|
|
krebs.htgen.news = {
|
|
|
|
port = 7999;
|
|
|
|
user = {
|
|
|
|
name = "radio-news";
|
|
|
|
};
|
|
|
|
script = ''. ${pkgs.writers.writeDash "htgen-news" ''
|
|
|
|
set -xefu
|
2022-05-18 14:57:57 +00:00
|
|
|
case "''${Method:-GET} $Request_URI" in
|
|
|
|
"GET /")
|
|
|
|
printf 'HTTP/1.1 200 OK\r\n'
|
|
|
|
printf 'Connection: close\r\n'
|
|
|
|
printf '\r\n'
|
|
|
|
cat "$HOME"/news | jq -sc .
|
|
|
|
exit
|
|
|
|
;;
|
2022-02-19 23:19:23 +00:00
|
|
|
"POST /")
|
2022-05-18 14:57:57 +00:00
|
|
|
payload=$(head -c "$req_content_length")
|
2022-02-19 23:19:23 +00:00
|
|
|
echo "$payload" | jq 'has("from") and has("to") and has("text")' >&2
|
2022-05-18 14:57:57 +00:00
|
|
|
echo "$payload" | jq -c '{ from: (.from | fromdate | todate), to: (.to | fromdate | todate), text: .text }' >> "$HOME"/news
|
2022-02-19 23:19:23 +00:00
|
|
|
printf 'HTTP/1.1 200 OK\r\n'
|
|
|
|
printf 'Connection: close\r\n'
|
|
|
|
printf '\r\n'
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
''}'';
|
2022-02-07 16:32:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
## debug
|
|
|
|
# environment.systemPackages = [
|
|
|
|
# weather_report
|
|
|
|
# send_to_radio
|
|
|
|
# newsshow
|
|
|
|
# ];
|
|
|
|
}
|