]>
Commit | Line | Data |
---|---|---|
e08e5cbe JN |
1 | import |
2 | httpClient, | |
3 | json, | |
4 | strformat, | |
5f9cdfef | 5 | strutils, |
e08e5cbe JN |
6 | uri |
7 | ||
a2d28598 JN |
8 | import |
9 | config, | |
10 | types | |
e08e5cbe JN |
11 | |
12 | ||
a2d28598 | 13 | discard """ |
e08e5cbe | 14 | |
5f9cdfef JN |
15 | Invidious API reference: |
16 | https://github.com/iv-org/documentation/blob/master/API.md | |
a2d28598 | 17 | """ |
e08e5cbe | 18 | |
5f9cdfef JN |
19 | func makeUrl(videoId: string): string = |
20 | "https://www.youtube.com/watch?v=" & videoId | |
21 | ||
22 | ||
a2d28598 | 23 | proc getSearchResults*(searchQuery: string): SearchResults = |
5f9cdfef | 24 | # Using Invidious API to retrieve the search results but playing the results directly from YouTube. |
a2d28598 JN |
25 | let queryParam = encodeUrl(searchQuery) |
26 | let client = newHttpClient() | |
27 | let response = get(client, &"{invidiousInstance}/api/v1/search?q={queryParam}") | |
28 | let jsonData = parseJson($response.body) | |
e08e5cbe | 29 | var searchResults: SearchResults = @[] |
a2d28598 JN |
30 | for item in jsonData: |
31 | if item["type"].getStr() == "video": | |
5f9cdfef | 32 | searchResults.add((item["title"].getStr(), makeUrl(item["videoId"].getStr()))) |
a2d28598 | 33 | elif item["type"].getStr() == "playlist": |
5f9cdfef | 34 | searchResults.add((item["title"].getStr(), makeUrl(item["playlistId"].getStr()))) |
a2d28598 | 35 | # Not handling type = channel for now |
e08e5cbe | 36 | searchResults |
5f9cdfef JN |
37 | |
38 | proc getAutoPlayVideo*(searchResult: SearchResult): SearchResult = | |
39 | # Take a search result and fetch its first recommendation | |
40 | let videoId = searchResult.url.split("=")[1] | |
41 | let client = newHttpClient() | |
42 | let response = get(client, &"{invidiousInstance}/api/v1/videos/{videoId}") | |
43 | let jsonData = parseJson($response.body) | |
44 | let firstRecommendation = jsonData["recommendedVideos"][0] | |
45 | (firstRecommendation["title"].getStr(), makeUrl(firstRecommendation["videoId"].getStr())) |