From 2f3c887592776c90e4f7c2197d0cd44c137b80d5 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Sat, 5 Oct 2019 13:36:39 +0530 Subject: [PATCH] Use a user-defined type for search results Signed-off-by: Joseph Nuthalapati --- cli_tube.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cli_tube.nim b/cli_tube.nim index 2c29825..ccf09ae 100644 --- a/cli_tube.nim +++ b/cli_tube.nim @@ -2,11 +2,11 @@ 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 @@ -15,6 +15,8 @@ import sequtils, sugar # TODO Should go into a config file let supportedPlayers = @["mpv", "mplayer", "vlc"] +type SearchResult = tuple[title: string, url: string] + proc findSupportedPlayer(): string = for player in supported_players: let playerBin = execProcess("which " & player) @@ -29,14 +31,14 @@ proc getYoutubePage(searchQuery: string): string = writeFile("/tmp/cli-tube-page.html", response.body) return "/tmp/cli-tube-page.html" -proc extractTitlesAndUrls(htmlFile: string): seq[tuple[title: string, url: string]] = +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 presentVideoOptions(titlesAndUrls: seq[tuple[title: string, url: string]]) = - for number, (title, url) in titlesAndUrls: - echo number, ". ", title, "\n", url, "\n" +proc presentVideoOptions(searchResults: seq[SearchResult]) = + for index, (title, url) in searchResults: + echo index, ". ", title, "\n", url, "\n" presentVideoOptions( extractTitlesAndUrls( -- 2.43.0