refactor: support search caching

remains to be seen whether this is a good idea or not
This commit is contained in:
2025-09-07 05:30:16 +02:00
parent 37e932956d
commit 2a9826eb03
3 changed files with 53 additions and 24 deletions

View File

@@ -348,29 +348,59 @@ def WEBgetSearchSuggestions(query: str, previous_query: str = '') -> list:
# Takes in a search query and returns relevant suggestions.
# Can optionally take the previous query but that's rather novel and
# not supported across players nor invidious API itself.
params = {
'ds': 'yt',
'hl': 'en', # host language
'gl': 'us', # geolocation
'client': 'youtube',
'gs_ri': 'youtube',
'q': str(query), # query
'pq': str(previous_query) if previous_query is not None else '' # previous query
}
response = requests.get(
'https://suggestqueries-clients6.youtube.com/complete/search',
params=params,
headers=stage2_headers
)
# can break anytime but hopefully the tiny speed gain will make up for it
results = response.text[23 + len(query):]
results = results[:results.rfind("{") - 1]
results = json.loads(results)
suggestions = []
for result in results:
suggestions.append(result[0])
if not isinstance(query, str):
print("WEBgetSearchSuggestions: query is not a string (as it should)")
return {}
if not isinstance(previous_query, str):
previous_query = ''
if ythdd_globals.config["general"]["cache"]:
# look for cached suggestions
for cached_search in ythdd_globals.general_cache["search"]:
if cached_search["q"] == query.lower() and cached_search["pq"] == previous_query.lower():
# found it? skip ahead
suggestions = cached_search["resp"]
break
# request wasn't cached? query the API
if suggestions == []:
params = {
'ds': 'yt',
'hl': 'en', # host language
'gl': 'us', # geolocation
'client': 'youtube',
'gs_ri': 'youtube',
'q': query, # query
'pq': previous_query # previous query
}
response = requests.get(
'https://suggestqueries-clients6.youtube.com/complete/search',
params=params,
headers=stage2_headers
)
# can break anytime but hopefully the tiny speed gain will make up for it
results = response.text[23 + len(query):]
results = results[:results.rfind("{") - 1]
results = json.loads(results)
for result in results:
suggestions.append(result[0])
# cache response
if ythdd_globals.config["general"]["cache"]:
ythdd_globals.general_cache["search"].append(
{
"q": query.lower(),
"pq": previous_query.lower(),
"resp": suggestions
}
)
return {
"query": query,