]> njoseph.me Git - nimcoon.git/blame - src/nimcoon.nim
Fix all recent bugs. Shift to Invidious API.
[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 =
a2d28598 15
121e06b2
JN
16 var
17 searchQuery = ""
a2d28598
JN
18 options = to_table({
19 "musicOnly": false,
20 "feelingLucky": false,
21 "fullScreen": false,
22 "download": false,
23 "non-interactive": false
24 })
121e06b2
JN
25
26 for kind, key, value in getopt():
27 case kind
28 of cmdArgument:
29 searchQuery = key
30 of cmdShortOption, cmdLongOption:
31 case key
6f161e0b
JN
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
26229fac 36 of "n", "non-interactive": options["non-interactive"] = true
6f161e0b
JN
37 of cmdEnd: discard
38
26229fac
JN
39 if searchQuery == "":
40 stderr.writeLine "NimCoon doesn't permit browsing. You must provide a search query."
41 quit(1)
42
55b0cae7 43 (searchQuery, options)
121e06b2 44
6f161e0b 45
fabf0294 46proc isValidOptions*(options: Options): bool =
6f161e0b 47 # Check for invalid combinations of options
95683732 48 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
86e6cb72 49 result = true
6f161e0b
JN
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]}"
86e6cb72 53 result = false
121e06b2 54
a2d28598 55
121e06b2
JN
56proc main() =
57 let
58 player = selectMediaPlayer()
6f161e0b
JN
59 (searchQuery, options) = parseArguments()
60
61 if(not isValidOptions(options)):
62 quit(1)
121e06b2 63
25f5a034 64 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
9a8ef4ad 65 if options["download"]:
811928a7 66 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
9a8ef4ad 67 else:
d36e2201 68 directPlay(sanitizeURL(searchQuery), player, options)
9e6b8568 69 quit(0)
121e06b2 70
e08e5cbe 71 let searchResults = getSearchResults(searchQuery)
26229fac
JN
72 if options["non-interactive"]:
73 for index, (title, url) in searchResults:
74 echo title
75 echo url
76 echo ""
77 quit(0)
78
fac67037 79 let numResults = min(limit, len(searchResults))
121e06b2 80
fac67037 81 present(searchResults, options, (0, numResults-1), player)
d807245d 82
a2319a3f 83
d65a1dcf
JN
84when isMainModule:
85 main()