]>
Commit | Line | Data |
---|---|---|
e08e5cbe JN |
1 | import |
2 | httpClient, | |
3 | json, | |
4 | strformat, | |
e08e5cbe JN |
5 | uri |
6 | ||
a2d28598 JN |
7 | import |
8 | config, | |
9 | types | |
e08e5cbe JN |
10 | |
11 | ||
a2d28598 JN |
12 | discard """ |
13 | Using Invidious API to retrieve the search results but playing the results directly from YouTube. | |
e08e5cbe | 14 | |
a2d28598 JN |
15 | API reference: |
16 | https://github.com/iv-org/documentation/blob/master/API.md#get-apiv1search | |
17 | """ | |
e08e5cbe | 18 | |
a2d28598 JN |
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) | |
e08e5cbe | 24 | var searchResults: SearchResults = @[] |
a2d28598 JN |
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 | |
e08e5cbe | 31 | searchResults |