19 processOptions = {poStdErrToStdOut, poUsePath} # poEchoCmd can be added to options for debugging
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}"
23 proc isInstalled(program: string): bool =
24 execProcess("which " & program).len != 0
27 proc selectMediaPlayer*(): string =
28 let availablePlayers = supportedPlayers.filter(isInstalled)
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")
33 return availablePlayers[0]
36 proc getPeerTubeMagnetLink(url: string): string =
37 ## Gets the magnet link of the best possible resolution from PeerTube
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()
47 proc presentVideoOptions*(searchResults: SearchResults) =
49 for index, (title, url) in searchResults:
50 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, " ", url, "\n"
52 func buildPlayerArgs(url: string, options: Table[string, bool], player: string): seq[string] =
53 let musicOnly = if options["musicOnly"]: "--no-video" else: ""
54 let fullScreen = if options["fullScreen"]: "--fullscreen" else: ""
55 filterIt([url, musicOnly, fullScreen], it != "")
58 proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
59 let args = buildPlayerArgs(url, options, player)
61 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
62 if "--no-video" in args:
63 discard execShellCmd(&"{player} {args.join(\" \")}")
65 discard startProcess(player, args=args, options=processOptions)
68 func buildMusicDownloadArgs(url: string): seq[string] =
70 let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'"
71 @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3",
72 "--audio-quality", "0", "-o", downloadLocation, url]
75 func buildVideoDownloadArgs(url: string): seq[string] =
77 let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'"
78 @["-f", "best", "-o", downloadLocation, url]
81 proc download*(args: openArray[string], title: string) =
82 styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
83 discard execShellCmd(&"youtube-dl {args.join(\" \")}")
86 func urlLongen(url: string): string = url.replace("youtu.be/", "www.youtube.com/watch?v=")
89 func rewriteInvidiousToYouTube(url: string): string =
91 if rewriteInvidiousURLs: url.replace("invidio.us", "www.youtube.com") else: url
94 func stripZshEscaping(url: string): string = url.replace("\\", "")
97 func sanitizeURL*(url: string): string =
98 rewriteInvidiousToYouTube(urlLongen(stripZshEscaping(url)))
101 proc directPlay*(url: string, player: string, options: Table[string, bool]) =
103 if find(url, PEERTUBE_REGEX) != -1 and isInstalled("webtorrent"):
104 getPeerTubeMagnetLink(url)
106 if url.startswith("magnet:") or url.endswith(".torrent"):
107 if options["musicOnly"]:
108 # TODO Replace with WebTorrent once it supports media player options
109 discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
111 # WebTorrent is so much faster!
112 discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions)
114 play(player, options, url)
117 proc directDownload*(url: string, musicOnly: bool) =
119 if musicOnly: buildMusicDownloadArgs(url)
120 else: buildVideoDownloadArgs(url)
121 if isInstalled("aria2c"):
122 discard execShellCmd(&"youtube-dl {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'")
124 discard execShellCmd(&"youtube-dl {args.join(\" \")}")
127 proc offerSelection(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange): string =
128 if options["feelingLucky"]: "0"
130 presentVideoOptions(searchResults[selectionRange.begin .. selectionRange.until])
131 stdout.styledWrite(fgYellow, "Choose video number: ")
135 proc handleUserInput(searchResult: SearchResult, options: Table[string, bool], player: string) =
136 if options["download"]:
137 if options["musicOnly"]:
138 download(buildMusicDownloadArgs(searchResult.url), searchResult.title)
140 download(buildVideoDownloadArgs(searchResult.url), searchResult.title)
142 play(player, options, searchResult.url, searchResult.title)
145 proc present*(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange, player: string) =
146 ##[ Continuously present options till the user quits the application
148 selectionRange: Currently available range to choose from depending on pagination
151 let userInput = offerSelection(searchResults, options, selectionRange)
155 for selection in selectionRange.begin .. selectionRange.until:
156 handleUserInput(searchResults[selection], options, player)
159 if selectionRange.until + 1 < len(searchResults):
160 let newSelectionRange = (selectionRange.until + 1, min(len(searchResults) - 1, selectionRange.until + limit))
161 present(searchResults, options, newSelectionRange, player)
163 present(searchResults, options, selectionRange, player)
165 if selectionRange.begin > 0:
166 let newSelectionRange = (selectionRange.begin - limit, selectionRange.until - limit)
167 present(searchResults, options, newSelectionRange, player)
169 present(searchResults, options, selectionRange, player)
173 let searchResult = searchResults[selectionRange.begin .. selectionRange.until][parseInt(userInput)]
174 handleUserInput(searchResult, options, player)
175 if options["feelingLucky"]:
178 present(searchResults, options, selectionRange, player)