X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/f7735b43cbeb05c3e30cc9d2d0df3197fa1c0eb9..d807245d4d9041d9098ea7bb273debfb0224d0ff:/nimcoon.nim diff --git a/nimcoon.nim b/nimcoon.nim index c838b34..ae22576 100644 --- a/nimcoon.nim +++ b/nimcoon.nim @@ -1,35 +1,9 @@ import - htmlparser, - httpClient, - logging, parseopt, - osproc, - sequtils, - sugar, - strformat, std/[terminal], - strtabs, - strutils, - tables, - uri, - xmltree + strutils -import config - -type - SearchResult = tuple[title: string, url: string] - CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool] - -let logger = newConsoleLogger() -setLogFilter(lvlInfo) - -proc selectMediaPlayer(): string = - let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) - if len(availablePlayers) == 0: - stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" - raise newException(OSError, "No supported media player found") - else: - return availablePlayers[0] +import lib proc parseOptions(): CommandLineOptions = var @@ -52,32 +26,6 @@ proc parseOptions(): CommandLineOptions = return (searchQuery, musicOnly, feelingLucky, fullScreen) -proc getYoutubePage(searchQuery: string): string = - let queryParam = encodeUrl(searchQuery) - let client = newHttpClient() - let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}") - return $response.body - -proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] = - parseHtml(htmlFile).findAll("a"). - filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). - map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)] - -proc presentVideoOptions(searchResults: seq[SearchResult]) = - echo "" - for index, (title, url) in searchResults: - styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" - -proc play(command: string) = - logger.log(lvlDebug, &"Executing: ${command}") - discard execProcess(command) - quit(0) - -proc directPlay(searchQuery: string, player: string) = - if "watch?" in searchQuery or "videos/watch" in searchQuery : - play(&"{player} {searchQuery}") - elif searchQuery.startswith("magnet:"): - play(&"peerflix \"{searchQuery}\" --{player}") proc main() = let @@ -89,24 +37,33 @@ proc main() = let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) - let number = - if feelingLucky: 0 + proc getUserInput(): string = + if feelingLucky: "0" else: presentVideoOptions(searchResults) stdout.styledWrite(fgYellow, "Choose video number: ") - parseInt(readLine(stdin)) + readLine(stdin) - styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title + # This is a pure function with no side effects + func buildArgs(number: int): seq[string] = + var args = @[searchResults[number].url] + if musicOnly: args.add("--no-video") + if fullScreen: args.add("--fullscreen") + return args - var command = @[player, searchResults[number].url] + while(true): + let userInput = getUserInput() + if userInput == "q": + break - if musicOnly: - command.add(playerOptions[player]["musicOnly"]) + let videoNumber = parseInt(userInput) + styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[videoNumber].title - if fullScreen: - command.add(playerOptions[player]["fullScreen"]) + # Play the video using the preferred/available media player + play(player, buildArgs(videoNumber)) + if feelingLucky: + break - # Play the video using the preferred/available media player - play(command.join(" ")) -main() +when isMainModule: + main()