about summary refs log tree commit diff
path: root/pkgs/profpatsch/youtube2audiopodcast
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2019-12-08 00:02:22 +0100
committerProfpatsch <mail@profpatsch.de>2019-12-08 00:04:59 +0100
commitd9e44704eb558af337ca61723bcd07da247cae1a (patch)
treec2d426a7c91908a8b033444dadd918876c76280a /pkgs/profpatsch/youtube2audiopodcast
parent82c5222422b01c440d987918a266373c07511c66 (diff)
pkgs.profpatsch: init youtube2audiopodcast
Minimal PoC of a small application which can download and convert a
youtube video with youtube-dl and then serve it via HTTP.
Diffstat (limited to 'pkgs/profpatsch/youtube2audiopodcast')
-rw-r--r--pkgs/profpatsch/youtube2audiopodcast/default.nix68
1 files changed, 68 insertions, 0 deletions
diff --git a/pkgs/profpatsch/youtube2audiopodcast/default.nix b/pkgs/profpatsch/youtube2audiopodcast/default.nix
new file mode 100644
index 00000000..1d8c80e6
--- /dev/null
+++ b/pkgs/profpatsch/youtube2audiopodcast/default.nix
@@ -0,0 +1,68 @@
+{ pkgs, lib, writeExecline, getBins, runInEmptyEnv }:
+
+let
+  bins = getBins pkgs.hello [ "hello" ]
+    // getBins pkgs.coreutils [ "echo" "env" "cat" "printf" "wc" "tr" ]
+    // getBins pkgs.youtube-dl [ "youtube-dl" ]
+    // getBins pkgs.s6-networking [ "s6-tcpserver" ]
+    // getBins pkgs.execline [ "fdmove" "backtick" "importas" "if" "redirfd" "pipeline" ];
+
+  youtube-dl-audio = writeExecline "abc" { readNArgs = 1; } [
+    bins.youtube-dl
+      "--extract-audio"
+      "--audio-format" "opus"
+      "--output" "\${1}/audio.opus" "https://www.youtube.com/watch?v=5Opw5oR2LDY"
+  ];
+
+  # minimal CGI request parser for use as UCSPI middleware
+  yolo-cgi = pkgs.writers.writePython3 "yolo-cgi" {} ''
+    import sys
+    import os
+
+
+    def parse_ass(bool):
+        if not bool:
+            sys.exit(1)
+
+
+    inbuf = sys.stdin.buffer
+
+    first_line = inbuf.readline().split(sep=b" ")
+    parse_ass(len(first_line) == 3)
+    parse_ass(first_line[2].startswith(b"HTTP/"))
+
+    os.environb[b"REQUEST_METHOD"] = first_line[0]
+    os.environb[b"REQUEST_URI"] = first_line[1]
+
+    cmd = sys.argv[1]
+    args = sys.argv[2:] if len(sys.argv) > 2 else []
+    os.execlp(cmd, cmd, *args)
+  '';
+
+
+  serve-audio = writeExecline "audio-server" {} [
+    (runInEmptyEnv [])
+    bins.s6-tcpserver "::1" "8888"
+    yolo-cgi
+    # bins.fdmove "1" "2" bins.env
+    bins.${"if"} [
+      bins.fdmove "1" "2"
+      youtube-dl-audio "tmpdir"
+    ]
+    bins.backtick "-i" "-n" "filesize" [
+      bins.redirfd "-r" "0" "tmpdir/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" "tmpdir/audio.opus" bins.cat
+  ];
+
+# in printFeed
+in serve-audio
+# in youtube-dl-audio