]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | htmlparser, | |
3 | httpClient, | |
4 | os, | |
5 | osproc, | |
6 | sequtils, | |
7 | sugar, | |
8 | strformat, | |
9 | std/[terminal], | |
10 | strformat, | |
11 | strtabs, | |
12 | strutils, | |
13 | tables, | |
14 | uri, | |
15 | xmltree | |
16 | ||
17 | import config | |
18 | ||
19 | type | |
20 | Options* = Table[string, bool] | |
21 | SearchResult* = tuple[title: string, url: string] | |
22 | CommandLineOptions* = tuple[searchQuery: string, options: Options] | |
23 | ||
24 | # poEchoCmd can be added to options for debugging | |
25 | let processOptions = {poStdErrToStdOut, poUsePath, poEchoCmd} | |
26 | ||
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") | |
32 | else: | |
33 | return availablePlayers[0] | |
34 | ||
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}") | |
39 | return $response.body | |
40 | ||
41 | func extractTitlesAndUrls*(html: string): seq[SearchResult] = | |
42 | {.noSideEffect.}: | |
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"])) | |
46 | ||
47 | proc presentVideoOptions*(searchResults: seq[SearchResult]) = | |
48 | eraseScreen() | |
49 | for index, (title, url) in searchResults: | |
50 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" | |
51 | ||
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(\" \")}") | |
56 | else: | |
57 | discard execProcess(player, args=args, options=processOptions) | |
58 | ||
59 | proc download*(args: openArray[string], title: string) = | |
60 | styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title | |
61 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
62 | ||
63 | func urlLongen(url: string): string = | |
64 | url.replace("youtu.be/", "www.youtube.com/watch?v=") | |
65 | ||
66 | func stripZshEscaping(url: string): string = | |
67 | url.replace("\\", "") | |
68 | ||
69 | func sanitizeURL*(url: string): string = | |
70 | urlLongen(stripZshEscaping(url)) | |
71 | ||
72 | proc directPlay*(searchQuery: string, player: string) = | |
73 | let url = sanitizeURL(searchQuery) | |
74 | if searchQuery.startswith("magnet:"): | |
75 | discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions) | |
76 | else: | |
77 | discard execProcess(player, args=[url], options=processOptions) |