blob: ed8ad9675cf480cde27b97fb395497e12e4df2aa (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
{ pkgs, lib, writeExecline, getBins, runInEmptyEnv, sandbox }:
let
bins = getBins pkgs.hello [ "hello" ]
// getBins pkgs.coreutils [ "printf" "wc" "tr" "cut" "mktemp" ]
// getBins pkgs.youtube-dl [ "youtube-dl" ]
// getBins pkgs.s6-networking [ "s6-tcpserver" ]
// getBins pkgs.execline [ "fdmove" "backtick" "importas" "if" "redirfd" "pipeline" ]
// getBins pkgs.s6-portable-utils [
{ use = "s6-cat"; as = "cat"; }
];
youtube-dl-audio = writeExecline "youtube-dl-audio" { readNArgs = 1; } [
bins.youtube-dl
"--verbose"
"--extract-audio"
"--audio-format" "opus"
"--output" "./audio.opus" "https://www.youtube.com/watch?v=\${1}"
];
# 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().rstrip(b"\n").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)
'';
envvar-to-stdin = writeExecline "envvar-to-stdin" { readNArgs = 1; } [
"importas" "VAR" "$1"
"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.${"if"} [
# remove leading slash from youtube-id
bins.backtick "-i" "yt-video-id" [
envvar-to-stdin "REQUEST_URI"
bins.cut "-c2-"
]
bins.importas "yt-video-id" "yt-video-id"
bins.fdmove "-c" "1" "2"
youtube-dl-audio "$yt-video-id"
]
serve-http-opus-file "./audio.opus"
];
# in printFeed
in serve-audio
# in youtube-dl-audio
|