]>
Commit | Line | Data |
---|---|---|
44978125 JN |
1 | import htmlparser |
2 | import httpClient | |
3 | import os | |
4 | import osproc | |
2f3c8875 | 5 | import sequtils, sugar |
3d5bf3fd | 6 | import std/[terminal] |
44978125 JN |
7 | import strtabs |
8 | import strutils | |
9 | import uri | |
10 | import xmltree | |
44978125 | 11 | |
44978125 | 12 | # Supported video players in order of preference |
485b1053 | 13 | let supportedPlayers = ["mpv", "vlc"] |
44978125 | 14 | |
c760b5d9 JN |
15 | # Only show these many results |
16 | let limit = 10 | |
17 | ||
2f3c8875 JN |
18 | type SearchResult = tuple[title: string, url: string] |
19 | ||
933e339b JN |
20 | proc selectMediaPlayer(): string = |
21 | let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) | |
22 | if len(availablePlayers) == 0: | |
833bfdd4 | 23 | stderr.writeLine "Please install one of the supported media players: ", $supportedPlayers |
933e339b JN |
24 | raise newException(OSError, "No supported media player found") |
25 | else: | |
26 | return availablePlayers[0] | |
44978125 | 27 | |
39a495a9 JN |
28 | proc getYoutubePage(searchQuery: string): string = |
29 | let queryParam = encodeUrl(searchQuery) | |
485b1053 | 30 | let client = newHttpClient() |
39a495a9 | 31 | let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & queryParam) |
485b1053 | 32 | return $response.body |
44978125 | 33 | |
2f3c8875 | 34 | proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] = |
485b1053 | 35 | parseHtml(htmlFile).findAll("a"). |
44978125 | 36 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). |
c760b5d9 | 37 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)] |
44978125 | 38 | |
2f3c8875 | 39 | proc presentVideoOptions(searchResults: seq[SearchResult]) = |
efcc0441 | 40 | echo "" |
2f3c8875 | 41 | for index, (title, url) in searchResults: |
3d5bf3fd | 42 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" |
44978125 | 43 | |
84d84780 JN |
44 | let input = paramStr(1) |
45 | let player = selectMediaPlayer() | |
46 | ||
47 | if "https://www.youtube.com" in input: | |
48 | discard execProcess(player & " " & input) | |
49 | quit(0) | |
50 | ||
51 | let searchResults = extractTitlesAndUrls(getYoutubePage(input)) | |
efcc0441 JN |
52 | |
53 | presentVideoOptions(searchResults) | |
54 | ||
3d5bf3fd | 55 | stdout.styledWrite(fgYellow, "Choose video number: ") |
485b1053 | 56 | let number: int = parseInt(readLine(stdin)) |
efcc0441 | 57 | |
3d5bf3fd | 58 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title |
efcc0441 | 59 | |
3d5bf3fd | 60 | # Play the video using the preferred/available media player |
efcc0441 | 61 | discard execProcess(player & " " & searchResults[number].url) |