]> njoseph.me Git - nimcoon.git/blame - src/nimcoon.nim
Remove dependency on Invidious
[nimcoon.git] / src / nimcoon.nim
CommitLineData
6ff2dbac 1import
6ff2dbac 2 parseopt,
6f161e0b
JN
3 strformat,
4 strutils,
5 tables
44978125 6
e08e5cbe
JN
7import
8 config,
9 lib,
10 types,
11 youtube
44978125 12
6f161e0b
JN
13
14proc parseArguments(): CommandLineOptions =
121e06b2
JN
15 var
16 searchQuery = ""
6f161e0b 17 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
121e06b2
JN
18
19 for kind, key, value in getopt():
20 case kind
21 of cmdArgument:
22 searchQuery = key
23 of cmdShortOption, cmdLongOption:
24 case key
6f161e0b
JN
25 of "m", "music": options["musicOnly"] = true
26 of "l", "lucky": options["feelingLucky"] = true
27 of "f", "full-screen": options["fullScreen"] = true
28 of "d", "download": options["download"] = true
29 of cmdEnd: discard
30
55b0cae7 31 (searchQuery, options)
121e06b2 32
6f161e0b
JN
33
34proc isValidOptions*(options: Options): bool =
35 # Check for invalid combinations of options
95683732 36 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
86e6cb72 37 result = true
6f161e0b
JN
38 for combination in invalidCombinations:
39 if options[combination[0]] and options[combination[1]]:
40 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
86e6cb72 41 result = false
121e06b2 42
121e06b2
JN
43proc main() =
44 let
45 player = selectMediaPlayer()
6f161e0b
JN
46 (searchQuery, options) = parseArguments()
47
48 if(not isValidOptions(options)):
49 quit(1)
121e06b2 50
25f5a034 51 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
9a8ef4ad 52 if options["download"]:
811928a7 53 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
9a8ef4ad 54 else:
d36e2201 55 directPlay(sanitizeURL(searchQuery), player, options)
9e6b8568 56 quit(0)
121e06b2 57
e08e5cbe 58 let searchResults = getSearchResults(searchQuery)
fac67037 59 let numResults = min(limit, len(searchResults))
121e06b2 60
fac67037 61 present(searchResults, options, (0, numResults-1), player)
d807245d 62
a2319a3f 63
d65a1dcf
JN
64when isMainModule:
65 main()