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")]
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 directPlay(searchQuery, player)
55 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
57 proc getUserInput(): string =
58 if options["feelingLucky"]: "0"
60 presentVideoOptions(searchResults[..(limit-1)])
61 stdout.styledWrite(fgYellow, "Choose video number: ")
64 # This is a pure function with no side effects
65 func buildPlayerArgs(number: int): seq[string] =
66 var args = @[searchResults[number].url]
67 if options["musicOnly"]: args.add("--no-video")
68 if options["fullScreen"]: args.add("--fullscreen")
71 func buildMusicDownloadArgs(number: int): seq[string] =
73 var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"]
74 let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/{searchResults[number].title}.mp3'"
75 args.add(downloadLocation)
76 args.add(searchResults[number].url)
79 proc handleUserInput(number: int) =
80 if options["download"]:
81 if options["musicOnly"]:
82 download(buildMusicDownloadArgs(number), searchResults[number].title)
84 play(player, buildPlayerArgs(number), searchResults[number].title)
88 let userInput = getUserInput()
90 if userInput == "all":
91 for number in 0..(len(searchResults)):
93 handleUserInput(number)
98 # Play the video using the preferred/available media player
99 let videoNumber = parseInt(userInput)
100 handleUserInput(videoNumber)
101 if options["feelingLucky"]: