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