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