Display title when feeling lucky
authorJoseph Nuthalapati <njoseph@riseup.net>
Sat, 2 Jan 2021 10:28:08 +0000 (15:58 +0530)
committerJoseph Nuthalapati <njoseph@riseup.net>
Sat, 2 Jan 2021 10:28:37 +0000 (15:58 +0530)
Signed-off-by: Joseph Nuthalapati <njoseph@riseup.net>
src/lib.nim

index 25a80169bbc1b8f6c8359da8b1cd18f56f2429a7..4dbbf8be6f74ea687d8b728fcbbd5d460a9fafaa 100644 (file)
@@ -33,6 +33,10 @@ proc selectMediaPlayer*(): string =
     return availablePlayers[0]
 
 
+proc printTitle(action: string, title: string) =
+    styledEcho "\n", fgGreen, &"{action} ", styleBright, fgMagenta, title
+
+
 ###############
 # URL CLEANUP #
 ###############
@@ -68,7 +72,7 @@ func buildPlayerArgs(url: string, options: Table[string, bool], player: string):
 proc play*(player: string, options: Table[string, bool], url: string, title: string = "") =
   let args = buildPlayerArgs(url, options, player)
   if title != "":
-    styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title
+    printTitle("Playing", title)
   if "--no-video" in args:
     discard execShellCmd(&"{player} {args.join(\" \")}")
   else:
@@ -114,7 +118,7 @@ func buildDownloadArgs(url: string, options: Options): seq[string] =
 
 
 proc download*(args: openArray[string], title: string) =
-  styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
+  printTitle("Downloading", title)
   discard execShellCmd(&"youtube-dl {args.join(\" \")}")
 
 
@@ -127,14 +131,16 @@ proc directDownload*(url: string, options: Options) =
 
 proc luckyDownload*(searchQuery: string, options: Options) =
   let args = @[&"ytsearch:\"{searchQuery}\""] & buildDownloadArgs("", options)
-  styledEcho "\n", fgGreen, "Searching and downloading using youtube-dl"
-  discard execShellCmd(&"youtube-dl {args.join(\" \")}")
+  let title = execProcess(&"youtube-dl --get-title {args.join(\" \")}").split("\n")[0]
+  download(args, title)
 
 proc luckyPlay*(searchQuery: string, player: string, options: Options) =
   let args = @[&"ytsearch:\"{searchQuery}\""] & buildDownloadArgs("", options)
-  let output = execProcess(&"youtube-dl --get-url {args.join(\" \")}")
-  let url = output.split("\n")[0]
-  play(player, options, url)
+  let output = execProcess(&"youtube-dl --get-url --get-title {args.join(\" \")}").split("\n")
+  let
+    title = output[0]
+    url = &"\"{output[1]}\""
+  play(player, options, url, title)
 
 ###########
 # OPTIONS #