]> njoseph.me Git - nimcoon.git/blob - src/youtube.nim
3457062f8bf7eeddab312e01e8ec182612f3d2fb
[nimcoon.git] / src / youtube.nim
1 import
2 httpClient,
3 json,
4 strformat,
5 uri
6
7 import
8 config,
9 types
10
11
12 discard """
13 Using Invidious API to retrieve the search results but playing the results directly from YouTube.
14
15 API reference:
16 https://github.com/iv-org/documentation/blob/master/API.md#get-apiv1search
17 """
18
19 proc getSearchResults*(searchQuery: string): SearchResults =
20 let queryParam = encodeUrl(searchQuery)
21 let client = newHttpClient()
22 let response = get(client, &"{invidiousInstance}/api/v1/search?q={queryParam}")
23 let jsonData = parseJson($response.body)
24 var searchResults: SearchResults = @[]
25 for item in jsonData:
26 if item["type"].getStr() == "video":
27 searchResults.add((item["title"].getStr(), "https://www.youtube.com/watch?v=" & item["videoId"].getStr()))
28 elif item["type"].getStr() == "playlist":
29 searchResults.add((item["title"].getStr(), "https://www.youtube.com/watch?v=" & item["playlistId"].getStr()))
30 # Not handling type = channel for now
31 searchResults