1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-14 03:57:31 +00:00
NewLeaf/extractors/suggestions.py

32 lines
640 B
Python
Raw Normal View History

2020-08-13 14:20:11 +00:00
import requests
from cachetools import TTLCache
suggestions_cache = TTLCache(maxsize=400, ttl=60)
def extract_search_suggestions(q):
if q in suggestions_cache:
return suggestions_cache[q]
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()
result = {
"query": q,
"suggestions": [s[0] for s in response[1]]
}
suggestions_cache[q] = result
return result