]> njoseph.me Git - nimcoon.git/commitdiff
Add limit option with default set to 10 results
authorJoseph Nuthalapati <njoseph@riseup.net>
Thu, 28 Nov 2019 17:41:57 +0000 (23:11 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Thu, 28 Nov 2019 17:41:57 +0000 (23:11 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
TODO.org
clitube.nim

index 0377b8966d949612814e808e615b0c8ce312d8fd..a34f276a9b0e666e5ce129acc09d25414a711573 100644 (file)
--- a/TODO.org
+++ b/TODO.org
@@ -5,3 +5,4 @@
 * TODO 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
index a1ba868a8c0d5c45e045a4b6885a153c7770a451..b6dda43787b43bab99ca04b2d33816f45ffac20d 100644 (file)
@@ -11,6 +11,9 @@ import xmltree
 # Supported video players in order of preference
 let supportedPlayers = @["mpv", "mplayer", "vlc"]
 
+# Only show these many results
+let limit = 10
+
 # Hard-coded terminal colors
 proc reset(): string {.procvar.} = "\e[0m"
 proc bold*(s: string): string {.procvar.} = "\e[1m" & s & reset()
@@ -32,16 +35,16 @@ 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=" & 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 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"]))
+    map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)]
 
 proc presentVideoOptions(searchResults: seq[SearchResult]) =
+  echo "\n"
   for index, (title, url) in searchResults:
     echo index, ". ", title.bold.fgMagenta, "\n", url.fgCyan, "\n"