about summary refs log tree commit diff
path: root/pkgs/profpatsch/youtube2audiopodcast
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2019-12-08 22:51:48 +0100
committerProfpatsch <mail@profpatsch.de>2019-12-09 01:04:26 +0100
commit2b34b7cb6e0ddfafac5469c1e9fd2bc0bd48c066 (patch)
treecdb6ed138a3da230d5c9b33bf2a3918f8b00a68e /pkgs/profpatsch/youtube2audiopodcast
parent1e1b710c19973b3d3fce7b1cfbbe82a993771629 (diff)
pkgs/profpatsch/youtube2audiopodcast: factor out http serving
Diffstat (limited to 'pkgs/profpatsch/youtube2audiopodcast')
-rw-r--r--pkgs/profpatsch/youtube2audiopodcast/default.nix36
1 files changed, 22 insertions, 14 deletions
diff --git a/pkgs/profpatsch/youtube2audiopodcast/default.nix b/pkgs/profpatsch/youtube2audiopodcast/default.nix
index d4a4993a..5d435476 100644
--- a/pkgs/profpatsch/youtube2audiopodcast/default.nix
+++ b/pkgs/profpatsch/youtube2audiopodcast/default.nix
@@ -45,14 +45,33 @@ let
     "pipeline" [ bins.printf "%s" "$VAR" ] "$@"
   ];
 
+  # serve an opus file as HTTP on stdout
+  serve-http-opus-file =
+    writeExecline "serve-http-opus-file" { readNArgs = 1; } [
+      # determine file size
+      bins.backtick "-i" "-n" "filesize" [
+        bins.redirfd "-r" "0" "$1"
+        bins.wc "--bytes"
+      ]
+      bins.importas "filesize" "filesize"
+      # yolo html
+      bins.${"if"} [ bins.printf ''
+        HTTP/1.1 200 OK
+        Content-Type: audio/ogg
+        Content-Length: %u
+
+      '' "$filesize" ]
+      # the payload is our file
+      bins.redirfd "-r" "0" "$1" bins.cat
+    ];
+
   serve-audio = writeExecline "audio-server" {} [
     (runInEmptyEnv [])
     bins.s6-tcpserver "::1" "8888"
     (sandbox { extraMounts = [ "/etc" ]; })
     yolo-cgi
-    # bins.fdmove "1" "2" bins.env
     bins.${"if"} [
-      # remove leading slash
+      # remove leading slash from youtube-id
       bins.backtick "-i" "yt-video-id" [
         envvar-to-stdin "REQUEST_URI"
         bins.cut "-c2-"
@@ -61,18 +80,7 @@ let
       bins.fdmove "-c" "1" "2"
       youtube-dl-audio "$yt-video-id"
     ]
-    bins.backtick "-i" "-n" "filesize" [
-      bins.redirfd "-r" "0" "./audio.opus"
-      bins.wc "--bytes"
-    ]
-    bins.importas "filesize" "filesize"
-    bins.${"if"} [ bins.printf ''
-      HTTP/1.1 200 OK
-      Content-Type: audio/ogg
-      Content-Length: %u
-
-    '' "$filesize" ]
-    bins.redirfd "-r" "0" "./audio.opus" bins.cat
+    serve-http-opus-file "./audio.opus"
   ];
 
 # in printFeed