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