From 080b00bc0c065d1fae58ca6c3f1b6b2f9ac98185 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 10 Aug 2020 19:25:15 +1200 Subject: [PATCH] Implement search suggestions --- index.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/index.py b/index.py index 6dc666b..68e4aa9 100644 --- a/index.py +++ b/index.py @@ -39,7 +39,7 @@ class Second(object): endpoints = [ ["channels", 1, 2], ["videos", 1, 1], - ["search", 0, 0] + ["search", 0, 1] ] for e in endpoints: if vpath[2] == e[0] and len(vpath) >= e[1]+3 and len(vpath) <= e[2]+3: @@ -389,7 +389,10 @@ class Second(object): @cherrypy.expose @cherrypy.tools.json_out() - def search(self, *, q, **kwargs): + def search(self, *suffix, q, **kwargs): + if suffix == ("suggestions",): + return self.suggestions(q=q) + info = ytdl.extract_info("ytsearchall:{}".format(q), download=False) return list({ "type": "video", @@ -411,6 +414,30 @@ class Second(object): "isUpcoming": None } for video in info["entries"] if "title" in video) + @cherrypy.expose + @cherrypy.tools.json_out() + def suggestions(self, *, q, **kwargs): + params = { + "client": "youtube", + "hl": "en", + "gl": "us", + "gs_rn": "64", + "gs_ri": "youtube", + "ds": "yt", + "cp": "3", + "gs_id": "k", + "q": q, + "xhr": "t", + # "xssi": "t" + } + with requests.get("https://clients1.google.com/complete/search", params=params) as r: + r.raise_for_status() + response = r.json() + return { + "query": q, + "suggestions": [s[0] for s in response[1]] + } + @cherrypy.expose def vi(self, id, file): with requests.get("https://i.ytimg.com/vi/{}/{}".format(id, file)) as r: