summaryrefslogtreecommitdiffstats
path: root/lass/2configs/services/radio/weather.nix
blob: dca8a7843d9d7a12bb6a0fb5db2840a407da32d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{ config, lib, pkgs, ... }:
let
  weather_for_ips = pkgs.writers.writePython3Bin "weather_for_ips" {
    libraries = [ pkgs.python3Packages.geoip2 ];
    flakeIgnore = [ "E501" ];
  } ./weather_for_ips.py;

  weather_report = pkgs.writers.writeDashBin "weather_report" ''
    set -efux
    export PATH="${lib.makeBinPath [
      pkgs.coreutils
      pkgs.curl
      pkgs.jq
    ]}"
    curl -fSsz /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
    (
      curl -sS 'http://admin:hackme@localhost:8000/admin/listclients.json?mount=/radio.ogg'
      curl -sS 'http://admin:hackme@localhost:8000/admin/listclients.json?mount=/radio.mp3'
      curl -sS 'http://admin:hackme@localhost:8000/admin/listclients.json?mount=/radio.opus'
    ) | jq -rs '
      [
        .[][].source|values|to_entries[].value |
        (.listener//[]) [] |
        (.useragent | capture("client-ip=(?<ip>[a-f0-9.:]+)")).ip // .ip
      ] |
        unique[] |
        select(. != "127.0.0.1") |
        select(. != "::1")
    ' |
      ${weather_for_ips}/bin/weather_for_ips
  '';
in {
  systemd.services.weather = {
    path = [
      weather_report
      pkgs.retry
      pkgs.jq
      pkgs.curl
    ];
    script = ''
      set -xefu
      retry -t 5 -d 10 -- weather_report |
        jq \
          --arg from "$(date -u +'%FT%TZ')" \
          --arg to "$(date -u +'%FT%TZ' -d '+1 hours')" \
          --slurp --raw-input --compact-output --ascii-output \
          '{text: ., from: $from, to: $to, priority: 100}' |
        retry -t 5 -d 10 -- curl -fSs -d@- http://radio-news.r
    '';
    startAt = "*:58:00";
    serviceConfig = {
      User = "radio-news";
      LoadCredential = [
        "openweather_api:${toString <secrets>}/openweather_api_key"
      ];
    };
  };
}