]>
Commit | Line | Data |
---|---|---|
1 | import | |
2 | parseopt, | |
3 | std/[terminal], | |
4 | strutils | |
5 | ||
6 | import config | |
7 | import lib | |
8 | ||
9 | proc parseOptions(): CommandLineOptions = | |
10 | var | |
11 | searchQuery = "" | |
12 | musicOnly = false | |
13 | feelingLucky = false | |
14 | fullScreen = false | |
15 | ||
16 | for kind, key, value in getopt(): | |
17 | case kind | |
18 | of cmdArgument: | |
19 | searchQuery = key | |
20 | of cmdShortOption, cmdLongOption: | |
21 | case key | |
22 | of "m", "music": musicOnly = true | |
23 | of "l", "lucky": feelingLucky = true | |
24 | of "f", "full-screen": fullScreen = true | |
25 | of cmdEnd: | |
26 | discard | |
27 | ||
28 | return (searchQuery, musicOnly, feelingLucky, fullScreen) | |
29 | ||
30 | ||
31 | proc main() = | |
32 | let | |
33 | player = selectMediaPlayer() | |
34 | (searchQuery, musicOnly, feelingLucky, fullScreen) = parseOptions() | |
35 | ||
36 | if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"): | |
37 | directPlay(searchQuery, player) | |
38 | ||
39 | let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) | |
40 | ||
41 | proc getUserInput(): string = | |
42 | if feelingLucky: "0" | |
43 | else: | |
44 | presentVideoOptions(searchResults[..(limit-1)]) | |
45 | stdout.styledWrite(fgYellow, "Choose video number: ") | |
46 | readLine(stdin) | |
47 | ||
48 | # This is a pure function with no side effects | |
49 | func buildArgs(number: int): seq[string] = | |
50 | var args = @[searchResults[number].url] | |
51 | if musicOnly: args.add("--no-video") | |
52 | if fullScreen: args.add("--fullscreen") | |
53 | return args | |
54 | ||
55 | while(true): | |
56 | let userInput = getUserInput() | |
57 | ||
58 | if userInput == "all": | |
59 | for number in 0..(len(searchResults)): | |
60 | play(player, buildArgs(number)) | |
61 | ||
62 | if userInput == "q": | |
63 | break | |
64 | ||
65 | let videoNumber = parseInt(userInput) | |
66 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[videoNumber].title | |
67 | ||
68 | # Play the video using the preferred/available media player | |
69 | play(player, buildArgs(videoNumber)) | |
70 | if feelingLucky: | |
71 | break | |
72 | ||
73 | ||
74 | when isMainModule: | |
75 | main() |