# 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)
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
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")