]>
Commit | Line | Data |
---|---|---|
6ff2dbac | 1 | import |
6ff2dbac | 2 | parseopt, |
6f161e0b JN |
3 | strformat, |
4 | strutils, | |
5 | tables | |
44978125 | 6 | |
72720bec | 7 | import config |
d65a1dcf | 8 | import lib |
44978125 | 9 | |
6f161e0b JN |
10 | |
11 | proc 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 | ||
28 | return (searchQuery, options) | |
121e06b2 | 29 | |
6f161e0b JN |
30 | |
31 | proc isValidOptions*(options: Options): bool = | |
32 | # Check for invalid combinations of options | |
95683732 | 33 | var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")] |
6f161e0b JN |
34 | for combination in invalidCombinations: |
35 | if options[combination[0]] and options[combination[1]]: | |
36 | stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}" | |
37 | return false | |
38 | return true | |
121e06b2 | 39 | |
121e06b2 JN |
40 | proc main() = |
41 | let | |
42 | player = selectMediaPlayer() | |
6f161e0b JN |
43 | (searchQuery, options) = parseArguments() |
44 | ||
45 | if(not isValidOptions(options)): | |
46 | quit(1) | |
121e06b2 | 47 | |
92f80e5a | 48 | if searchQuery.startswith("https:") 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 |
60 | when isMainModule: |
61 | main() |