]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | parseopt, | |
3 | strutils, | |
4 | tables | |
5 | ||
6 | import | |
7 | config, | |
8 | lib, | |
9 | types, | |
10 | youtube | |
11 | ||
12 | ||
13 | proc parseArguments(): CommandLineOptions = | |
14 | ||
15 | var | |
16 | searchQuery = "" | |
17 | options = to_table({ | |
18 | "musicOnly": false, | |
19 | "feelingLucky": false, | |
20 | "fullScreen": false, | |
21 | "download": false, | |
22 | "nonInteractive": false, | |
23 | "autoPlay": false | |
24 | }) | |
25 | ||
26 | # Non-interactive/Global options | |
27 | for kind, key, value in getopt(): | |
28 | case kind | |
29 | of cmdArgument: | |
30 | searchQuery = key | |
31 | of cmdShortOption, cmdLongOption: | |
32 | case key | |
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 | |
37 | of "n", "non-interactive": options["nonInteractive"] = true | |
38 | of "a", "auto-play": options["autoPlay"] = true | |
39 | of cmdEnd: discard | |
40 | ||
41 | if searchQuery == "": | |
42 | stderr.writeLine "Nimcoon doesn't permit browsing. You must provide a search query." | |
43 | quit(1) | |
44 | ||
45 | (searchQuery, options) | |
46 | ||
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) | |
59 | else: | |
60 | directPlay(sanitizeURL(searchQuery), player, options) | |
61 | quit(0) | |
62 | ||
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 | ||
69 | let searchResults = getSearchResults(searchQuery) | |
70 | if options["nonInteractive"]: # Present in machine-readable format | |
71 | for index, (title, url) in searchResults: | |
72 | echo title | |
73 | echo url | |
74 | echo "" | |
75 | quit(0) | |
76 | ||
77 | let numResults = min(limit, len(searchResults)) | |
78 | ||
79 | present(searchResults, options, (0, numResults-1), player) | |
80 | ||
81 | ||
82 | when isMainModule: | |
83 | main() |