Bump version and update changelog
[nimcoon.git] / src / nimcoon.nim
CommitLineData
6ff2dbac 1import
6ff2dbac 2 parseopt,
6f161e0b
JN
3 strutils,
4 tables
44978125 5
e08e5cbe
JN
6import
7 config,
8 lib,
9 types,
10 youtube
44978125 11
6f161e0b
JN
12
13proc parseArguments(): CommandLineOptions =
a2d28598 14
121e06b2
JN
15 var
16 searchQuery = ""
a2d28598
JN
17 options = to_table({
18 "musicOnly": false,
19 "feelingLucky": false,
20 "fullScreen": false,
21 "download": false,
5f9cdfef
JN
22 "nonInteractive": false,
23 "autoPlay": false
a2d28598 24 })
121e06b2 25
5f9cdfef 26 # Non-interactive/Global options
121e06b2
JN
27 for kind, key, value in getopt():
28 case kind
29 of cmdArgument:
30 searchQuery = key
31 of cmdShortOption, cmdLongOption:
32 case key
6f161e0b
JN
33 of "m", "music": options["musicOnly"] = true
34 of "l", "lucky": options["feelingLucky"] = true
35 of "f", "full-screen": options["fullScreen"] = true
36 of "d", "download": options["download"] = true
5f9cdfef
JN
37 of "n", "non-interactive": options["nonInteractive"] = true
38 of "a", "auto-play": options["autoPlay"] = true
6f161e0b
JN
39 of cmdEnd: discard
40
26229fac 41 if searchQuery == "":
81fb030b 42 stderr.writeLine "Nimcoon doesn't permit browsing. You must provide a search query."
26229fac
JN
43 quit(1)
44
55b0cae7 45 (searchQuery, options)
121e06b2 46
6f161e0b 47
121e06b2
JN
48proc main() =
49 let
50 player = selectMediaPlayer()
6f161e0b
JN
51 (searchQuery, options) = parseArguments()
52
53 if(not isValidOptions(options)):
54 quit(1)
121e06b2 55
25f5a034 56 if searchQuery.startswith("http") or searchQuery.startswith("magnet"):
9a8ef4ad 57 if options["download"]:
6697cfd2 58 directDownload(sanitizeURL(searchQuery), options)
9a8ef4ad 59 else:
d36e2201 60 directPlay(sanitizeURL(searchQuery), player, options)
9e6b8568 61 quit(0)
121e06b2 62
6697cfd2
JN
63 # Take a shortcut and search directly with youtube-dl
64 if options["feelingLucky"]:
65 if options["download"]: luckyDownload(searchQuery, options)
66 else: luckyPlay(searchQuery, player, options)
67 quit(0)
68
e08e5cbe 69 let searchResults = getSearchResults(searchQuery)
6697cfd2 70 if options["nonInteractive"]: # Present in machine-readable format
26229fac
JN
71 for index, (title, url) in searchResults:
72 echo title
73 echo url
74 echo ""
75 quit(0)
76
fac67037 77 let numResults = min(limit, len(searchResults))
121e06b2 78
fac67037 79 present(searchResults, options, (0, numResults-1), player)
d807245d 80
a2319a3f 81
d65a1dcf
JN
82when isMainModule:
83 main()