]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | htmlparser, | |
3 | httpClient, | |
4 | parseopt, | |
5 | osproc, | |
6 | sequtils, | |
7 | sugar, | |
8 | strformat, | |
9 | std/[terminal], | |
10 | strtabs, | |
11 | strutils, | |
12 | uri, | |
13 | xmltree | |
14 | ||
15 | import config | |
16 | ||
17 | type | |
18 | SearchResult = tuple[title: string, url: string] | |
19 | CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool] | |
20 | ||
21 | proc selectMediaPlayer(): string = | |
22 | let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) | |
23 | if len(availablePlayers) == 0: | |
24 | stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" | |
25 | raise newException(OSError, "No supported media player found") | |
26 | else: | |
27 | return availablePlayers[0] | |
28 | ||
29 | proc parseOptions(): CommandLineOptions = | |
30 | var | |
31 | searchQuery = "" | |
32 | musicOnly = false | |
33 | feelingLucky = false | |
34 | fullScreen = false | |
35 | ||
36 | for kind, key, value in getopt(): | |
37 | case kind | |
38 | of cmdArgument: | |
39 | searchQuery = key | |
40 | of cmdShortOption, cmdLongOption: | |
41 | case key | |
42 | of "m", "music": musicOnly = true | |
43 | of "l", "lucky": feelingLucky = true | |
44 | of "f", "full-screen": fullScreen = true | |
45 | of cmdEnd: | |
46 | discard | |
47 | ||
48 | return (searchQuery, musicOnly, feelingLucky, fullScreen) | |
49 | ||
50 | proc getYoutubePage(searchQuery: string): string = | |
51 | let queryParam = encodeUrl(searchQuery) | |
52 | let client = newHttpClient() | |
53 | let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}") | |
54 | return $response.body | |
55 | ||
56 | proc extractTitlesAndUrls(html: string): seq[SearchResult] = | |
57 | parseHtml(html).findAll("a"). | |
58 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). | |
59 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)] | |
60 | ||
61 | proc presentVideoOptions(searchResults: seq[SearchResult]) = | |
62 | echo "" | |
63 | for index, (title, url) in searchResults: | |
64 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" | |
65 | ||
66 | proc play(player: string, args: openArray[string]) = | |
67 | discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath, poEchoCmd}) | |
68 | quit(0) | |
69 | ||
70 | proc directPlay(searchQuery: string, player: string) = | |
71 | if "watch?" in searchQuery or "videos/watch" in searchQuery: | |
72 | play(player, args=[searchQuery]) | |
73 | elif searchQuery.startswith("magnet:"): | |
74 | play("peerflix", args=[searchQuery, &"--{player}"]) | |
75 | ||
76 | proc main() = | |
77 | let | |
78 | player = selectMediaPlayer() | |
79 | (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions() | |
80 | ||
81 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): | |
82 | directPlay(searchQuery, player) | |
83 | ||
84 | let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) | |
85 | ||
86 | let number = | |
87 | if feelingLucky: 0 | |
88 | else: | |
89 | presentVideoOptions(searchResults) | |
90 | stdout.styledWrite(fgYellow, "Choose video number: ") | |
91 | parseInt(readLine(stdin)) | |
92 | ||
93 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title | |
94 | ||
95 | # This is a pure function with no side effects | |
96 | func buildArgs(): seq[string] = | |
97 | var args = @[searchResults[number].url] | |
98 | if musicOnly: args.add("--no-video") | |
99 | if fullScreen: args.add("--fullscreen") | |
100 | return args | |
101 | ||
102 | # Play the video using the preferred/available media player | |
103 | play(player, buildArgs()) | |
104 | ||
105 | main() |