]> njoseph.me Git - nimcoon.git/blob - src/nimcoon.nim
Fix all recent bugs. Shift to Invidious API.
[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
16 var
17 searchQuery = ""
18 options = to_table({
19 "musicOnly": false,
20 "feelingLucky": false,
21 "fullScreen": false,
22 "download": false,
23 "non-interactive": false
24 })
25
26 for kind, key, value in getopt():
27 case kind
28 of cmdArgument:
29 searchQuery = key
30 of cmdShortOption, cmdLongOption:
31 case key
32 of "m", "music": options["musicOnly"] = true
33 of "l", "lucky": options["feelingLucky"] = true
34 of "f", "full-screen": options["fullScreen"] = true
35 of "d", "download": options["download"] = true
36 of "n", "non-interactive": options["non-interactive"] = true
37 of cmdEnd: discard
38
39 if searchQuery == "":
40 stderr.writeLine "NimCoon doesn't permit browsing. You must provide a search query."
41 quit(1)
42
43 (searchQuery, options)
44
45
46 proc isValidOptions*(options: Options): bool =
47 # Check for invalid combinations of options
48 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
49 result = true
50 for combination in invalidCombinations:
51 if options[combination[0]] and options[combination[1]]:
52 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
53 result = false
54
55
56 proc main() =
57 let
58 player = selectMediaPlayer()
59 (searchQuery, options) = parseArguments()
60
61 if(not isValidOptions(options)):
62 quit(1)
63
64 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
65 if options["download"]:
66 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
67 else:
68 directPlay(sanitizeURL(searchQuery), player, options)
69 quit(0)
70
71 let searchResults = getSearchResults(searchQuery)
72 if options["non-interactive"]:
73 for index, (title, url) in searchResults:
74 echo title
75 echo url
76 echo ""
77 quit(0)
78
79 let numResults = min(limit, len(searchResults))
80
81 present(searchResults, options, (0, numResults-1), player)
82
83
84 when isMainModule:
85 main()