From b9a2e8b387212d35cc83da50a07ee8fa47d2a56e Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Mon, 7 Feb 2022 16:45:48 +0100 Subject: [PATCH] [mod] hostname_replace: replace hostnames in result's data_src param To test you need to redirect embeded videos (e.g.) from youtube to a invidios instance. Search for videos using engine `!youtube lebowski`. The result URLs and the embeded videos should link to the invidios instance. Here is an example of such a `hostname_replace` configuration:: hostname_replace: # youtube --> Invidious '(.*\.)?youtube-nocookie\.com': 'invidio.xamh.de' '(.*\.)?youtube\.com$': 'invidio.xamh.de' '(.*\.)?invidious\.snopyta\.org$': 'invidio.xamh.de' '(.*\.)?vid\.puffyan\.us': 'invidio.xamh.de' '(.*\.)?invidious\.kavin\.rocks$': 'invidio.xamh.de' '(.*\.)?inv\.riverside\.rocks$': 'invidio.xamh.de' Closes: https://github.com/searxng/searxng/issues/873 Signed-off-by: Markus Heiser --- searx/plugins/hostname_replace.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/searx/plugins/hostname_replace.py b/searx/plugins/hostname_replace.py index 778b84615..e22b2dca0 100644 --- a/searx/plugins/hostname_replace.py +++ b/searx/plugins/hostname_replace.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: AGPL-3.0-or-later import re -from urllib.parse import urlunparse +from urllib.parse import urlunparse, urlparse from searx import settings from searx.plugins import logger from flask_babel import gettext @@ -28,5 +28,10 @@ def on_result(request, search, result): return False result[parsed] = result[parsed]._replace(netloc=pattern.sub(replacement, result[parsed].netloc)) result['url'] = urlunparse(result[parsed]) + if result.get('data_src', False): + parsed_data_src = urlparse(result['data_src']) + if pattern.search(parsed_data_src.netloc): + parsed_data_src = parsed_data_src._replace(netloc=pattern.sub(replacement, parsed_data_src.netloc)) + result['data_src'] = urlunparse(parsed_data_src) return True