]> njoseph.me Git - nimcoon.git/blame - src/lib.nim
YouTube auto-play
[nimcoon.git] / src / lib.nim
CommitLineData
d65a1dcf 1import
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
13import
14 config,
5f9cdfef
JN
15 types,
16 youtube
d65a1dcf 17
b44b6494 18
b44b6494 19let
a2d28598 20 processOptions = {poStdErrToStdOut, poUsePath, poEchoCmd}
b44b6494
JN
21 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}"
22
c12cc641
JN
23
24proc isInstalled(program: string): bool =
25 execProcess("which " & program).len != 0
9e6b8568 26
b44b6494 27
d65a1dcf 28proc selectMediaPlayer*(): string =
c12cc641 29 let availablePlayers = supportedPlayers.filter(isInstalled)
d65a1dcf
JN
30 if len(availablePlayers) == 0:
31 stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}"
32 raise newException(OSError, "No supported media player found")
33 else:
34 return availablePlayers[0]
35
b44b6494 36
c12cc641 37proc getPeerTubeMagnetLink(url: string): string =
86e6cb72 38 ## Gets the magnet link of the best possible resolution from PeerTube
c12cc641
JN
39 let uuid = url.substr(find(url, PEERTUBE_REGEX) + "videos/watch/".len)
40 let domainName = url.substr(8, find(url, '/', start=8) - 1)
41 let apiURL = &"https://{domainName}/api/v1/videos/{uuid}"
42 let client = newHttpClient()
43 let response = get(client, apiURL)
44 let jsonNode = parseJson($response.body)
45 jsonNode["files"][0]["magnetUri"].getStr()
46
b44b6494 47
13a4017d 48proc presentVideoOptions*(searchResults: SearchResults) =
17955bba 49 eraseScreen()
d65a1dcf 50 for index, (title, url) in searchResults:
86e6cb72 51 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, " ", url, "\n"
d65a1dcf 52
a2d28598 53
d36e2201 54func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
046c2cc3
JN
55 let musicOnly = if options["musicOnly"]: "--no-video" else: ""
56 let fullScreen = if options["fullScreen"]: "--fullscreen" else: ""
55b0cae7 57 filterIt([url, musicOnly, fullScreen], it != "")
d36e2201 58
b44b6494 59
d36e2201
JN
60proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
61 let args = buildPlayerArgs(url, options, player)
62 if title != "":
63 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
e71f26f3
JN
64 if "--no-video" in args:
65 discard execShellCmd(&"{player} {args.join(\" \")}")
66 else:
a2d28598 67 discard execProcess(player, args=args, options=processOptions)
d65a1dcf 68
b44b6494
JN
69
70func buildMusicDownloadArgs(url: string): seq[string] =
9a8ef4ad 71 {.noSideEffect.}:
9a8ef4ad 72 let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
b44b6494
JN
73 @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3",
74 "--audio-quality", "0", "-o", downloadLocation, url]
75
9a8ef4ad 76
b44b6494 77func buildVideoDownloadArgs(url: string): seq[string] =
9a8ef4ad 78 {.noSideEffect.}:
9a8ef4ad 79 let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
55b0cae7 80 @["-f", "best", "-o", downloadLocation, url]
9a8ef4ad 81
b44b6494 82
e9f0c7d0
JN
83proc download*(args: openArray[string], title: string) =
84 styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
85 discard execShellCmd(&"youtube-dl {args.join(\" \")}")
86
b44b6494 87
55b0cae7 88func urlLongen(url: string): string = url.replace("youtu.be/", "www.youtube.com/watch?v=")
d65a1dcf 89
b44b6494 90
a2d28598 91func rewriteInvidiousToYouTube*(url: string): string =
b44b6494 92 {.noSideEffect.}:
a2d28598
JN
93 if rewriteInvidiousURLs and url.replace(".", "").contains("invidious"):
94 &"https://www.youtube.com/watch?v={url.split(\"=\")[1]}"
95 else: url
b44b6494
JN
96
97
c30c694e 98func stripZshEscaping(url: string): string = url.replace("\\", "")
d65a1dcf 99
b44b6494
JN
100
101func sanitizeURL*(url: string): string =
102 rewriteInvidiousToYouTube(urlLongen(stripZshEscaping(url)))
103
d65a1dcf 104
d36e2201 105proc directPlay*(url: string, player: string, options: Table[string, bool]) =
c12cc641
JN
106 let url =
107 if find(url, PEERTUBE_REGEX) != -1 and isInstalled("webtorrent"):
108 getPeerTubeMagnetLink(url)
109 else: url
d7688e97 110 if url.startswith("magnet:") or url.endswith(".torrent"):
d36e2201 111 if options["musicOnly"]:
8d06ad69 112 # TODO Replace with WebTorrent once it supports media player options
811928a7
JN
113 discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
114 else:
8d06ad69
JN
115 # WebTorrent is so much faster!
116 discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions)
fe1a5856 117 else:
d36e2201 118 play(player, options, url)
811928a7 119
b44b6494 120
811928a7
JN
121proc directDownload*(url: string, musicOnly: bool) =
122 let args =
123 if musicOnly: buildMusicDownloadArgs(url)
124 else: buildVideoDownloadArgs(url)
0c2f0385
JN
125 if isInstalled("aria2c"):
126 discard execShellCmd(&"youtube-dl {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'")
127 else:
128 discard execShellCmd(&"youtube-dl {args.join(\" \")}")
13a4017d 129
b44b6494 130
13a4017d
JN
131proc offerSelection(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange): string =
132 if options["feelingLucky"]: "0"
133 else:
134 presentVideoOptions(searchResults[selectionRange.begin .. selectionRange.until])
135 stdout.styledWrite(fgYellow, "Choose video number: ")
136 readLine(stdin)
137
b44b6494 138
13a4017d 139proc handleUserInput(searchResult: SearchResult, options: Table[string, bool], player: string) =
5f9cdfef
JN
140 if options["autoPlay"]:
141 play(player, options, searchResult.url, searchResult.title)
142 let nextResult = getAutoPlayVideo(searchResult)
143 handleUserInput(nextResult, options, player) # inifinite playlist till user quits
144 elif options["download"]:
13a4017d
JN
145 if options["musicOnly"]:
146 download(buildMusicDownloadArgs(searchResult.url), searchResult.title)
147 else:
148 download(buildVideoDownloadArgs(searchResult.url), searchResult.title)
149 else:
d36e2201 150 play(player, options, searchResult.url, searchResult.title)
13a4017d 151
b44b6494 152
9ed70b9e
JN
153proc isValidOptions*(options: Options): bool =
154 # Check for invalid combinations of options
5f9cdfef 155 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen"), ("download", "autoPlay")]
9ed70b9e
JN
156 result = true
157 for combination in invalidCombinations:
158 if options[combination[0]] and options[combination[1]]:
159 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
160 result = false
5f9cdfef
JN
161 # TODO Make this overridable in configuration
162 if options["autoPlay"] and not options["musicOnly"]:
163 stderr.writeLine "--music-only must be provided with --auto-play. This is to prevent binge-watching."
164 result = false
9ed70b9e
JN
165
166
167proc updateOptions(options: Options, newOptions: string): Options =
168 result = options
169
5f9cdfef 170 # Interactive options
9ed70b9e
JN
171 for option in newOptions:
172 case option
173 of 'm': result["musicOnly"] = true
174 of 'f': result["fullScreen"] = true
175 of 'd': result["download"] = true
5f9cdfef 176 of 'a': result["autoPlay"] = true
9ed70b9e 177 else:
5f9cdfef 178 stderr.writeLine "Invalid option provided!"
9ed70b9e
JN
179 quit(2)
180
181 if(not isValidOptions(result)):
182 quit(2)
183
184
13a4017d 185proc present*(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange, player: string) =
55b0cae7
JN
186 ##[ Continuously present options till the user quits the application
187
188 selectionRange: Currently available range to choose from depending on pagination
189 ]##
13a4017d
JN
190
191 let userInput = offerSelection(searchResults, options, selectionRange)
192
193 case userInput
194 of "all":
195 for selection in selectionRange.begin .. selectionRange.until:
196 handleUserInput(searchResults[selection], options, player)
197 quit(0)
198 of "n":
199 if selectionRange.until + 1 < len(searchResults):
200 let newSelectionRange = (selectionRange.until + 1, min(len(searchResults) - 1, selectionRange.until + limit))
201 present(searchResults, options, newSelectionRange, player)
ebae91b4
JN
202 else:
203 present(searchResults, options, selectionRange, player)
13a4017d
JN
204 of "p":
205 if selectionRange.begin > 0:
206 let newSelectionRange = (selectionRange.begin - limit, selectionRange.until - limit)
207 present(searchResults, options, newSelectionRange, player)
ebae91b4
JN
208 else:
209 present(searchResults, options, selectionRange, player)
13a4017d
JN
210 of "q":
211 quit(0)
212 else:
9ed70b9e
JN
213 if " " in userInput:
214 let selection = parseInt(userInput.split(" ")[0])
215 let updatedOptions = updateOptions(options, userInput.split(" ")[1])
216 let searchResult = searchResults[selectionRange.begin .. selectionRange.until][selection]
217 handleUserInput(searchResult, updatedOptions, player)
218 else:
219 let searchResult = searchResults[selectionRange.begin .. selectionRange.until][parseInt(userInput)]
220 handleUserInput(searchResult, options, player)
13a4017d
JN
221 if options["feelingLucky"]:
222 quit(0)
223 else:
224 present(searchResults, options, selectionRange, player)