]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | os, | |
3 | parseopt, | |
4 | std/[terminal], | |
5 | strformat, | |
6 | strutils, | |
7 | tables | |
8 | ||
9 | import config | |
10 | import lib | |
11 | ||
12 | ||
13 | proc parseArguments(): CommandLineOptions = | |
14 | var | |
15 | searchQuery = "" | |
16 | options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false}) | |
17 | ||
18 | for kind, key, value in getopt(): | |
19 | case kind | |
20 | of cmdArgument: | |
21 | searchQuery = key | |
22 | of cmdShortOption, cmdLongOption: | |
23 | case key | |
24 | of "m", "music": options["musicOnly"] = true | |
25 | of "l", "lucky": options["feelingLucky"] = true | |
26 | of "f", "full-screen": options["fullScreen"] = true | |
27 | of "d", "download": options["download"] = true | |
28 | of cmdEnd: discard | |
29 | ||
30 | return (searchQuery, options) | |
31 | ||
32 | ||
33 | proc isValidOptions*(options: Options): bool = | |
34 | # Check for invalid combinations of options | |
35 | var invalidCombinations = [("musicOnly", "fullScreen"), ("download", "fullScreen")] | |
36 | for combination in invalidCombinations: | |
37 | if options[combination[0]] and options[combination[1]]: | |
38 | stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}" | |
39 | return false | |
40 | return true | |
41 | ||
42 | ||
43 | proc main() = | |
44 | let | |
45 | player = selectMediaPlayer() | |
46 | (searchQuery, options) = parseArguments() | |
47 | ||
48 | if(not isValidOptions(options)): | |
49 | quit(1) | |
50 | ||
51 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): | |
52 | if options["download"]: | |
53 | directDownload(sanitizeURL(searchQuery), options["musicOnly"]) | |
54 | else: | |
55 | directPlay(sanitizeURL(searchQuery), player, options["musicOnly"]) | |
56 | quit(0) | |
57 | ||
58 | let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) | |
59 | ||
60 | proc getUserInput(): string = | |
61 | if options["feelingLucky"]: "0" | |
62 | else: | |
63 | presentVideoOptions(searchResults[..(limit-1)]) | |
64 | stdout.styledWrite(fgYellow, "Choose video number: ") | |
65 | readLine(stdin) | |
66 | ||
67 | # This is a pure function with no side effects | |
68 | func buildPlayerArgs(number: int): seq[string] = | |
69 | var args = @[searchResults[number].url] | |
70 | if options["musicOnly"]: args.add("--no-video") | |
71 | if options["fullScreen"]: args.add("--fullscreen") | |
72 | return args | |
73 | ||
74 | proc handleUserInput(number: int) = | |
75 | if options["download"]: | |
76 | if options["musicOnly"]: | |
77 | download(buildMusicDownloadArgs(searchResults[number].url), searchResults[number].title) | |
78 | else: | |
79 | download(buildVideoDownloadArgs(searchResults[number].url), searchResults[number].title) | |
80 | else: | |
81 | play(player, buildPlayerArgs(number), searchResults[number].title) | |
82 | ||
83 | ||
84 | while(true): | |
85 | let userInput = getUserInput() | |
86 | ||
87 | if userInput == "all": | |
88 | for number in 0..(len(searchResults)): | |
89 | # TODO `spawn` this? | |
90 | handleUserInput(number) | |
91 | ||
92 | if userInput == "q": | |
93 | break | |
94 | ||
95 | # Play the video using the preferred/available media player | |
96 | let videoNumber = parseInt(userInput) | |
97 | handleUserInput(videoNumber) | |
98 | if options["feelingLucky"]: | |
99 | break | |
100 | ||
101 | ||
102 | when isMainModule: | |
103 | main() |