]>
Commit | Line | Data |
---|---|---|
44978125 JN |
1 | import htmlparser |
2 | import httpClient | |
3 | import os | |
4 | import osproc | |
5 | import strtabs | |
6 | import strutils | |
7 | import uri | |
8 | import xmltree | |
9 | import sequtils, sugar | |
10 | ||
11 | # TODO Pretty colors in terminal | |
12 | # import terminal | |
13 | ||
14 | # Supported video players in order of preference | |
15 | # TODO Should go into a config file | |
39a495a9 | 16 | let supportedPlayers = @["mpv", "mplayer", "vlc"] |
44978125 | 17 | |
39a495a9 | 18 | proc findSupportedPlayer(): string = |
44978125 | 19 | for player in supported_players: |
39a495a9 JN |
20 | let playerBin = execProcess("which " & player) |
21 | if playerBin.len != 0: | |
22 | return strip(playerBin) | |
44978125 | 23 | |
39a495a9 JN |
24 | proc getYoutubePage(searchQuery: string): string = |
25 | let queryParam = encodeUrl(searchQuery) | |
44978125 | 26 | var client = newHttpClient() |
39a495a9 | 27 | let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & queryParam) |
44978125 JN |
28 | # TODO Get rid of temp file or make one temp file per user |
29 | writeFile("/tmp/cli-tube-page.html", response.body) | |
30 | return "/tmp/cli-tube-page.html" | |
31 | ||
39a495a9 JN |
32 | proc extractTitlesAndUrls(htmlFile: string): seq[tuple[title: string, url: string]] = |
33 | loadHtml(htmlFile).findAll("a"). | |
44978125 JN |
34 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). |
35 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"])) | |
36 | ||
39a495a9 JN |
37 | proc presentVideoOptions(titlesAndUrls: seq[tuple[title: string, url: string]]) = |
38 | for number, (title, url) in titlesAndUrls: | |
44978125 JN |
39 | echo number, ". ", title, "\n", url, "\n" |
40 | ||
39a495a9 JN |
41 | presentVideoOptions( |
42 | extractTitlesAndUrls( | |
43 | getYoutubePage( | |
44978125 | 44 | paramStr(1)))) |