X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/d36e22010231d8bd139b906fc66e060575decb2d..046c2cc3b1dc773cf9e1b92f2c4fb41e2d6d8eb0:/src/lib.nim diff --git a/src/lib.nim b/src/lib.nim index 67095ac..6153992 100644 --- a/src/lib.nim +++ b/src/lib.nim @@ -52,18 +52,20 @@ proc presentVideoOptions*(searchResults: SearchResults) = styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" 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 # 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": - args.add("--ytdl-raw-options=\"yes-playlist=\"") - 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) @@ -76,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 @@ -104,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)