From 485b105320ea9074fc53bdf5967704b09e20c5cb Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Tue, 3 Dec 2019 23:02:44 +0530 Subject: [PATCH] Get rid of temp HTML file Signed-off-by: Joseph Nuthalapati --- TODO.org | 2 +- clitube.nim | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) 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 -- 2.43.0