]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
Add option to download music
[nimcoon.git] / src / lib.nim
index 83154737295ab7f25e9d5957245591cd3f550a7e..1b6ed93770f0571ec0d9df915cb4302371637e17 100644 (file)
@@ -1,11 +1,13 @@
 import
   htmlparser,
   httpClient,
+  os,
   osproc,
   sequtils,
   sugar,
   strformat,
   std/[terminal],
+  strformat,
   strtabs,
   strutils,
   tables,
@@ -15,11 +17,12 @@ import
 import config
 
 type
-  SearchResult* = tuple[title: string, url: string]
   Options* = Table[string, bool]
+  SearchResult* = tuple[title: string, url: string]
   CommandLineOptions* = tuple[searchQuery: string, options: Options]
 
-let processOptions = {poStdErrToStdOut, poUsePath}
+# poEchoCmd can be added to options for debugging
+let processOptions = {poStdErrToStdOut, poUsePath, poEchoCmd}
 
 proc selectMediaPlayer*(): string =
   let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
@@ -47,10 +50,13 @@ proc presentVideoOptions*(searchResults: seq[SearchResult]) =
     styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
 
 proc play*(player: string, args: openArray[string], title: string) =
-  # poEchoCmd can be added to options for debugging
   styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
   discard execProcess(player, args=args, options=processOptions)
 
+proc download*(args: openArray[string], title: string) =
+  styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
+  discard execShellCmd(&"youtube-dl {args.join(\" \")}")
+
 func urlLongen(url: string): string =
   url.replace("youtu.be/", "www.youtube.com/watch?v=")