refactor: support search caching
remains to be seen whether this is a good idea or not
This commit is contained in:
@@ -348,14 +348,34 @@ def WEBgetSearchSuggestions(query: str, previous_query: str = '') -> list:
|
|||||||
# Takes in a search query and returns relevant suggestions.
|
# Takes in a search query and returns relevant suggestions.
|
||||||
# Can optionally take the previous query but that's rather novel and
|
# Can optionally take the previous query but that's rather novel and
|
||||||
# not supported across players nor invidious API itself.
|
# 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 = {
|
params = {
|
||||||
'ds': 'yt',
|
'ds': 'yt',
|
||||||
'hl': 'en', # host language
|
'hl': 'en', # host language
|
||||||
'gl': 'us', # geolocation
|
'gl': 'us', # geolocation
|
||||||
'client': 'youtube',
|
'client': 'youtube',
|
||||||
'gs_ri': 'youtube',
|
'gs_ri': 'youtube',
|
||||||
'q': str(query), # query
|
'q': query, # query
|
||||||
'pq': str(previous_query) if previous_query is not None else '' # previous query
|
'pq': previous_query # previous query
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
@@ -363,15 +383,25 @@ def WEBgetSearchSuggestions(query: str, previous_query: str = '') -> list:
|
|||||||
params=params,
|
params=params,
|
||||||
headers=stage2_headers
|
headers=stage2_headers
|
||||||
)
|
)
|
||||||
|
|
||||||
# can break anytime but hopefully the tiny speed gain will make up for it
|
# can break anytime but hopefully the tiny speed gain will make up for it
|
||||||
results = response.text[23 + len(query):]
|
results = response.text[23 + len(query):]
|
||||||
results = results[:results.rfind("{") - 1]
|
results = results[:results.rfind("{") - 1]
|
||||||
results = json.loads(results)
|
results = json.loads(results)
|
||||||
|
|
||||||
suggestions = []
|
|
||||||
for result in results:
|
for result in results:
|
||||||
suggestions.append(result[0])
|
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 {
|
return {
|
||||||
"query": query,
|
"query": query,
|
||||||
"suggestions": suggestions
|
"suggestions": suggestions
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ version = "0.0.1"
|
|||||||
apiVersion = "1"
|
apiVersion = "1"
|
||||||
randomly_generated_passcode = 0
|
randomly_generated_passcode = 0
|
||||||
video_cache = {}
|
video_cache = {}
|
||||||
|
general_cache = {"search": []}
|
||||||
|
|
||||||
def getConfig(configfile):
|
def getConfig(configfile):
|
||||||
|
|
||||||
|
|||||||
@@ -633,7 +633,6 @@ def parseViewsFromViewText(viewcounttext: str) -> int:
|
|||||||
|
|
||||||
def search(data, req):
|
def search(data, req):
|
||||||
search_query = req.args.get('q')
|
search_query = req.args.get('q')
|
||||||
print(f"search query: {search_query}")
|
|
||||||
|
|
||||||
# ignore paginated requests as we do nothing with the continuation token
|
# ignore paginated requests as we do nothing with the continuation token
|
||||||
page = req.args.get('page')
|
page = req.args.get('page')
|
||||||
@@ -641,7 +640,6 @@ def search(data, req):
|
|||||||
return send(404, [])
|
return send(404, [])
|
||||||
|
|
||||||
if (data[-2].lower() != "search" or data[-1].lower() != "") and data[-1].lower() != "search":
|
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')
|
previous_query = req.args.get('pq')
|
||||||
suggestions = ythdd_extractor.WEBgetSearchSuggestions(search_query, previous_query)
|
suggestions = ythdd_extractor.WEBgetSearchSuggestions(search_query, previous_query)
|
||||||
return send(200, suggestions)
|
return send(200, suggestions)
|
||||||
|
|||||||
Reference in New Issue
Block a user