Rename to Nim Coon
[nimcoon.git] / nimcoon.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]
d1e4d2de 20 CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: 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
d1e4d2de 38 fullScreen = false
121e06b2
JN
39
40 for kind, key, value in getopt():
41 case kind
42 of cmdArgument:
43 searchQuery = key
44 of cmdShortOption, cmdLongOption:
45 case key
46 of "m", "music": musicOnly = true
47 of "l", "lucky": feelingLucky = true
d1e4d2de 48 of "f", "full-screen": fullScreen = true
121e06b2
JN
49 of cmdEnd:
50 discard
51
d1e4d2de 52 return (searchQuery, musicOnly, feelingLucky, fullScreen)
121e06b2 53
39a495a9
JN
54proc getYoutubePage(searchQuery: string): string =
55 let queryParam = encodeUrl(searchQuery)
485b1053 56 let client = newHttpClient()
b40b7243 57 let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
485b1053 58 return $response.body
44978125 59
2f3c8875 60proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
485b1053 61 parseHtml(htmlFile).findAll("a").
44978125 62 filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
c760b5d9 63 map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)]
44978125 64
2f3c8875 65proc presentVideoOptions(searchResults: seq[SearchResult]) =
efcc0441 66 echo ""
2f3c8875 67 for index, (title, url) in searchResults:
3d5bf3fd 68 styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
44978125 69
ba35766a
JN
70proc play(command: string) =
71 logger.log(lvlDebug, &"Executing: ${command}")
72 discard execProcess(command)
73 quit(0)
74
121e06b2 75proc directPlay(searchQuery: string, player: string) =
121e06b2 76 if "watch?" in searchQuery or "videos/watch" in searchQuery :
ba35766a 77 play(&"{player} {searchQuery}")
121e06b2 78 elif searchQuery.startswith("magnet:"):
ba35766a 79 play(&"peerflix \"{searchQuery}\" --{player}")
121e06b2
JN
80
81proc main() =
82 let
83 player = selectMediaPlayer()
d1e4d2de 84 (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions()
121e06b2 85
92f80e5a
JN
86 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
87 directPlay(searchQuery, player)
121e06b2
JN
88
89 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
90
91 let number =
92 if feelingLucky: 0
93 else:
94 presentVideoOptions(searchResults)
95 stdout.styledWrite(fgYellow, "Choose video number: ")
96 parseInt(readLine(stdin))
97
98 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
efcc0441 99
121e06b2 100 var command = @[player, searchResults[number].url]
efcc0441 101
121e06b2
JN
102 if musicOnly:
103 command.add("--no-video")
a2319a3f 104
d1e4d2de
JN
105 if fullScreen:
106 if player == "cvlc":
107 command.add("--fullscreen")
108 if player == "mpv":
109 command.add("--fs")
110
121e06b2 111 # Play the video using the preferred/available media player
ba35766a 112 play(command.join(" "))
a2319a3f 113
121e06b2 114main()