]>
Commit | Line | Data |
---|---|---|
6ff2dbac | 1 | import |
e9f0c7d0 | 2 | os, |
6ff2dbac | 3 | parseopt, |
6ff2dbac | 4 | std/[terminal], |
6f161e0b JN |
5 | strformat, |
6 | strutils, | |
7 | tables | |
44978125 | 8 | |
72720bec | 9 | import config |
d65a1dcf | 10 | import lib |
44978125 | 11 | |
6f161e0b JN |
12 | |
13 | proc parseArguments(): CommandLineOptions = | |
121e06b2 JN |
14 | var |
15 | searchQuery = "" | |
6f161e0b | 16 | options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false}) |
121e06b2 JN |
17 | |
18 | for kind, key, value in getopt(): | |
19 | case kind | |
20 | of cmdArgument: | |
21 | searchQuery = key | |
22 | of cmdShortOption, cmdLongOption: | |
23 | case key | |
6f161e0b JN |
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) | |
121e06b2 | 31 | |
6f161e0b JN |
32 | |
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]}" | |
39 | return false | |
40 | return true | |
121e06b2 | 41 | |
d807245d | 42 | |
121e06b2 JN |
43 | proc main() = |
44 | let | |
45 | player = selectMediaPlayer() | |
6f161e0b JN |
46 | (searchQuery, options) = parseArguments() |
47 | ||
48 | if(not isValidOptions(options)): | |
49 | quit(1) | |
121e06b2 | 50 | |
92f80e5a JN |
51 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): |
52 | directPlay(searchQuery, player) | |
9e6b8568 | 53 | quit(0) |
121e06b2 JN |
54 | |
55 | let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) | |
56 | ||
d807245d | 57 | proc getUserInput(): string = |
6f161e0b | 58 | if options["feelingLucky"]: "0" |
121e06b2 | 59 | else: |
72720bec | 60 | presentVideoOptions(searchResults[..(limit-1)]) |
121e06b2 | 61 | stdout.styledWrite(fgYellow, "Choose video number: ") |
d807245d | 62 | readLine(stdin) |
efcc0441 | 63 | |
c8812b68 | 64 | # This is a pure function with no side effects |
e9f0c7d0 | 65 | func buildPlayerArgs(number: int): seq[string] = |
c8812b68 | 66 | var args = @[searchResults[number].url] |
6f161e0b JN |
67 | if options["musicOnly"]: args.add("--no-video") |
68 | if options["fullScreen"]: args.add("--fullscreen") | |
c8812b68 | 69 | return args |
d1e4d2de | 70 | |
e9f0c7d0 JN |
71 | func buildMusicDownloadArgs(number: int): seq[string] = |
72 | {.noSideEffect.}: | |
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) | |
77 | return args | |
78 | ||
79 | proc handleUserInput(number: int) = | |
80 | if options["download"]: | |
81 | if options["musicOnly"]: | |
82 | download(buildMusicDownloadArgs(number), searchResults[number].title) | |
83 | else: | |
84 | play(player, buildPlayerArgs(number), searchResults[number].title) | |
85 | ||
86 | ||
d807245d JN |
87 | while(true): |
88 | let userInput = getUserInput() | |
72720bec JN |
89 | |
90 | if userInput == "all": | |
91 | for number in 0..(len(searchResults)): | |
e9f0c7d0 JN |
92 | # TODO `spawn` this? |
93 | handleUserInput(number) | |
72720bec | 94 | |
d807245d JN |
95 | if userInput == "q": |
96 | break | |
97 | ||
d807245d | 98 | # Play the video using the preferred/available media player |
9e6b8568 | 99 | let videoNumber = parseInt(userInput) |
e9f0c7d0 | 100 | handleUserInput(videoNumber) |
6f161e0b | 101 | if options["feelingLucky"]: |
d807245d JN |
102 | break |
103 | ||
a2319a3f | 104 | |
d65a1dcf JN |
105 | when isMainModule: |
106 | main() |