1
0
mirror of https://github.com/searx/searx synced 2024-12-12 08:46:26 +01:00

Add hostname_replace plugin

This commit is contained in:
3nprob 2021-04-05 15:43:43 +09:00 committed by Noémi Ványi
parent c00a33feee
commit fc4bf4bf10
3 changed files with 42 additions and 0 deletions

View File

@ -32,6 +32,7 @@ from searx.plugins import (oa_doi_rewrite,
https_rewrite, https_rewrite,
infinite_scroll, infinite_scroll,
self_info, self_info,
hostname_replace,
search_on_category_select, search_on_category_select,
tracker_url_remover, tracker_url_remover,
vim_hotkeys) vim_hotkeys)
@ -168,6 +169,7 @@ plugins.register(hash_plugin)
plugins.register(https_rewrite) plugins.register(https_rewrite)
plugins.register(infinite_scroll) plugins.register(infinite_scroll)
plugins.register(self_info) plugins.register(self_info)
plugins.register(hostname_replace)
plugins.register(search_on_category_select) plugins.register(search_on_category_select)
plugins.register(tracker_url_remover) plugins.register(tracker_url_remover)
plugins.register(vim_hotkeys) plugins.register(vim_hotkeys)

View File

@ -0,0 +1,28 @@
import re
from urllib.parse import urlunparse
from searx import settings
from searx.plugins import logger
from flask_babel import gettext
name = gettext('Hostname replace')
description = gettext('Rewrite result hostnames')
default_on = False
preference_section = 'general'
plugin_id = 'hostname_replace'
parsed = 'parsed_url'
replacements = {re.compile(p): r for (p, r) in settings[plugin_id].items()} if plugin_id in settings else {}
logger = logger.getChild(plugin_id)
def on_result(request, search, result):
if parsed not in result:
return True
for (pattern, replacement) in replacements.items():
if pattern.search(result[parsed].netloc):
result[parsed] = result[parsed]._replace(netloc=pattern.sub(replacement, result[parsed].netloc))
result['url'] = urlunparse(result[parsed])
return True

View File

@ -104,6 +104,18 @@ outgoing: # communication with search engines
# - "HTTPS rewrite" # - "HTTPS rewrite"
# - ... # - ...
# Example to rewrite hostnames in external links
#
# enabled_plugins:
# - 'Hostname replace'
# hostname_replace:
# '(.*\.)?youtube\.com$': 'invidious.example.com'
# '(.*\.)?youtu\.be$': 'invidious.example.com'
# '(.*\.)?youtube-noocookie\.com$': 'yotter.example.com'
# '(.*\.)?reddit\.com$': 'teddit.example.com'
# '(.*\.)?redd\.it$': 'teddit.example.com'
# '(www\.)?twitter\.com$': 'nitter.example.com'
checker: checker:
# disable checker when in debug mode # disable checker when in debug mode
off_when_debug: True off_when_debug: True