]> njoseph.me Git - nimcoon.git/blobdiff - nimcoon.nim
Introduce a func
[nimcoon.git] / nimcoon.nim
index c838b343f66568c6e054d4f05de5bd7c55e20590..7b69ddb5ab3e8320184054e9e82b17db9bf6c376 100644 (file)
@@ -1,7 +1,6 @@
 import
   htmlparser,
   httpClient,
-  logging,
   parseopt,
   osproc,
   sequtils,
@@ -10,7 +9,6 @@ import
   std/[terminal],
   strtabs,
   strutils,
-  tables,
   uri,
   xmltree
 
@@ -20,9 +18,6 @@ type
   SearchResult = tuple[title: string, url: string]
   CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool]
 
-let logger = newConsoleLogger()
-setLogFilter(lvlInfo)
-
 proc selectMediaPlayer(): string =
   let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
   if len(availablePlayers) == 0:
@@ -58,8 +53,8 @@ proc getYoutubePage(searchQuery: string): string =
   let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
   return $response.body
 
-proc extractTitlesAndUrls(htmlFile: string): seq[SearchResult] =
-  parseHtml(htmlFile).findAll("a").
+proc extractTitlesAndUrls(html: string): seq[SearchResult] =
+  parseHtml(html).findAll("a").
     filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title").
     map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"]))[..(limit-1)]
 
@@ -68,16 +63,15 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) =
   for index, (title, url) in searchResults:
     styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
 
-proc play(command: string) =
-  logger.log(lvlDebug, &"Executing: ${command}")
-  discard execProcess(command)
+proc play(player: string, args: openArray[string]) =
+  discard execProcess(player, args=args, options={poStdErrToStdOut, poUsePath, poEchoCmd})
   quit(0)
 
 proc directPlay(searchQuery: string, player: string) =
-  if "watch?" in searchQuery or "videos/watch" in searchQuery :
-    play(&"{player} {searchQuery}")
+  if "watch?" in searchQuery or "videos/watch" in searchQuery:
+    play(player, args=[searchQuery])
   elif searchQuery.startswith("magnet:"):
-    play(&"peerflix \"{searchQuery}\" --{player}")
+    play("peerflix", args=[searchQuery, &"--{player}"])
 
 proc main() =
   let
@@ -98,15 +92,14 @@ proc main() =
 
   styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
 
-  var command = @[player, searchResults[number].url]
-
-  if musicOnly:
-    command.add(playerOptions[player]["musicOnly"])
-
-  if fullScreen:
-    command.add(playerOptions[player]["fullScreen"])
+  # This is a pure function with no side effects
+  func buildArgs(): seq[string] =
+    var args = @[searchResults[number].url]
+    if musicOnly: args.add("--no-video")
+    if fullScreen: args.add("--fullscreen")
+    return args
 
   # Play the video using the preferred/available media player
-  play(command.join(" "))
+  play(player, buildArgs())
 
 main()