]> njoseph.me Git - nimcoon.git/blob - src/nimcoon.nim
pagination: "p" takes to the previous page of results
[nimcoon.git] / src / nimcoon.nim
1 import
2 os,
3 parseopt,
4 std/[terminal],
5 strformat,
6 strutils,
7 tables
8
9 import config
10 import lib
11
12
13 proc parseArguments(): CommandLineOptions =
14 var
15 searchQuery = ""
16 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
17
18 for kind, key, value in getopt():
19 case kind
20 of cmdArgument:
21 searchQuery = key
22 of cmdShortOption, cmdLongOption:
23 case key
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)
31
32
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]}"
39 return false
40 return true
41
42
43 proc main() =
44 let
45 player = selectMediaPlayer()
46 (searchQuery, options) = parseArguments()
47
48 if(not isValidOptions(options)):
49 quit(1)
50
51 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
52 if options["download"]:
53 directDownload(sanitizeURL(searchQuery), options["musicOnly"])
54 else:
55 directPlay(sanitizeURL(searchQuery), player, options["musicOnly"])
56 quit(0)
57
58 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
59
60 # Currently available range to choose from depending on pagination
61 var selectionRange: SelectionRange = (0, limit-1) # Nim decided to deviate from Python ranges here
62
63 proc offerSelection(): string =
64 if options["feelingLucky"]: "0"
65 else:
66 presentVideoOptions(searchResults[selectionRange.begin .. selectionRange.until])
67 stdout.styledWrite(fgYellow, "Choose video number: ")
68 readLine(stdin)
69
70 # This is a pure function with no side effects
71 func buildPlayerArgs(number: int): seq[string] =
72 var args = @[searchResults[number].url]
73 if options["musicOnly"]: args.add("--no-video")
74 if options["fullScreen"]: args.add("--fullscreen")
75 return args
76
77 proc handleUserInput(selection: int) =
78 if options["download"]:
79 if options["musicOnly"]:
80 download(buildMusicDownloadArgs(searchResults[selection].url), searchResults[selection].title)
81 else:
82 download(buildVideoDownloadArgs(searchResults[selection].url), searchResults[selection].title)
83 else:
84 play(player, buildPlayerArgs(selection), searchResults[selection].title)
85
86
87 while(true):
88 let userInput = offerSelection()
89
90 case userInput
91 of "all":
92 for selection in selectionRange.begin .. selectionRange.until:
93 # TODO `spawn` this?
94 handleUserInput(selection)
95 of "n":
96 if selectionRange.until + 1 < len(searchResults):
97 selectionRange = (selectionRange.until + 1, min(len(searchResults) - 1, selectionRange.until + limit))
98 continue
99 of "p":
100 if selectionRange.begin > 0:
101 selectionRange = (selectionRange.begin - limit, selectionRange.until - limit)
102 continue
103 of "q":
104 quit(0)
105
106 # Play the video using the preferred/available media player
107 let videoNumber = parseInt(userInput)
108 handleUserInput(videoNumber)
109 if options["feelingLucky"]:
110 break
111
112
113 when isMainModule:
114 main()