Add option to play music only
[nimcoon.git] / clitube.nim
index 0c21a941bbaa8d30f206b2eded4371c134f3a277..3714c13612433ed295690aec1a41063ecfde2110 100644 (file)
@@ -1,6 +1,6 @@
 import htmlparser
 import httpClient
-import os
+import parseopt
 import osproc
 import sequtils, sugar
 import strformat
@@ -38,14 +38,29 @@ 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)
 
@@ -55,4 +70,4 @@ 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}")