summaryrefslogtreecommitdiffstats
path: root/lass/2configs/services/radio/news.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2023-03-02 09:20:37 +0100
committertv <tv@krebsco.de>2023-03-02 09:20:37 +0100
commit03a9448a0922fcf158c4357922bed689245105e3 (patch)
tree7dbedf90d0443bd1e375aac61f63735f43e64984 /lass/2configs/services/radio/news.nix
parent177fd1eeec05f0821f1ccc63733b3e0fd5aed7b6 (diff)
parent8639d428c2e9f2190ec4e4b5dd931f24a4166f36 (diff)
Merge remote-tracking branch 'prism/master' into head
Diffstat (limited to 'lass/2configs/services/radio/news.nix')
-rw-r--r--lass/2configs/services/radio/news.nix106
1 files changed, 106 insertions, 0 deletions
diff --git a/lass/2configs/services/radio/news.nix b/lass/2configs/services/radio/news.nix
new file mode 100644
index 000000000..0dc711e6c
--- /dev/null
+++ b/lass/2configs/services/radio/news.nix
@@ -0,0 +1,106 @@
+{ config, lib, pkgs, ... }:
+let
+
+ send_to_radio = pkgs.writers.writeDashBin "send_to_radio" ''
+ ${pkgs.vorbis-tools}/bin/oggenc - |
+ ${pkgs.cyberlocker-tools}/bin/cput news.ogg
+ ${pkgs.curl}/bin/curl -fSs -X POST http://localhost:8002/newsshow
+ '';
+
+ gc_news = pkgs.writers.writeDashBin "gc_news" ''
+ set -xefu
+ export TZ=UTC #workaround for jq parsing wrong timestamp
+ ${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
+ export TZ=UTC #workaround for jq parsing wrong timestamp
+ ${pkgs.coreutils}/bin/cat $HOME/news | ${pkgs.jq}/bin/jq -rs '
+ sort_by(.priority) |
+ map(select(
+ ((.to | fromdateiso8601) > now) and
+ (.from|fromdateiso8601) < now) |
+ .text
+ ) | .[]'
+ '';
+
+ newsshow = pkgs.writers.writeDashBin "newsshow" /* sh */ ''
+ cat << EOF
+ hello crabpeople!
+ $(${pkgs.ddate}/bin/ddate +'Today is %{%A, the %e of %B%}, %Y. %N%nCelebrate %H')
+ It is $(date --utc +%H) o clock UTC.
+ todays news:
+ $(get_current_news)
+ $(gc_news)
+ EOF
+ '';
+in
+{
+ systemd.services.newsshow = {
+ path = [
+ newsshow
+ send_to_radio
+ gc_news
+ get_current_news
+ pkgs.curl
+ pkgs.retry
+ ];
+ script = ''
+ set -efu
+ retry -t 5 -d 10 -- newsshow |
+ retry -t 5 -d 10 -- curl -fSsG http://tts.r/api/tts --data-urlencode 'text@-' |
+ retry -t 5 -d 10 -- send_to_radio
+ '';
+ startAt = "*:00:00";
+ serviceConfig = {
+ User = "radio-news";
+ };
+ };
+
+ 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';
+ '';
+ };
+ };
+ krebs.htgen.news = {
+ port = 7999;
+ user = {
+ name = "radio-news";
+ };
+ script = ''. ${pkgs.writers.writeDash "htgen-news" ''
+ set -xefu
+ 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
+ ;;
+ "POST /")
+ payload=$(head -c "$req_content_length")
+ printf '%s' "$payload" | jq 'has("from") and has("to") and has("text")' >&2
+ printf '%s' "$payload" | jq -c '{ from: .from, to: .to, text: .text, priority: (.priority // 0)}' >> "$HOME"/news
+ printf 'HTTP/1.1 200 OK\r\n'
+ printf 'Connection: close\r\n'
+ printf '\r\n'
+ exit
+ ;;
+ esac
+ ''}'';
+ };
+
+ ## debug
+ # environment.systemPackages = [
+ # weather_report
+ # send_to_radio
+ # newsshow
+ # ];
+}