about summary refs log tree commit diff
path: root/pkgs/profpatsch/youtube2audiopodcast
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2019-12-27 22:03:00 +0100
committerProfpatsch <mail@profpatsch.de>2019-12-27 22:03:00 +0100
commit3c742087af0d79bc2b86965606be673201b16346 (patch)
tree98ec1cd57066cb916914d6535e1f65851dd973c9 /pkgs/profpatsch/youtube2audiopodcast
parenta3909720af30191715520414e89bafe947df12e4 (diff)
pkgs/profpatsch/youtube2audiopodcast: serve rss feed & audio files
Diffstat (limited to 'pkgs/profpatsch/youtube2audiopodcast')
-rwxr-xr-xpkgs/profpatsch/youtube2audiopodcast/Main.hs4
-rw-r--r--pkgs/profpatsch/youtube2audiopodcast/default.nix62
2 files changed, 37 insertions, 29 deletions
diff --git a/pkgs/profpatsch/youtube2audiopodcast/Main.hs b/pkgs/profpatsch/youtube2audiopodcast/Main.hs
index 98be3867..4d0f9a93 100755
--- a/pkgs/profpatsch/youtube2audiopodcast/Main.hs
+++ b/pkgs/profpatsch/youtube2audiopodcast/Main.hs
@@ -5,6 +5,7 @@
 module Main where
 
 import Data.Text (Text)
+import Data.Text.Lazy.IO as TIO
 import Text.RSS.Syntax
 import Text.Feed.Types (Feed(RSSFeed))
 import Text.Feed.Export (textFeed)
@@ -68,7 +69,8 @@ main = do
         { channelName = "channel name"
         , channelURL = "channel url"
         })
-  print $ textFeed $ RSSFeed $ toRSS exConfig rss
+  let (Just feed) = textFeed $ RSSFeed $ toRSS exConfig rss
+  TIO.putStr feed
 
 toRSS :: Config -> Channel -> RSS
 toRSS Config{..} Channel{channelInfo, channelItems}  =
diff --git a/pkgs/profpatsch/youtube2audiopodcast/default.nix b/pkgs/profpatsch/youtube2audiopodcast/default.nix
index 6f3f85ec..379e46cf 100644
--- a/pkgs/profpatsch/youtube2audiopodcast/default.nix
+++ b/pkgs/profpatsch/youtube2audiopodcast/default.nix
@@ -1,8 +1,10 @@
 { pkgs, lib, writeExecline, writeHaskellInterpret, getBins, runInEmptyEnv, sandbox }:
 
+config@{ url, internalPort }:
+
 let
   bins = getBins pkgs.hello [ "hello" ]
-    // getBins pkgs.coreutils [ "printf" "wc" "tr" "cut" "mktemp" ]
+    // getBins pkgs.coreutils [ "printf" "wc" "tr" "cut" "mktemp" "mkdir" "ls" ]
     // getBins pkgs.youtube-dl [ "youtube-dl" ]
     // getBins pkgs.s6-networking [ "s6-tcpserver" ]
     // getBins pkgs.execline [ "fdmove" "backtick" "importas" "if" "redirfd" "pipeline" ]
@@ -72,8 +74,8 @@ let
   ];
 
   # serve an opus file as HTTP on stdout
-  serve-http-opus-file =
-    writeExecline "serve-http-opus-file" { readNArgs = 1; } [
+  serve-http-file = content-type:
+    writeExecline "serve-http-file" { readNArgs = 1; } [
       # determine file size
       bins.backtick "-i" "-n" "filesize" [
         bins.redirfd "-r" "0" "$1"
@@ -83,7 +85,7 @@ let
       # yolo html
       bins.${"if"} [ bins.printf ''
         HTTP/1.1 200 OK
-        Content-Type: audio/ogg
+        Content-Type: ${content-type}
         Content-Length: %u
 
       '' "$filesize" ]
@@ -91,22 +93,28 @@ let
       bins.redirfd "-r" "0" "$1" bins.cat
     ];
 
-  serve-audio = writeExecline "audio-server" {} [
+  dispatch-request = pkgs.writers.writeDash "dispatch-request" ''
+    case "$REQUEST_URI" in
+      /playlist/*)
+        ${bins.mkdir} /tmp >&2
+        ${bins.mkdir} /work >&2
+        ${print-feed-xml} "''${REQUEST_URI#/playlist/}" >/work/feed \
+          && ${serve-http-file "text/xml"} /work/feed
+        ;;
+      /video/*)
+        ${youtube-dl-audio} "''${REQUEST_URI#/video/}" 1>&2 \
+          && ${serve-http-file "audio/opus"} "./audio.opus"
+        ;;
+      *) return 1 ;;
+    esac
+  '';
+
+  http-server = writeExecline "http-server" {} [
     (runInEmptyEnv [])
-    bins.s6-tcpserver "::1" "8888"
+    bins.s6-tcpserver config.url config.internalPort
     (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"
+    dispatch-request
   ];
 
   example-config = pkgs.writeText "example-config.json" (lib.generators.toJSON {} {
@@ -150,18 +158,16 @@ let
     bins.jl playlist-info-jl
   ];
 
-  print-feed-json = writeExecline "ex2" {} [
-    "pipeline" [
-      youtube-playlist-info "PLV9hywkogVcOuHJ8O121ulSfFDKUhJw66"
-    ] (transform-flat-playlist-to-rss "localhost:8888")
-  ];
+  ex = "PLV9hywkogVcOuHJ8O121ulSfFDKUhJw66";
 
-  print-example-feed = writeExecline "ex" {} [
-    "pipeline" [ print-feed-json ]
+  print-feed-xml = writeExecline "print-feed-xml" { readNArgs = 1; } [
+    "pipeline" [
+      youtube-playlist-info "$1"
+    ]
+    "pipeline" [
+      (transform-flat-playlist-to-rss config.url)
+    ]
     printFeed
   ];
 
-
-# in printFeed
-# in serve-audio
-in print-example-feed
+in http-server