aboutsummaryrefslogtreecommitdiff
path: root/modules/rss/utils.nix
blob: 63d09dff666935e46cba9327a06e2b3027947241 (plain) (blame)
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
{ pkgs, lib, ... }:
with lib;
let
  outlineItem = feed:
    ''<outline title="${feed.title}" text="${feed.title}" xmlUrl="${feed.url}" />'';

  outlineGroup = group: items:
    ''<outline title="${group}" text="${group}">
      ${concatStringsSep "\n" (map outlineItem items)}
    </outline>'';

  toOpml = cfg: ''
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.1">
  <head>
    <title>
      ${cfg.title}
    </title>
  </head>
  <body>
    ${concatStringsSep "\n" (mapAttrsToList outlineGroup cfg.groups)}
  </body>
</opml>'';

  createYarrSetupScript = { username, api_key, api_url, feeds, settings }:
    let
      api = method: path: args:
        ''curl -i -X${method} '${api_url}${path}' --cookie 'auth=${username}:${api_key}' ${args}'';
      opmlFile = pkgs.writeText "rss.opml" (toOpml feeds);
    in
    pkgs.writeShellScriptBin "yarr-setup-script" ''
      #!/usr/bin/env sh

      ${api "POST" "/opml/import" "-F 'opml=@${opmlFile}' -F 'replace=true'"} && echo "Imported feeds";
      ${api "PUT" "/api/settings" "--data '${builtins.toJSON settings}'"} && echo "Applied settings";
    '';
in {
  inherit toOpml createYarrSetupScript;
}