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