X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/blobdiff_plain/44978125178962c833bed6f53853b74863a93c99..2f3c887592776c90e4f7c2197d0cd44c137b80d5:/cli_tube.nim diff --git a/cli_tube.nim b/cli_tube.nim index d7a3d91..ccf09ae 100644 --- a/cli_tube.nim +++ b/cli_tube.nim @@ -2,43 +2,45 @@ import htmlparser import httpClient import os import osproc +import sequtils, sugar import strtabs import strutils import uri import xmltree -import sequtils, sugar # TODO Pretty colors in terminal # import terminal # Supported video players in order of preference # TODO Should go into a config file -let supported_players = @["mpv", "mplayer", "vlc"] +let supportedPlayers = @["mpv", "mplayer", "vlc"] + +type SearchResult = tuple[title: string, url: string] -proc find_supported_player(): string = +proc findSupportedPlayer(): string = for player in supported_players: - let player_bin = execProcess("which " & player) - if player_bin.len != 0: - return strip(player_bin) + let playerBin = execProcess("which " & player) + if playerBin.len != 0: + return strip(playerBin) -proc get_youtube_page(search_query: string): string = - let query_param = encodeUrl(search_query) +proc getYoutubePage(searchQuery: string): string = + let queryParam = encodeUrl(searchQuery) var client = newHttpClient() - let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & query_param) + let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & queryParam) # TODO Get rid of temp file or make one temp file per user writeFile("/tmp/cli-tube-page.html", response.body) return "/tmp/cli-tube-page.html" -proc extract_titles_and_urls(html_file: string): seq[tuple[title: string, url: string]] = - loadHtml(html_file).findAll("a"). +proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] = + loadHtml(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"])) -proc present_video_options(titles_and_urls: seq[tuple[title: string, url: string]]) = - for number, (title, url) in titles_and_urls: - echo number, ". ", title, "\n", url, "\n" +proc presentVideoOptions(searchResults: seq[SearchResult]) = + for index, (title, url) in searchResults: + echo index, ". ", title, "\n", url, "\n" -present_video_options( - extract_titles_and_urls( - get_youtube_page( +presentVideoOptions( + extractTitlesAndUrls( + getYoutubePage( paramStr(1))))