]> njoseph.me Git - nimcoon.git/blame - clitube.nim
Add additional guard for directPlay
[nimcoon.git] / clitube.nim
CommitLineData
6ff2dbac
JN
1import
2 htmlparser,
3 httpClient,
ba35766a 4 logging,
6ff2dbac
JN
5 parseopt,
6 osproc,
7 sequtils,
8 sugar,
9 strformat,
10 std/[terminal],
11 strtabs,
12 strutils,
13 uri,
14 xmltree
44978125 15
2f0b8428 16import preferences
c760b5d9 17
121e06b2
JN
18type
19 SearchResult = tuple[title: string, url: string]
20 CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool]
2f3c8875 21
ba35766a
JN
22let logger = newConsoleLogger()
23setLogFilter(lvlInfo)
24
933e339b
JN
25proc selectMediaPlayer(): string =
26 let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
27 if len(availablePlayers) == 0:
b40b7243 28 stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}"
933e339b
JN
29 raise newException(OSError, "No supported media player found")
30 else:
31 return availablePlayers[0]
44978125 32
121e06b2
JN
33proc parseOptions(): CommandLineOptions =
34 var
35 searchQuery = ""
36 musicOnly = false
37 feelingLucky = false
38
39 for kind, key, value in getopt():
40 case kind
41 of cmdArgument:
42 searchQuery = key
43 of cmdShortOption, cmdLongOption:
44 case key
45 of "m", "music": musicOnly = true
46 of "l", "lucky": feelingLucky = true
47 of cmdEnd:
48 discard
49
50 return (searchQuery, musicOnly, feelingLucky)
51
39a495a9
JN
52proc getYoutubePage(searchQuery: string): string =
53 let queryParam = encodeUrl(searchQuery)
485b1053 54 let client = newHttpClient()
b40b7243 55 let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
485b1053 56 return $response.body
44978125 57
2f3c8875 58proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
485b1053 59 parseHtml(htmlFile).findAll("a").
44978125 60 filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
c760b5d9 61 map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)]
44978125 62
2f3c8875 63proc presentVideoOptions(searchResults: seq[SearchResult]) =
efcc0441 64 echo ""
2f3c8875 65 for index, (title, url) in searchResults:
3d5bf3fd 66 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
44978125 67
ba35766a
JN
68proc play(command: string) =
69 logger.log(lvlDebug, &"Executing: ${command}")
70 discard execProcess(command)
71 quit(0)
72
121e06b2 73proc directPlay(searchQuery: string, player: string) =
121e06b2 74 if "watch?" in searchQuery or "videos/watch" in searchQuery :
ba35766a 75 play(&"{player} {searchQuery}")
121e06b2 76 elif searchQuery.startswith("magnet:"):
ba35766a 77 play(&"peerflix \"{searchQuery}\" --{player}")
121e06b2
JN
78
79proc main() =
80 let
81 player = selectMediaPlayer()
82 (searchQuery, musicOnly, feelingLucky) = parseOptions()
83
92f80e5a
JN
84 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
85 directPlay(searchQuery, player)
121e06b2
JN
86
87 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
88
89 let number =
90 if feelingLucky: 0
91 else:
92 presentVideoOptions(searchResults)
93 stdout.styledWrite(fgYellow, "Choose video number: ")
94 parseInt(readLine(stdin))
95
96 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
efcc0441 97
121e06b2 98 var command = @[player, searchResults[number].url]
efcc0441 99
121e06b2
JN
100 if musicOnly:
101 command.add("--no-video")
a2319a3f 102
121e06b2 103 # Play the video using the preferred/available media player
ba35766a 104 play(command.join(" "))
a2319a3f 105
121e06b2 106main()