From: Joseph Nuthalapati Date: Tue, 3 Dec 2019 17:32:44 +0000 (+0530) Subject: Get rid of temp HTML file X-Git-Tag: 0.1.0~49 X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/commitdiff_plain/485b105320ea9074fc53bdf5967704b09e20c5cb Get rid of temp HTML file Signed-off-by: Joseph Nuthalapati --- diff --git a/TODO.org b/TODO.org index 4481a37..cb9d33a 100644 --- a/TODO.org +++ b/TODO.org @@ -5,5 +5,5 @@ * DONE Pretty colors in terminal * TODO Preferences should go into a config file * TODO Terminal color themes? -* TODO Get rid of temp file or make one temp file per user +* DONE Get rid of temp file or make one temp file per user * TODO Use cligen to create a proper CLI diff --git a/clitube.nim b/clitube.nim index 95c83a5..121b3d4 100644 --- a/clitube.nim +++ b/clitube.nim @@ -10,7 +10,7 @@ import uri import xmltree # Supported video players in order of preference -let supportedPlayers = @["mpv", "vlc"] +let supportedPlayers = ["mpv", "vlc"] # Only show these many results let limit = 10 @@ -27,13 +27,12 @@ proc selectMediaPlayer(): string = proc getYoutubePage(searchQuery: string): string = let queryParam = encodeUrl(searchQuery) - var client = newHttpClient() + let client = newHttpClient() let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & queryParam) - writeFile("/tmp/clitube-page.html", response.body) - return "/tmp/clitube-page.html" + return $response.body proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] = - loadHtml(htmlFile).findAll("a"). + 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)] @@ -54,7 +53,7 @@ let searchResults = extractTitlesAndUrls(getYoutubePage(input)) presentVideoOptions(searchResults) stdout.styledWrite(fgYellow, "Choose video number: ") -var number: int = parseInt(readLine(stdin)) +let number: int = parseInt(readLine(stdin)) styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title