[fix] engines: update brave images/videos parser and news xpath

This commit is contained in:
vojkovic
2026-09-05 13:41:56 +00:00
committed by Brock Vojkovic
parent eaf1fcb349
commit 8b01679e8f
2 changed files with 17 additions and 17 deletions

View File

@@ -248,13 +248,13 @@ def extract_json_data(text: str) -> dict[str, t.Any]:
# node_ids: [0, 19], # node_ids: [0, 19],
# data: [{type:"data",data: .... ["q","goggles_id"],route:1,url:1}}] # data: [{type:"data",data: .... ["q","goggles_id"],route:1,url:1}}]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
text = text[text.index("<script") : text.index("</script")] # form: null,
if not text: # error: null
raise ValueError("can't find JS/JSON data in the given text") # });
start = text.index("data: [{") start = text.index("data: [{")
end = text.rindex("}}]") newline = text.index("\n", start)
js_obj_str = text[start:end] end = text.rindex("}}]", start, newline)
js_obj_str = "{" + js_obj_str + "}}]}" js_obj_str = "{" + text[start:end] + "}}]}"
# js_obj_str = js_obj_str.replace("\xa0", "") # remove ASCII for &nbsp; # js_obj_str = js_obj_str.replace("\xa0", "") # remove ASCII for &nbsp;
# js_obj_str = js_obj_str.replace(r"\u003C", "<").replace(r"\u003c", "<") # fix broken HTML tags in strings # js_obj_str = js_obj_str.replace(r"\u003C", "<").replace(r"\u003c", "<") # fix broken HTML tags in strings
json_str = js_obj_str_to_json_str(js_obj_str) json_str = js_obj_str_to_json_str(js_obj_str)
@@ -354,14 +354,14 @@ def _parse_news(resp: SXNG_Response) -> EngineResults:
res = EngineResults() res = EngineResults()
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
for result in eval_xpath_list(dom, "//div[contains(@class, 'results')]//div[@data-type='news']"): for result in eval_xpath_list(dom, "//div[@data-type='news']"):
url = eval_xpath_getindex(result, ".//a[contains(@class, 'result-header')]/@href", 0, default=None) url = eval_xpath_getindex(result, ".//a/@href", 0, default=None)
if url is None: if url is None:
continue continue
title = eval_xpath_list(result, ".//span[contains(@class, 'snippet-title')]") title = eval_xpath_list(result, ".//div[contains(@class, 'title')]")
content = eval_xpath_list(result, ".//p[contains(@class, 'desc')]") content = eval_xpath_list(result, ".//div[contains(@class, 'description')]")
thumbnail = eval_xpath_getindex(result, ".//div[contains(@class, 'image-wrapper')]//img/@src", 0, default="") thumbnail = eval_xpath_getindex(result, ".//a[contains(@class, 'thumbnail')]//img/@src", 0, default="")
item = res.types.LegacyResult( item = res.types.LegacyResult(
template="default.html", template="default.html",

View File

@@ -735,13 +735,13 @@ def js_obj_str_to_json_str(js_obj_str: str) -> str:
if in_string == "'": if in_string == "'":
p = p.replace('"', r'\"') p = p.replace('"', r'\"')
parts[i] = p parts[i] = p
# deal with the sequence blackslash then quote # drop a trailing \ that was escaping the quote
# since js_obj_str splits on quote, we detect this case: # leave it if it has been escaped twice as a literal i.e. two \ and ' in a row
# * the previous part ends with a black slash
# * the current part is a single quote
# when detected the blackslash is removed on the previous part
if blackslash_just_before and p[:1] == "'": if blackslash_just_before and p[:1] == "'":
parts[i - 1] = parts[i - 1][:-1] prev = parts[i - 1]
num_backslashes = len(prev) - len(prev.rstrip("\\"))
if num_backslashes % 2 == 1:
parts[i - 1] = prev[:-1]
elif in_string is None and p in ('"', "'", "`"): elif in_string is None and p in ('"', "'", "`"):
# we are not in string but p is string delimiter # we are not in string but p is string delimiter