]> njoseph.me Git - nimcoon.git/blob - src/nimcoon.nim
e1cbb23bf562bf03cd851fd5b6a67f7d10a2cb94
[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, "non-interactive": 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 "n", "non-interactive": options["non-interactive"] = true
30 of cmdEnd: discard
31
32 if searchQuery == "":
33 stderr.writeLine "NimCoon doesn't permit browsing. You must provide a search query."
34 quit(1)
35
36 (searchQuery, options)
37
38
39 proc isValidOptions(options: Options): bool =
40 # Check for invalid combinations of options
41 var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")]
42 result = true
43 for combination in invalidCombinations:
44 if options[combination[0]] and options[combination[1]]:
45 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
46 result = false
47
48 proc main() =
49 let
50 player = selectMediaPlayer()
51 (searchQuery, options) = parseArguments()
52
53 if(not isValidOptions(options)):
54 quit(1)
55
56 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
57 if options["download"]:
58 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
59 else:
60 directPlay(sanitizeURL(searchQuery), player, options)
61 quit(0)
62
63 let searchResults = getSearchResults(searchQuery)
64 if options["non-interactive"]:
65 for index, (title, url) in searchResults:
66 echo title
67 echo url
68 echo ""
69 quit(0)
70
71 let numResults = min(limit, len(searchResults))
72
73 present(searchResults, options, (0, numResults-1), player)
74
75
76 when isMainModule:
77 main()