]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | htmlparser, | |
3 | httpClient, | |
4 | os, | |
5 | osproc, | |
6 | sequtils, | |
7 | sugar, | |
8 | strformat, | |
9 | std/[terminal], | |
10 | strformat, | |
11 | strtabs, | |
12 | strutils, | |
13 | tables, | |
14 | uri, | |
15 | xmltree | |
16 | ||
17 | import config | |
18 | ||
19 | type | |
20 | Options* = Table[string, bool] | |
21 | SearchResult* = tuple[title: string, url: string] | |
22 | SearchResults* = seq[tuple[title: string, url: string]] | |
23 | CommandLineOptions* = tuple[searchQuery: string, options: Options] | |
24 | SelectionRange* = tuple[begin: int, until: int] | |
25 | ||
26 | # poEchoCmd can be added to options for debugging | |
27 | let processOptions = {poStdErrToStdOut, poUsePath} | |
28 | ||
29 | proc selectMediaPlayer*(): string = | |
30 | let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) | |
31 | if len(availablePlayers) == 0: | |
32 | stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" | |
33 | raise newException(OSError, "No supported media player found") | |
34 | else: | |
35 | return availablePlayers[0] | |
36 | ||
37 | proc getYoutubePage*(searchQuery: string): string = | |
38 | let queryParam = encodeUrl(searchQuery) | |
39 | let client = newHttpClient() | |
40 | let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}") | |
41 | return $response.body | |
42 | ||
43 | func extractTitlesAndUrls*(html: string): SearchResults = | |
44 | {.noSideEffect.}: | |
45 | parseHtml(html).findAll("a"). | |
46 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). | |
47 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"])) | |
48 | ||
49 | proc presentVideoOptions*(searchResults: SearchResults) = | |
50 | eraseScreen() | |
51 | for index, (title, url) in searchResults: | |
52 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" | |
53 | ||
54 | func isPlaylist(url: string): bool = | |
55 | # Identifies if video is part of a playlist | |
56 | # Only YouTube playlists are supported for now | |
57 | "www.youtube.com" in url and "&list=" in url | |
58 | ||
59 | # This is a pure function with no side effects | |
60 | func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] = | |
61 | var args = @[url] | |
62 | if options["musicOnly"]: args.add("--no-video") | |
63 | if options["fullScreen"]: args.add("--fullscreen") | |
64 | # Playlists are only supported for MPV player | |
65 | if isPlaylist(url) and player == "mpv": | |
66 | let list_arg = url.split('&')[1] | |
67 | args[0] = "https://www.youtube.com/playlist?" & list_arg | |
68 | return args | |
69 | ||
70 | proc play*(player: string, options: Table[string, bool], url: string, title: string = "") = | |
71 | let args = buildPlayerArgs(url, options, player) | |
72 | if title != "": | |
73 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title | |
74 | if "--no-video" in args: | |
75 | discard execShellCmd(&"{player} {args.join(\" \")}") | |
76 | else: | |
77 | discard execProcess(player, args=args, options=processOptions) | |
78 | ||
79 | func buildMusicDownloadArgs*(url: string): seq[string] = | |
80 | {.noSideEffect.}: | |
81 | var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"] | |
82 | let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'" | |
83 | args.add(downloadLocation) | |
84 | args.add(url) | |
85 | return args | |
86 | ||
87 | func buildVideoDownloadArgs*(url: string): seq[string] = | |
88 | {.noSideEffect.}: | |
89 | var args = @["-f", "best", "-o"] | |
90 | let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'" | |
91 | args.add(downloadLocation) | |
92 | args.add(url) | |
93 | return args | |
94 | ||
95 | proc download*(args: openArray[string], title: string) = | |
96 | styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title | |
97 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
98 | ||
99 | func urlLongen(url: string): string = | |
100 | url.replace("youtu.be/", "www.youtube.com/watch?v=") | |
101 | ||
102 | func stripZshEscaping(url: string): string = | |
103 | url.replace("\\", "") | |
104 | ||
105 | func sanitizeURL*(url: string): string = | |
106 | urlLongen(stripZshEscaping(url)) | |
107 | ||
108 | proc directPlay*(url: string, player: string, options: Table[string, bool]) = | |
109 | if url.startswith("magnet:"): | |
110 | if options["musicOnly"]: | |
111 | # TODO Replace with WebTorrent once it supports media player options | |
112 | discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video") | |
113 | else: | |
114 | # WebTorrent is so much faster! | |
115 | discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions) | |
116 | else: | |
117 | play(player, options, url) | |
118 | ||
119 | proc directDownload*(url: string, musicOnly: bool) = | |
120 | let args = | |
121 | if musicOnly: buildMusicDownloadArgs(url) | |
122 | else: buildVideoDownloadArgs(url) | |
123 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
124 | ||
125 | proc offerSelection(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange): string = | |
126 | if options["feelingLucky"]: "0" | |
127 | else: | |
128 | presentVideoOptions(searchResults[selectionRange.begin .. selectionRange.until]) | |
129 | stdout.styledWrite(fgYellow, "Choose video number: ") | |
130 | readLine(stdin) | |
131 | ||
132 | proc handleUserInput(searchResult: SearchResult, options: Table[string, bool], player: string) = | |
133 | if options["download"]: | |
134 | if options["musicOnly"]: | |
135 | download(buildMusicDownloadArgs(searchResult.url), searchResult.title) | |
136 | else: | |
137 | download(buildVideoDownloadArgs(searchResult.url), searchResult.title) | |
138 | else: | |
139 | play(player, options, searchResult.url, searchResult.title) | |
140 | ||
141 | proc present*(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange, player: string) = | |
142 | #[ Continuously present options till the user quits the application | |
143 | selectionRange: Currently available range to choose from depending on pagination | |
144 | ]# | |
145 | ||
146 | let userInput = offerSelection(searchResults, options, selectionRange) | |
147 | ||
148 | case userInput | |
149 | of "all": | |
150 | for selection in selectionRange.begin .. selectionRange.until: | |
151 | handleUserInput(searchResults[selection], options, player) | |
152 | quit(0) | |
153 | of "n": | |
154 | if selectionRange.until + 1 < len(searchResults): | |
155 | let newSelectionRange = (selectionRange.until + 1, min(len(searchResults) - 1, selectionRange.until + limit)) | |
156 | present(searchResults, options, newSelectionRange, player) | |
157 | else: | |
158 | present(searchResults, options, selectionRange, player) | |
159 | of "p": | |
160 | if selectionRange.begin > 0: | |
161 | let newSelectionRange = (selectionRange.begin - limit, selectionRange.until - limit) | |
162 | present(searchResults, options, newSelectionRange, player) | |
163 | else: | |
164 | present(searchResults, options, selectionRange, player) | |
165 | of "q": | |
166 | quit(0) | |
167 | else: | |
168 | let searchResult = searchResults[selectionRange.begin .. selectionRange.until][parseInt(userInput)] | |
169 | handleUserInput(searchResult, options, player) | |
170 | if options["feelingLucky"]: | |
171 | quit(0) | |
172 | else: | |
173 | present(searchResults, options, selectionRange, player) |