13 proc parseArguments(): CommandLineOptions =
16 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
18 for kind, key, value in getopt():
22 of cmdShortOption, cmdLongOption:
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
30 return (searchQuery, options)
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]}"
45 player = selectMediaPlayer()
46 (searchQuery, options) = parseArguments()
48 if(not isValidOptions(options)):
51 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
52 if options["download"]:
53 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
55 directPlay(sanitizeURL(searchQuery), player, options["musicOnly"])
58 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
60 proc getUserInput(): string =
61 if options["feelingLucky"]: "0"
63 presentVideoOptions(searchResults[..(limit-1)])
64 stdout.styledWrite(fgYellow, "Choose video number: ")
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")
74 proc handleUserInput(number: int) =
75 if options["download"]:
76 if options["musicOnly"]:
77 download(buildMusicDownloadArgs(searchResults[number].url), searchResults[number].title)
79 download(buildVideoDownloadArgs(searchResults[number].url), searchResults[number].title)
81 play(player, buildPlayerArgs(number), searchResults[number].title)
85 let userInput = getUserInput()
87 if userInput == "all":
88 for number in 0..(len(searchResults)):
90 handleUserInput(number)
95 # Play the video using the preferred/available media player
96 let videoNumber = parseInt(userInput)
97 handleUserInput(videoNumber)
98 if options["feelingLucky"]: