]>
Commit | Line | Data |
---|---|---|
6ff2dbac JN |
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 | |
44978125 | 14 | |
f7735b43 | 15 | import config |
c760b5d9 | 16 | |
121e06b2 JN |
17 | type |
18 | SearchResult = tuple[title: string, url: string] | |
d1e4d2de | 19 | CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool] |
2f3c8875 | 20 | |
933e339b JN |
21 | proc selectMediaPlayer(): string = |
22 | let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) | |
23 | if len(availablePlayers) == 0: | |
b40b7243 | 24 | stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" |
933e339b JN |
25 | raise newException(OSError, "No supported media player found") |
26 | else: | |
27 | return availablePlayers[0] | |
44978125 | 28 | |
121e06b2 JN |
29 | proc parseOptions(): CommandLineOptions = |
30 | var | |
31 | searchQuery = "" | |
32 | musicOnly = false | |
33 | feelingLucky = false | |
d1e4d2de | 34 | fullScreen = false |
121e06b2 JN |
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 | |
d1e4d2de | 44 | of "f", "full-screen": fullScreen = true |
121e06b2 JN |
45 | of cmdEnd: |
46 | discard | |
47 | ||
d1e4d2de | 48 | return (searchQuery, musicOnly, feelingLucky, fullScreen) |
121e06b2 | 49 | |
39a495a9 JN |
50 | proc getYoutubePage(searchQuery: string): string = |
51 | let queryParam = encodeUrl(searchQuery) | |
485b1053 | 52 | let client = newHttpClient() |
b40b7243 | 53 | let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}") |
485b1053 | 54 | return $response.body |
44978125 | 55 | |
2f3c8875 | 56 | proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] = |
485b1053 | 57 | parseHtml(htmlFile).findAll("a"). |
44978125 | 58 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). |
c760b5d9 | 59 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)] |
44978125 | 60 | |
2f3c8875 | 61 | proc presentVideoOptions(searchResults: seq[SearchResult]) = |
efcc0441 | 62 | echo "" |
2f3c8875 | 63 | for index, (title, url) in searchResults: |
3d5bf3fd | 64 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" |
44978125 | 65 | |
aaeaf00b JN |
66 | proc play(player: string, args: openArray[string]) = |
67 | discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath, poEchoCmd}) | |
ba35766a JN |
68 | quit(0) |
69 | ||
121e06b2 | 70 | proc directPlay(searchQuery: string, player: string) = |
aaeaf00b JN |
71 | if "watch?" in searchQuery or "videos/watch" in searchQuery: |
72 | play(player, args=[searchQuery]) | |
121e06b2 | 73 | elif searchQuery.startswith("magnet:"): |
aaeaf00b | 74 | play("peerflix", args=[searchQuery, &"--{player}"]) |
121e06b2 JN |
75 | |
76 | proc main() = | |
77 | let | |
78 | player = selectMediaPlayer() | |
d1e4d2de | 79 | (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions() |
121e06b2 | 80 | |
92f80e5a JN |
81 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): |
82 | directPlay(searchQuery, player) | |
121e06b2 JN |
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 | |
efcc0441 | 94 | |
aaeaf00b | 95 | var args = @[searchResults[number].url] |
efcc0441 | 96 | |
121e06b2 | 97 | if musicOnly: |
46817cb7 | 98 | args.add("--no-video") |
a2319a3f | 99 | |
d1e4d2de | 100 | if fullScreen: |
46817cb7 | 101 | args.add("--fullscreen") |
d1e4d2de | 102 | |
121e06b2 | 103 | # Play the video using the preferred/available media player |
aaeaf00b | 104 | play(player, args) |
a2319a3f | 105 | |
121e06b2 | 106 | main() |