17 SearchResult* = tuple[title: string, url: string]
18 CommandLineOptions* = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool]
20 proc selectMediaPlayer*(): string =
21 let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
22 if len(availablePlayers) == 0:
23 stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}"
24 raise newException(OSError, "No supported media player found")
26 return availablePlayers[0]
28 proc getYoutubePage*(searchQuery: string): string =
29 let queryParam = encodeUrl(searchQuery)
30 let client = newHttpClient()
31 let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
34 proc extractTitlesAndUrls*(html: string): seq[SearchResult] =
35 parseHtml(html).findAll("a").
36 filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
37 map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)]
39 proc presentVideoOptions*(searchResults: seq[SearchResult]) =
41 for index, (title, url) in searchResults:
42 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
44 proc play*(player: string, args: openArray[string]) =
45 # poEchoCmd can be added to options for debugging
46 discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath})
49 func urlLongen(url: string): string =
50 url.replace("youtu.be/", "www.youtube.com/watch?v=")
52 func stripZshEscaping(url: string): string =
55 func sanitizeURL*(url: string): string =
56 urlLongen(stripZshEscaping(url))
58 proc directPlay*(searchQuery: string, player: string) =
59 let url = sanitizeURL(searchQuery)
60 if searchQuery.startswith("magnet:"):
61 play("peerflix", args=[url, &"--{player}"])
63 play(player, args=[url])