]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
Support directPlay and directDownload for music
[nimcoon.git] / src / lib.nim
index f5ee8a57001ccafa86eeb8f99e2f861b347eb2f8..1fa21685006241c3e2bd81aea15cab7b0fd64eb8 100644 (file)
@@ -56,6 +56,22 @@ proc play*(player: string, args: openArray[string], title: string) =
   else:
     discard execProcess(player, args=args, options=processOptions)
 
+func buildMusicDownloadArgs*(url: string): seq[string] =
+  {.noSideEffect.}:
+    var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
+    let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
+    args.add(downloadLocation)
+    args.add(url)
+    return args
+
+func buildVideoDownloadArgs*(url: string): seq[string] =
+  {.noSideEffect.}:
+    var args = @["-f", "best", "-o"]
+    let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
+    args.add(downloadLocation)
+    args.add(url)
+    return args
+
 proc download*(args: openArray[string], title: string) =
   styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
   discard execShellCmd(&"youtube-dl {args.join(\" \")}")
@@ -69,9 +85,20 @@ func stripZshEscaping(url: string): string =
 func sanitizeURL*(url: string): string =
   urlLongen(stripZshEscaping(url))
 
-proc directPlay*(searchQuery: string, player: string) =
-  let url = sanitizeURL(searchQuery)
-  if searchQuery.startswith("magnet:"):
-    discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
+proc directPlay*(url: string, player: string, musicOnly: bool) =
+  if url.startswith("magnet:"):
+    if musicOnly:
+      discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
+    else:
+      discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
   else:
-    discard execProcess(player, args=[url], options=processOptions)
+    if musicOnly:
+      discard execShellCmd(&"{player} --no-video {url}")
+    else:
+      discard execProcess(player, args=[url], options=processOptions)
+
+proc directDownload*(url: string, musicOnly: bool) =
+  let args =
+    if musicOnly: buildMusicDownloadArgs(url)
+    else: buildVideoDownloadArgs(url)
+  discard execShellCmd(&"youtube-dl {args.join(\" \")}")