mirror of
https://git.sr.ht/~cadence/NewLeaf
synced 2024-11-15 20:37:30 +00:00
32 lines
640 B
Python
32 lines
640 B
Python
|
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
|