]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
Get rid of Peerflix. Only use Webtorrent.
[nimcoon.git] / src / lib.nim
index 25a80169bbc1b8f6c8359da8b1cd18f56f2429a7..4064ae43cdbdde02acbf5722317597f4a2d11fa1 100644 (file)
@@ -17,7 +17,7 @@ import
 
 let
   processOptions = {poStdErrToStdOut, poUsePath} # Add poEchoCmd to debug
-  PEERTUBE_REGEX = re"videos\/watch\/[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}"
+  PEERTUBE_REGEX = re"w\/[0-9a-zA-z]{22}"
 
 
 proc isInstalled(program: string): bool =
@@ -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:
@@ -78,14 +82,12 @@ proc play*(player: string, options: Table[string, bool], url: string, title: str
 proc directPlay*(url: string, player: string, options: Table[string, bool]) =
   let url =
     if find(url, PEERTUBE_REGEX) != -1 and "webtorrent".isInstalled:
-      getPeerTubeMagnetLink(url)
+      getPeerTubeMagnetLink(url, options["musicOnly"])
     else: url
   if url.startswith("magnet:") or url.endswith(".torrent"):
     if options["musicOnly"]:
-      # TODO Replace with WebTorrent once it supports media player options
-      discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video")
+      discard execShellCmd(&"webtorrent '{url}' --{player} --player-args='--no-video'")
     else:
-      # WebTorrent is so much faster!
       discard execProcess("webtorrent", args=[url, &"--{player}"], options=processOptions)
   else:
     play(player, options, url)
@@ -114,27 +116,29 @@ func buildDownloadArgs(url: string, options: Options): seq[string] =
 
 
 proc download*(args: openArray[string], title: string) =
-  styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title
-  discard execShellCmd(&"youtube-dl {args.join(\" \")}")
+  printTitle("Downloading", title)
+  discard execShellCmd(&"yt-dlp {args.join(\" \")}")
 
 
 proc directDownload*(url: string, options: Options) =
   let args = buildDownloadArgs(url, options)
   if "aria2c".isInstalled:
-    discard execShellCmd(&"youtube-dl {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'")
+    discard execShellCmd(&"yt-dlp {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'")
   else:
-    discard execShellCmd(&"youtube-dl {args.join(\" \")}")
+    discard execShellCmd(&"yt-dlp {args.join(\" \")}")
 
 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 args = @[&"ytsearch1:\"{searchQuery}\""] & buildDownloadArgs("", options)
+  let title = execProcess(&"yt-dlp --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(&"yt-dlp --get-url --get-title {args.join(\" \")}").split("\n")
+  let
+    title = output[0]
+    url = &"\"{output[1]}\""
+  play(player, options, url, title)
 
 ###########
 # OPTIONS #