X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/427e87f9d63a2514a8f31ba915e934fcab8399a2..9a8ef4ad5f1c6dbee407cbea01224676131060ba:/src/lib.nim diff --git a/src/lib.nim b/src/lib.nim index 53c2e5d..ffa4a4a 100644 --- a/src/lib.nim +++ b/src/lib.nim @@ -1,23 +1,28 @@ import htmlparser, httpClient, + os, osproc, sequtils, sugar, strformat, std/[terminal], + strformat, strtabs, strutils, + tables, uri, xmltree import config type + Options* = Table[string, bool] SearchResult* = tuple[title: string, url: string] - CommandLineOptions* = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool] + CommandLineOptions* = tuple[searchQuery: string, options: Options] -let processOptions = {poStdErrToStdOut, poUsePath} +# poEchoCmd can be added to options for debugging +let processOptions = {poStdErrToStdOut, poUsePath, poEchoCmd} proc selectMediaPlayer*(): string = let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) @@ -40,14 +45,36 @@ func extractTitlesAndUrls*(html: string): seq[SearchResult] = map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"])) proc presentVideoOptions*(searchResults: seq[SearchResult]) = - echo "" + eraseScreen() for index, (title, url) in searchResults: styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" proc play*(player: string, args: openArray[string], title: string) = - # poEchoCmd can be added to options for debugging styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title - discard execProcess(player, args=args, options=processOptions) + 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.}: + 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(\" \")}") func urlLongen(url: string): string = url.replace("youtu.be/", "www.youtube.com/watch?v=") @@ -58,9 +85,12 @@ 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:"): +proc directPlay*(url: string, player: string) = + if url.startswith("magnet:"): discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions) else: discard execProcess(player, args=[url], options=processOptions) + +proc directDownload*(url: string) = + let args = buildVideoDownloadArgs(url) + discard execShellCmd(&"youtube-dl {args.join(\" \")}")