]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
pagination: "n" takes to next page of results
[nimcoon.git] / src / lib.nim
index 83154737295ab7f25e9d5957245591cd3f550a7e..54259e2d9f7d08237c3bb73ec51e30fb9751167a 100644 (file)
@@ -1,11 +1,13 @@
 import
   htmlparser,
   httpClient,
+  os,
   osproc,
   sequtils,
   sugar,
   strformat,
   std/[terminal],
+  strformat,
   strtabs,
   strutils,
   tables,
@@ -15,10 +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]
+  SelectionRange* = tuple[begin: int, until: int]
 
+# poEchoCmd can be added to options for debugging
 let processOptions = {poStdErrToStdOut, poUsePath}
 
 proc selectMediaPlayer*(): string =
@@ -47,9 +51,31 @@ 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)
+  if "--no-video" in args:
+    discard execShellCmd(&"{player} {args.join(\" \")}")
+  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(\" \")}")
 
 func urlLongen(url: string): string =
   url.replace("youtu.be/", "www.youtube.com/watch?v=")
@@ -60,9 +86,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(\" \")}")