]>
Commit | Line | Data |
---|---|---|
6ff2dbac | 1 | import |
6ff2dbac | 2 | parseopt, |
6ff2dbac | 3 | std/[terminal], |
6f161e0b JN |
4 | strformat, |
5 | strutils, | |
6 | tables | |
44978125 | 7 | |
72720bec | 8 | import config |
d65a1dcf | 9 | import lib |
44978125 | 10 | |
6f161e0b JN |
11 | |
12 | proc parseArguments(): CommandLineOptions = | |
121e06b2 JN |
13 | var |
14 | searchQuery = "" | |
6f161e0b | 15 | options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false}) |
121e06b2 JN |
16 | |
17 | for kind, key, value in getopt(): | |
18 | case kind | |
19 | of cmdArgument: | |
20 | searchQuery = key | |
21 | of cmdShortOption, cmdLongOption: | |
22 | case key | |
6f161e0b JN |
23 | of "m", "music": options["musicOnly"] = true |
24 | of "l", "lucky": options["feelingLucky"] = true | |
25 | of "f", "full-screen": options["fullScreen"] = true | |
26 | of "d", "download": options["download"] = true | |
27 | of cmdEnd: discard | |
28 | ||
29 | return (searchQuery, options) | |
121e06b2 | 30 | |
6f161e0b JN |
31 | |
32 | proc isValidOptions*(options: Options): bool = | |
33 | # Check for invalid combinations of options | |
34 | var invalidCombinations = [("musicOnly", "fullScreen")] | |
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]}" | |
38 | return false | |
39 | return true | |
121e06b2 | 40 | |
d807245d | 41 | |
121e06b2 JN |
42 | proc main() = |
43 | let | |
44 | player = selectMediaPlayer() | |
6f161e0b JN |
45 | (searchQuery, options) = parseArguments() |
46 | ||
47 | if(not isValidOptions(options)): | |
48 | quit(1) | |
121e06b2 | 49 | |
92f80e5a JN |
50 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): |
51 | directPlay(searchQuery, player) | |
9e6b8568 | 52 | quit(0) |
121e06b2 JN |
53 | |
54 | let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) | |
55 | ||
d807245d | 56 | proc getUserInput(): string = |
6f161e0b | 57 | if options["feelingLucky"]: "0" |
121e06b2 | 58 | else: |
72720bec | 59 | presentVideoOptions(searchResults[..(limit-1)]) |
121e06b2 | 60 | stdout.styledWrite(fgYellow, "Choose video number: ") |
d807245d | 61 | readLine(stdin) |
efcc0441 | 62 | |
c8812b68 | 63 | # This is a pure function with no side effects |
d807245d | 64 | func buildArgs(number: int): seq[string] = |
c8812b68 | 65 | var args = @[searchResults[number].url] |
6f161e0b JN |
66 | if options["musicOnly"]: args.add("--no-video") |
67 | if options["fullScreen"]: args.add("--fullscreen") | |
c8812b68 | 68 | return args |
d1e4d2de | 69 | |
d807245d JN |
70 | while(true): |
71 | let userInput = getUserInput() | |
72720bec JN |
72 | |
73 | if userInput == "all": | |
74 | for number in 0..(len(searchResults)): | |
9e6b8568 | 75 | play(player, buildArgs(number), searchResults[number].title) |
72720bec | 76 | |
d807245d JN |
77 | if userInput == "q": |
78 | break | |
79 | ||
d807245d | 80 | # Play the video using the preferred/available media player |
9e6b8568 JN |
81 | let videoNumber = parseInt(userInput) |
82 | play(player, buildArgs(videoNumber), searchResults[videoNumber].title) | |
6f161e0b | 83 | if options["feelingLucky"]: |
d807245d JN |
84 | break |
85 | ||
a2319a3f | 86 | |
d65a1dcf JN |
87 | when isMainModule: |
88 | main() |