]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
Make all sequences immutable
[nimcoon.git] / src / lib.nim
index 4305b2cbb1c0387cee28ce38887608d744009608..6153992432c28b5bbcd8f4323b65063aa322e3b1 100644 (file)
@@ -58,14 +58,14 @@ func isPlaylist(url: string): bool =
 
 # This is a pure function with no side effects
 func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
-  var args = @[url]
-  if options["musicOnly"]: args.add("--no-video")
-  if options["fullScreen"]: args.add("--fullscreen")
-  # Playlists are only supported for MPV player
-  if isPlaylist(url) and player == "mpv":
-    let list_arg = url.split('&')[1]
-    args[0] = "https://www.youtube.com/playlist?" & list_arg
-  return args
+  let url =
+    # Playlists are only supported for MPV player
+    if isPlaylist(url) and player == "mpv":
+      "https://www.youtube.com/playlist?" & url.split('&')[1]
+    else: url
+  let musicOnly = if options["musicOnly"]: "--no-video" else: ""
+  let fullScreen = if options["fullScreen"]: "--fullscreen" else: ""
+  return filterIt([url, musicOnly, fullScreen], it != "")
 
 proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
   let args = buildPlayerArgs(url, options, player)
@@ -78,19 +78,13 @@ proc play*(player: string, options: Table[string, bool], url: string, title: str
 
 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
+    return @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o", downloadLocation, url]
 
 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
+    return @["-f", "best", "-o", downloadLocation, url]
 
 proc download*(args: openArray[string], title: string) =
   styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
@@ -106,11 +100,13 @@ func sanitizeURL*(url: string): string =
   urlLongen(stripZshEscaping(url))
 
 proc directPlay*(url: string, player: string, options: Table[string, bool]) =
-  if url.startswith("magnet:"):
+  if url.startswith("magnet:") or url.endswith(".torrent"):
     if options["musicOnly"]:
+      # TODO Replace with WebTorrent once it supports media player options
       discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
     else:
-      discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
+      # WebTorrent is so much faster!
+      discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions)
   else:
     play(player, options, url)