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