mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-10 09:07:48 +01:00
Add /api/v1/search/suggestions
This commit is contained in:
parent
196ee1aa8b
commit
fb383458d7
1 changed files with 36 additions and 0 deletions
|
@ -3840,6 +3840,42 @@ get "/api/v1/search" do |env|
|
|||
end
|
||||
end
|
||||
|
||||
get "/api/v1/search/suggestions" do |env|
|
||||
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
||||
region = env.params.query["region"]?
|
||||
|
||||
env.response.content_type = "application/json"
|
||||
|
||||
query = env.params.query["q"]?
|
||||
query ||= ""
|
||||
|
||||
begin
|
||||
client = make_client(URI.parse("https://suggestqueries.google.com"))
|
||||
response = client.get("/complete/search?hl=en&gl=#{region}&client=youtube&ds=yt&q=#{URI.escape(query)}&callback=suggestCallback").body
|
||||
|
||||
body = response[35..-2]
|
||||
body = JSON.parse(body).as_a
|
||||
suggestions = body[1].as_a[0..-2]
|
||||
|
||||
JSON.build do |json|
|
||||
json.object do
|
||||
json.field "query", body[0].as_s
|
||||
json.field "suggestions" do
|
||||
json.array do
|
||||
suggestions.each do |suggestion|
|
||||
json.string suggestion[0].as_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue ex
|
||||
env.response.status_code = 500
|
||||
error_message = {"error" => ex.message}.to_json
|
||||
next error_message
|
||||
end
|
||||
end
|
||||
|
||||
get "/api/v1/playlists/:plid" do |env|
|
||||
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
||||
|
||||
|
|
Loading…
Reference in a new issue