]> njoseph.me Git - nimcoon.git/blob - src/nimcoon.nim
Remove dependency on Invidious
[nimcoon.git] / src / nimcoon.nim
1 import
2 parseopt,
3 strformat,
4 strutils,
5 tables
6
7 import
8 config,
9 lib,
10 types,
11 youtube
12
13
14 proc parseArguments(): CommandLineOptions =
15 var
16 searchQuery = ""
17 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
18
19 for kind, key, value in getopt():
20 case kind
21 of cmdArgument:
22 searchQuery = key
23 of cmdShortOption, cmdLongOption:
24 case key
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
31 (searchQuery, options)
32
33
34 proc isValidOptions*(options: Options): bool =
35 # Check for invalid combinations of options
36 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
37 result = true
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]}"
41 result = false
42
43 proc main() =
44 let
45 player = selectMediaPlayer()
46 (searchQuery, options) = parseArguments()
47
48 if(not isValidOptions(options)):
49 quit(1)
50
51 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
52 if options["download"]:
53 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
54 else:
55 directPlay(sanitizeURL(searchQuery), player, options)
56 quit(0)
57
58 let searchResults = getSearchResults(searchQuery)
59 let numResults = min(limit, len(searchResults))
60
61 present(searchResults, options, (0, numResults-1), player)
62
63
64 when isMainModule:
65 main()