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,14 +348,34 @@ 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.
suggestions = []
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': str(query), # query
'pq': str(previous_query) if previous_query is not None else '' # previous query
'q': query, # query
'pq': previous_query # previous query
}
response = requests.get(
@@ -363,15 +383,25 @@ def WEBgetSearchSuggestions(query: str, previous_query: str = '') -> list:
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])
# 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,
"suggestions": suggestions

View File

@@ -23,6 +23,7 @@ version = "0.0.1"
apiVersion = "1"
randomly_generated_passcode = 0
video_cache = {}
general_cache = {"search": []}
def getConfig(configfile):

View File

@@ -633,7 +633,6 @@ def parseViewsFromViewText(viewcounttext: str) -> int:
def search(data, req):
search_query = req.args.get('q')
print(f"search query: {search_query}")
# ignore paginated requests as we do nothing with the continuation token
page = req.args.get('page')
@@ -641,7 +640,6 @@ def search(data, req):
return send(404, [])
if (data[-2].lower() != "search" or data[-1].lower() != "") and data[-1].lower() != "search":
print(f"'{data[-2]}', '{data[-1]}'")
previous_query = req.args.get('pq')
suggestions = ythdd_extractor.WEBgetSearchSuggestions(search_query, previous_query)
return send(200, suggestions)