]>
Commit | Line | Data |
---|---|---|
d65a1dcf | 1 | import |
d65a1dcf | 2 | httpClient, |
c12cc641 | 3 | json, |
e9f0c7d0 | 4 | os, |
d65a1dcf | 5 | osproc, |
c12cc641 | 6 | re, |
d65a1dcf | 7 | sequtils, |
d65a1dcf | 8 | std/[terminal], |
e9f0c7d0 | 9 | strformat, |
d65a1dcf | 10 | strutils, |
e08e5cbe | 11 | tables |
d65a1dcf | 12 | |
e08e5cbe JN |
13 | import |
14 | config, | |
15 | types | |
d65a1dcf | 16 | |
b44b6494 | 17 | |
b44b6494 | 18 | let |
a2d28598 | 19 | processOptions = {poStdErrToStdOut, poUsePath, poEchoCmd} |
b44b6494 JN |
20 | PEERTUBE_REGEX = re"videos\/watch\/[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}" |
21 | ||
c12cc641 JN |
22 | |
23 | proc isInstalled(program: string): bool = | |
24 | execProcess("which " & program).len != 0 | |
9e6b8568 | 25 | |
b44b6494 | 26 | |
d65a1dcf | 27 | proc selectMediaPlayer*(): string = |
c12cc641 | 28 | let availablePlayers = supportedPlayers.filter(isInstalled) |
d65a1dcf JN |
29 | if len(availablePlayers) == 0: |
30 | stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" | |
31 | raise newException(OSError, "No supported media player found") | |
32 | else: | |
33 | return availablePlayers[0] | |
34 | ||
b44b6494 | 35 | |
c12cc641 | 36 | proc getPeerTubeMagnetLink(url: string): string = |
86e6cb72 | 37 | ## Gets the magnet link of the best possible resolution from PeerTube |
c12cc641 JN |
38 | let uuid = url.substr(find(url, PEERTUBE_REGEX) + "videos/watch/".len) |
39 | let domainName = url.substr(8, find(url, '/', start=8) - 1) | |
40 | let apiURL = &"https://{domainName}/api/v1/videos/{uuid}" | |
41 | let client = newHttpClient() | |
42 | let response = get(client, apiURL) | |
43 | let jsonNode = parseJson($response.body) | |
44 | jsonNode["files"][0]["magnetUri"].getStr() | |
45 | ||
b44b6494 | 46 | |
13a4017d | 47 | proc presentVideoOptions*(searchResults: SearchResults) = |
17955bba | 48 | eraseScreen() |
d65a1dcf | 49 | for index, (title, url) in searchResults: |
86e6cb72 | 50 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, " ", url, "\n" |
d65a1dcf | 51 | |
a2d28598 | 52 | |
d36e2201 | 53 | func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] = |
046c2cc3 JN |
54 | let musicOnly = if options["musicOnly"]: "--no-video" else: "" |
55 | let fullScreen = if options["fullScreen"]: "--fullscreen" else: "" | |
55b0cae7 | 56 | filterIt([url, musicOnly, fullScreen], it != "") |
d36e2201 | 57 | |
b44b6494 | 58 | |
d36e2201 JN |
59 | proc play*(player: string, options: Table[string, bool], url: string, title: string = "") = |
60 | let args = buildPlayerArgs(url, options, player) | |
61 | if title != "": | |
62 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title | |
e71f26f3 JN |
63 | if "--no-video" in args: |
64 | discard execShellCmd(&"{player} {args.join(\" \")}") | |
65 | else: | |
a2d28598 | 66 | discard execProcess(player, args=args, options=processOptions) |
d65a1dcf | 67 | |
b44b6494 JN |
68 | |
69 | func buildMusicDownloadArgs(url: string): seq[string] = | |
9a8ef4ad | 70 | {.noSideEffect.}: |
9a8ef4ad | 71 | let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'" |
b44b6494 JN |
72 | @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", |
73 | "--audio-quality", "0", "-o", downloadLocation, url] | |
74 | ||
9a8ef4ad | 75 | |
b44b6494 | 76 | func buildVideoDownloadArgs(url: string): seq[string] = |
9a8ef4ad | 77 | {.noSideEffect.}: |
9a8ef4ad | 78 | let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'" |
55b0cae7 | 79 | @["-f", "best", "-o", downloadLocation, url] |
9a8ef4ad | 80 | |
b44b6494 | 81 | |
e9f0c7d0 JN |
82 | proc download*(args: openArray[string], title: string) = |
83 | styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title | |
84 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
85 | ||
b44b6494 | 86 | |
55b0cae7 | 87 | func urlLongen(url: string): string = url.replace("youtu.be/", "www.youtube.com/watch?v=") |
d65a1dcf | 88 | |
b44b6494 | 89 | |
a2d28598 | 90 | func rewriteInvidiousToYouTube*(url: string): string = |
b44b6494 | 91 | {.noSideEffect.}: |
a2d28598 JN |
92 | if rewriteInvidiousURLs and url.replace(".", "").contains("invidious"): |
93 | &"https://www.youtube.com/watch?v={url.split(\"=\")[1]}" | |
94 | else: url | |
b44b6494 JN |
95 | |
96 | ||
c30c694e | 97 | func stripZshEscaping(url: string): string = url.replace("\\", "") |
d65a1dcf | 98 | |
b44b6494 JN |
99 | |
100 | func sanitizeURL*(url: string): string = | |
101 | rewriteInvidiousToYouTube(urlLongen(stripZshEscaping(url))) | |
102 | ||
d65a1dcf | 103 | |
d36e2201 | 104 | proc directPlay*(url: string, player: string, options: Table[string, bool]) = |
c12cc641 JN |
105 | let url = |
106 | if find(url, PEERTUBE_REGEX) != -1 and isInstalled("webtorrent"): | |
107 | getPeerTubeMagnetLink(url) | |
108 | else: url | |
d7688e97 | 109 | if url.startswith("magnet:") or url.endswith(".torrent"): |
d36e2201 | 110 | if options["musicOnly"]: |
8d06ad69 | 111 | # TODO Replace with WebTorrent once it supports media player options |
811928a7 JN |
112 | discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video") |
113 | else: | |
8d06ad69 JN |
114 | # WebTorrent is so much faster! |
115 | discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions) | |
fe1a5856 | 116 | else: |
d36e2201 | 117 | play(player, options, url) |
811928a7 | 118 | |
b44b6494 | 119 | |
811928a7 JN |
120 | proc directDownload*(url: string, musicOnly: bool) = |
121 | let args = | |
122 | if musicOnly: buildMusicDownloadArgs(url) | |
123 | else: buildVideoDownloadArgs(url) | |
0c2f0385 JN |
124 | if isInstalled("aria2c"): |
125 | discard execShellCmd(&"youtube-dl {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'") | |
126 | else: | |
127 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
13a4017d | 128 | |
b44b6494 | 129 | |
13a4017d JN |
130 | proc offerSelection(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange): string = |
131 | if options["feelingLucky"]: "0" | |
132 | else: | |
133 | presentVideoOptions(searchResults[selectionRange.begin .. selectionRange.until]) | |
134 | stdout.styledWrite(fgYellow, "Choose video number: ") | |
135 | readLine(stdin) | |
136 | ||
b44b6494 | 137 | |
13a4017d JN |
138 | proc handleUserInput(searchResult: SearchResult, options: Table[string, bool], player: string) = |
139 | if options["download"]: | |
140 | if options["musicOnly"]: | |
141 | download(buildMusicDownloadArgs(searchResult.url), searchResult.title) | |
142 | else: | |
143 | download(buildVideoDownloadArgs(searchResult.url), searchResult.title) | |
144 | else: | |
d36e2201 | 145 | play(player, options, searchResult.url, searchResult.title) |
13a4017d | 146 | |
b44b6494 | 147 | |
13a4017d | 148 | proc present*(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange, player: string) = |
55b0cae7 JN |
149 | ##[ Continuously present options till the user quits the application |
150 | ||
151 | selectionRange: Currently available range to choose from depending on pagination | |
152 | ]## | |
13a4017d JN |
153 | |
154 | let userInput = offerSelection(searchResults, options, selectionRange) | |
155 | ||
156 | case userInput | |
157 | of "all": | |
158 | for selection in selectionRange.begin .. selectionRange.until: | |
159 | handleUserInput(searchResults[selection], options, player) | |
160 | quit(0) | |
161 | of "n": | |
162 | if selectionRange.until + 1 < len(searchResults): | |
163 | let newSelectionRange = (selectionRange.until + 1, min(len(searchResults) - 1, selectionRange.until + limit)) | |
164 | present(searchResults, options, newSelectionRange, player) | |
ebae91b4 JN |
165 | else: |
166 | present(searchResults, options, selectionRange, player) | |
13a4017d JN |
167 | of "p": |
168 | if selectionRange.begin > 0: | |
169 | let newSelectionRange = (selectionRange.begin - limit, selectionRange.until - limit) | |
170 | present(searchResults, options, newSelectionRange, player) | |
ebae91b4 JN |
171 | else: |
172 | present(searchResults, options, selectionRange, player) | |
13a4017d JN |
173 | of "q": |
174 | quit(0) | |
175 | else: | |
db34bbff JN |
176 | let searchResult = searchResults[selectionRange.begin .. selectionRange.until][parseInt(userInput)] |
177 | handleUserInput(searchResult, options, player) | |
13a4017d JN |
178 | if options["feelingLucky"]: |
179 | quit(0) | |
180 | else: | |
181 | present(searchResults, options, selectionRange, player) |