20 Options* = Table[string, bool]
21 SearchResult* = tuple[title: string, url: string]
22 CommandLineOptions* = tuple[searchQuery: string, options: Options]
24 # poEchoCmd can be added to options for debugging
25 let processOptions = {poStdErrToStdOut, poUsePath}
27 proc selectMediaPlayer*(): string =
28 let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
29 if len(availablePlayers) == 0:
30 stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}"
31 raise newException(OSError, "No supported media player found")
33 return availablePlayers[0]
35 proc getYoutubePage*(searchQuery: string): string =
36 let queryParam = encodeUrl(searchQuery)
37 let client = newHttpClient()
38 let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
41 func extractTitlesAndUrls*(html: string): seq[SearchResult] =
43 parseHtml(html).findAll("a").
44 filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
45 map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))
47 proc presentVideoOptions*(searchResults: seq[SearchResult]) =
49 for index, (title, url) in searchResults:
50 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
52 proc play*(player: string, args: openArray[string], title: string) =
53 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
54 if "--no-video" in args:
55 discard execShellCmd(&"{player} {args.join(\" \")}")
57 discard execProcess(player, args=args, options=processOptions)
59 func buildMusicDownloadArgs*(url: string): seq[string] =
61 var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
62 let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
63 args.add(downloadLocation)
67 func buildVideoDownloadArgs*(url: string): seq[string] =
69 var args = @["-f", "best", "-o"]
70 let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
71 args.add(downloadLocation)
75 proc download*(args: openArray[string], title: string) =
76 styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
77 discard execShellCmd(&"youtube-dl {args.join(\" \")}")
79 func urlLongen(url: string): string =
80 url.replace("youtu.be/", "www.youtube.com/watch?v=")
82 func stripZshEscaping(url: string): string =
85 func sanitizeURL*(url: string): string =
86 urlLongen(stripZshEscaping(url))
88 proc directPlay*(url: string, player: string, musicOnly: bool) =
89 if url.startswith("magnet:"):
91 discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
93 discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions)
96 discard execShellCmd(&"{player} --no-video {url}")
98 discard execProcess(player, args=[url], options=processOptions)
100 proc directDownload*(url: string, musicOnly: bool) =
102 if musicOnly: buildMusicDownloadArgs(url)
103 else: buildVideoDownloadArgs(url)
104 discard execShellCmd(&"youtube-dl {args.join(\" \")}")