Add option to play music only
[nimcoon.git] / clitube.nim
index 89e863cb5bdf8af381fb44cbf9ac0f3dc466b8ba..3714c13612433ed295690aec1a41063ecfde2110 100644 (file)
@@ -1,6 +1,6 @@
 import htmlparser
 import httpClient
-import os
+import parseopt
 import osproc
 import sequtils, sugar
 import strformat
@@ -10,11 +10,7 @@ import strutils
 import uri
 import xmltree
 
-# Supported video players in order of preference
-let supportedPlayers = ["mpv", "vlc"]
-
-# Only show these many results
-let limit = 10
+import preferences
 
 type SearchResult = tuple[title: string, url: string]
 
@@ -42,21 +38,36 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) =
   for index, (title, url) in searchResults:
     styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
 
-let input = paramStr(1)
 let player = selectMediaPlayer()
 
-if "https://www.youtube.com" in input:
-  discard execProcess(&"{player} {input}")
+var searchQuery = ""
+var musicOnly = false
+
+for kind, key, value in getopt():
+  case kind
+  of cmdArgument:
+     searchQuery = key
+  of cmdShortOption, cmdLongOption:
+    case key
+    of "m", "music": musicOnly = true
+  of cmdEnd:
+    discard
+
+let noVideo = if musicOnly: "--no-video" else: ""
+
+if "https://www.youtube.com" in searchQuery:
+  discard execProcess(&"{player} {searchQuery}")
   quit(0)
 
-let searchResults = extractTitlesAndUrls(getYoutubePage(input))
+
+let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
 
 presentVideoOptions(searchResults)
 
 stdout.styledWrite(fgYellow, "Choose video number: ")
-let number: int = parseInt(readLine(stdin))
+let number = parseInt(readLine(stdin))
 
 styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title
 
 # Play the video using the preferred/available media player
-discard execProcess(&"{player}  {searchResults[number].url}")
+discard execProcess(&"{player} {noVideo} {searchResults[number].url}")