X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/e71f26f3b64d2bbe6c7b8d355a82c33f985aadf0..e6561dc98ae9f83c63ebf3e14fbefbd6e5264f5d:/src/lib.nim diff --git a/src/lib.nim b/src/lib.nim index f5ee8a5..54259e2 100644 --- a/src/lib.nim +++ b/src/lib.nim @@ -20,9 +20,10 @@ type 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, poEchoCmd} +let processOptions = {poStdErrToStdOut, poUsePath} proc selectMediaPlayer*(): string = let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) @@ -56,6 +57,22 @@ proc play*(player: string, args: openArray[string], title: string) = 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(\" \")}") @@ -69,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(\" \")}")