+func isPlaylist(url: string): bool =
+ ##[ Identifies if video is part of a playlist.
+ Only YouTube playlists are supported for now. ]##
+ "www.youtube.com" in url and "&list=" in url
+
+
+func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
+ let url =
+ # Playlists are only supported by MPV player. VLC needs a plugin.
+ 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: ""
+ filterIt([url, musicOnly, fullScreen], it != "")
+
+
+proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
+ let args = buildPlayerArgs(url, options, player)
+ if title != "":
+ styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
+ 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.}:
+ let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
+ @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3",
+ "--audio-quality", "0", "-o", downloadLocation, url]
+
+
+func buildVideoDownloadArgs(url: string): seq[string] =
+ {.noSideEffect.}:
+ let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
+ @["-f", "best", "-o", downloadLocation, url]
+
+
+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=")
+
+
+func rewriteInvidiousToYouTube(url: string): string =
+ {.noSideEffect.}:
+ if rewriteInvidiousURLs: url.replace("invidio.us", "www.youtube.com") else: url
+
+
+func stripZshEscaping(url: string): string = url.replace("\\", "")