]> njoseph.me Git - nimcoon.git/blobdiff - src/youtube.nim
Fix all recent bugs. Shift to Invidious API.
[nimcoon.git] / src / youtube.nim
index fd98d41e4f2f22b257789e34cf2fe437008b0ab3..3457062f8bf7eeddab312e01e8ec182612f3d2fb 100644 (file)
@@ -2,41 +2,30 @@ import
   httpClient,
   json,
   strformat,
-  strutils,
-  sequtils,
   uri
 
-import types
-
-proc getYouTubePage(searchQuery: string): string =
-  let queryParam = encodeUrl(searchQuery)
-  let client = newHttpClient()
-  let response = get(client, &"https://www.youtube.com/results?search_query={queryParam}")
-  $response.body
+import
+  config,
+  types
 
 
-proc getSearchResults*(searchQuery: string): SearchResults =
-  let html = getYouTubePage(searchQuery)
-  let lines = html.split('\n').filterIt(it.contains("ytInitialData"))
-  let line = lines[0]
-  let jsonString = line.split('=', maxsplit=1)[1].strip().strip(chars={';'})
-  let jsonData = parseJson(jsonString)
+discard """
+Using Invidious API to retrieve the search results but playing the results directly from YouTube.
 
-  let videos = jsonData["contents"]["twoColumnSearchResultsRenderer"]["primaryContents"]["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"]
+API reference:
+https://github.com/iv-org/documentation/blob/master/API.md#get-apiv1search
+"""
 
+proc getSearchResults*(searchQuery: string): SearchResults =
+  let queryParam = encodeUrl(searchQuery)
+  let client = newHttpClient()
+  let response = get(client, &"{invidiousInstance}/api/v1/search?q={queryParam}")
+  let jsonData = parseJson($response.body)
   var searchResults: SearchResults = @[]
-
-  for video in videos:
-    if video.hasKey("videoRenderer"):
-      let title = ($video["videoRenderer"]["title"]["runs"][0]["text"]).strip(chars={'"'})
-      let videoId = ($video["videoRenderer"]["videoId"]).strip(chars={'"'})
-      let videoUrl = &"https://www.youtube.com/watch?v={videoId}"
-      searchResults.add((title, videoUrl))
-
-    elif video.hasKey("playlistRenderer"):
-      let title = ($video["playlistRenderer"]["title"]["simpleText"]).strip(chars={'"'})
-      let playlistId = ($video["playlistRenderer"]["playlistId"]).strip(chars={'"'})
-      let playlistUrl = &"https://www.youtube.com/playlist?list={playlistId}"
-      searchResults.add((title, playlistUrl))
-
+  for item in jsonData:
+    if item["type"].getStr() == "video":
+      searchResults.add((item["title"].getStr(), "https://www.youtube.com/watch?v=" & item["videoId"].getStr()))
+    elif item["type"].getStr() == "playlist":
+      searchResults.add((item["title"].getStr(), "https://www.youtube.com/watch?v=" & item["playlistId"].getStr()))
+    # Not handling type = channel for now
   searchResults