Get rid of temp HTML file
authorJoseph Nuthalapati <njoseph@riseup.net>
Tue, 3 Dec 2019 17:32:44 +0000 (23:02 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Tue, 3 Dec 2019 17:33:44 +0000 (23:03 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
TODO.org
clitube.nim

index 4481a3721628860ce7849fffc3897896ee35ad35..cb9d33a01553238a7541a9286a186901d8c7fccf 100644 (file)
--- 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
index 95c83a5804c988669d878b1e99ac13e576606470..121b3d4bc79dc7ab4cc7da2e181899acafca61c3 100644 (file)
@@ -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