]> njoseph.me Git - nimcoon.git/blobdiff - src/lib.nim
Move to new invidious instance
[nimcoon.git] / src / lib.nim
index ccd1a39797f990c90c870f52d72bc72f179150ec..0f6ae154a249a7bf06fc36eaa1675444fb5878f9 100644 (file)
@@ -8,7 +8,6 @@ import
   sequtils,
   std/[terminal],
   strformat,
-  strformat,
   strtabs,
   strutils,
   sugar,
@@ -27,9 +26,8 @@ type
   SelectionRange* = tuple[begin: int, until: int]
 
 
-# poEchoCmd can be added to options for debugging
 let
-  processOptions = {poStdErrToStdOut, poUsePath}
+  processOptions = {poStdErrToStdOut, poUsePath} # poEchoCmd can be added to options for debugging
   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}"
 
 
@@ -49,12 +47,12 @@ proc selectMediaPlayer*(): string =
 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}")
+  let response = get(client, &"https://invidious.snopyta.org/search?q={queryParam}")
   $response.body
 
 
 proc getPeerTubeMagnetLink(url: string): string =
-  # Gets the magnet link of the best possible resolutino from PeerTube
+  ## Gets the magnet link of the best possible resolution from PeerTube
   let uuid = url.substr(find(url, PEERTUBE_REGEX) + "videos/watch/".len)
   let domainName = url.substr(8, find(url, '/', start=8) - 1)
   let apiURL = &"https://{domainName}/api/v1/videos/{uuid}"
@@ -66,15 +64,16 @@ proc getPeerTubeMagnetLink(url: string): string =
 
 func extractTitlesAndUrls*(html: string): SearchResults =
   {.noSideEffect.}:
+    # TODO Pick an invidious instance from config. Using YouTube directly for now.
     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"]))
+      filter(a => "watch" in a.attrs["href"] and len(a) == 1).
+      map(a => (innerText(a), "https://www.youtube.com" & a.attrs["href"]))
 
 
 proc presentVideoOptions*(searchResults: SearchResults) =
   eraseScreen()
   for index, (title, url) in searchResults:
-    styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
+    styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, "   ", url, "\n"
 
 
 func isPlaylist(url: string): bool =
@@ -157,7 +156,10 @@ proc directDownload*(url: string, musicOnly: bool) =
   let args =
     if musicOnly: buildMusicDownloadArgs(url)
     else: buildVideoDownloadArgs(url)
-  discard execShellCmd(&"youtube-dl {args.join(\" \")}")
+  if isInstalled("aria2c"):
+    discard execShellCmd(&"youtube-dl {args.join(\" \")} --external-downloader aria2c --external-downloader-args '-x 16 -s 16 -k 2M'")
+  else:
+    discard execShellCmd(&"youtube-dl {args.join(\" \")}")
 
 
 proc offerSelection(searchResults: SearchResults, options: Table[string, bool], selectionRange: SelectionRange): string =