]> njoseph.me Git - nimcoon.git/blobdiff - nimcoon.nim
Better UX for playing all search results
[nimcoon.git] / nimcoon.nim
index 7b69ddb5ab3e8320184054e9e82b17db9bf6c376..8b1ba591f166a0545053cc420ecf12149c5847ab 100644 (file)
@@ -1,30 +1,10 @@
 import
-  htmlparser,
-  httpClient,
   parseopt,
-  osproc,
-  sequtils,
-  sugar,
-  strformat,
   std/[terminal],
-  strtabs,
-  strutils,
-  uri,
-  xmltree
+  strutils
 
 import config
-
-type
-  SearchResult = tuple[title: string, url: string]
-  CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool, fullScreen: bool]
-
-proc selectMediaPlayer(): string =
-  let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
-  if len(availablePlayers) == 0:
-    stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}"
-    raise newException(OSError, "No supported media player found")
-  else:
-    return availablePlayers[0]
+import lib
 
 proc parseOptions(): CommandLineOptions =
   var
@@ -47,31 +27,6 @@ proc parseOptions(): CommandLineOptions =
 
   return (searchQuery, musicOnly, feelingLucky, fullScreen)
 
-proc getYoutubePage(searchQuery: string): string =
-  let queryParam = encodeUrl(searchQuery)
-  let client = newHttpClient()
-  let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}")
-  return $response.body
-
-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)]
-
-proc presentVideoOptions(searchResults: seq[SearchResult]) =
-  echo ""
-  for index, (title, url) in searchResults:
-    styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
-
-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, args=[searchQuery])
-  elif searchQuery.startswith("magnet:"):
-    play("peerflix", args=[searchQuery, &"--{player}"])
 
 proc main() =
   let
@@ -80,26 +35,40 @@ proc main() =
 
   if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
     directPlay(searchQuery, player)
+    quit(0)
 
   let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
 
-  let number =
-    if feelingLucky: 0
+  proc getUserInput(): string =
+    if feelingLucky: "0"
     else:
-      presentVideoOptions(searchResults)
+      presentVideoOptions(searchResults[..(limit-1)])
       stdout.styledWrite(fgYellow, "Choose video number: ")
-      parseInt(readLine(stdin))
-
-  styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
+      readLine(stdin)
 
   # This is a pure function with no side effects
-  func buildArgs(): seq[string] =
+  func buildArgs(number: int): 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(player, buildArgs())
+  while(true):
+    let userInput = getUserInput()
+
+    if userInput == "all":
+      for number in 0..(len(searchResults)):
+        play(player, buildArgs(number), searchResults[number].title)
+
+    if userInput == "q":
+      break
+
+    # Play the video using the preferred/available media player
+    let videoNumber = parseInt(userInput)
+    play(player, buildArgs(videoNumber), searchResults[videoNumber].title)
+    if feelingLucky:
+      break
+
 
-main()
+when isMainModule:
+  main()