Block by result title or url using regex (#473)

Allows blocking search results using a regex filter for either
result title or result url
main
Yadomin 2021-10-21 10:01:04 +08:00 committed by GitHub
parent f7b8b30e9d
commit 284a8102c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 3 deletions

View File

@ -45,12 +45,15 @@ class Filter:
def __init__(self, user_key: str, mobile=False, config=None) -> None: def __init__(self, user_key: str, mobile=False, config=None) -> None:
if config is None: if config is None:
config = {} config = {}
self.near = config['near'] if 'near' in config else '' self.near = config['near'] if 'near' in config else ''
self.dark = config['dark'] if 'dark' in config else False self.dark = config['dark'] if 'dark' in config else False
self.nojs = config['nojs'] if 'nojs' in config else False self.nojs = config['nojs'] if 'nojs' in config else False
self.new_tab = config['new_tab'] if 'new_tab' in config else False self.new_tab = config['new_tab'] if 'new_tab' in config else False
self.alt_redirect = config['alts'] if 'alts' in config else False self.alt_redirect = config['alts'] if 'alts' in config else False
self.block_title = (
config['block_title'] if 'block_title' in config else '')
self.block_url = (
config['block_url'] if 'block_url' in config else '')
self.mobile = mobile self.mobile = mobile
self.user_key = user_key self.user_key = user_key
self.main_divs = ResultSet('') self.main_divs = ResultSet('')
@ -87,6 +90,8 @@ class Filter:
def clean(self, soup) -> BeautifulSoup: def clean(self, soup) -> BeautifulSoup:
self.main_divs = soup.find('div', {'id': 'main'}) self.main_divs = soup.find('div', {'id': 'main'})
self.remove_ads() self.remove_ads()
self.remove_block_titles()
self.remove_block_url()
self.collapse_sections() self.collapse_sections()
self.update_styling(soup) self.update_styling(soup)
@ -134,6 +139,28 @@ class Filter:
if has_ad_content(_.text)] if has_ad_content(_.text)]
_ = div.decompose() if len(div_ads) else None _ = div.decompose() if len(div_ads) else None
def remove_block_titles(self) -> None:
if not self.main_divs:
return
if self.block_title == '':
return
block_title = re.compile(self.block_title)
for div in [_ for _ in self.main_divs.find_all('div', recursive=True)]:
block_divs = [_ for _ in div.find_all('h3', recursive=True)
if block_title.search(_.text) is not None]
_ = div.decompose() if len(block_divs) else None
def remove_block_url(self) -> None:
if not self.main_divs:
return
if self.block_url == '':
return
block_url = re.compile(self.block_url)
for div in [_ for _ in self.main_divs.find_all('div', recursive=True)]:
block_divs = [_ for _ in div.find_all('a', recursive=True)
if block_url.search(_.attrs['href']) is not None]
_ = div.decompose() if len(block_divs) else None
def collapse_sections(self) -> None: def collapse_sections(self) -> None:
"""Collapses long result sections ("people also asked", "related """Collapses long result sections ("people also asked", "related
searches", etc) into "details" elements searches", etc) into "details" elements

View File

@ -1,6 +1,7 @@
from app.utils.misc import read_config_bool from app.utils.misc import read_config_bool
from flask import current_app from flask import current_app
import os import os
import re
class Config: class Config:
@ -14,6 +15,8 @@ class Config:
open(os.path.join(app_config['STATIC_FOLDER'], open(os.path.join(app_config['STATIC_FOLDER'],
'css/variables.css')).read()) 'css/variables.css')).read())
self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '') self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '')
self.block_title = os.getenv('WHOOGLE_CONFIG_BLOCK_TITLE', '')
self.block_url = os.getenv('WHOOGLE_CONFIG_BLOCK_URL', '')
self.ctry = os.getenv('WHOOGLE_CONFIG_COUNTRY', '') self.ctry = os.getenv('WHOOGLE_CONFIG_COUNTRY', '')
self.theme = os.getenv('WHOOGLE_CONFIG_THEME', '') self.theme = os.getenv('WHOOGLE_CONFIG_THEME', '')
self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE') self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE')

View File

@ -10,6 +10,10 @@
"config-near-help": "City Name", "config-near-help": "City Name",
"config-block": "Block", "config-block": "Block",
"config-block-help": "Comma-separated site list", "config-block-help": "Comma-separated site list",
"config-block-title": "Block by Title",
"config-block-title-help": "Use regex",
"config-block-url": "Block by URL",
"config-block-url-help": "Use regex",
"config-theme": "Theme", "config-theme": "Theme",
"config-nojs": "Show NoJS Links", "config-nojs": "Show NoJS Links",
"config-dark": "Dark Mode", "config-dark": "Dark Mode",
@ -43,6 +47,10 @@
"config-near-help": "Stad", "config-near-help": "Stad",
"config-block": "Blok", "config-block": "Blok",
"config-block-help": "Lijst met sites met kommas onderscheiden", "config-block-help": "Lijst met sites met kommas onderscheiden",
"config-block-title": "Blokkeren op titel",
"config-block-title-help": "Gebruik regex",
"config-block-url": "Blokkeren op URL",
"config-block-url-help": "Gebruik regex",
"config-theme": "Thema", "config-theme": "Thema",
"config-nojs": "Laat NoJS links zien", "config-nojs": "Laat NoJS links zien",
"config-dark": "Donkere Modus", "config-dark": "Donkere Modus",
@ -76,6 +84,10 @@
"config-near-help": "Stadt-Name", "config-near-help": "Stadt-Name",
"config-block": "Block", "config-block": "Block",
"config-block-help": "Komma-getrennte Liste von Seiten", "config-block-help": "Komma-getrennte Liste von Seiten",
"config-block-title": "Nach Titel blockieren",
"config-block-title-help": "Regex verwenden",
"config-block-url": "Nach URL blockieren",
"config-block-url-help": "Regex verwenden",
"config-theme": "Thema", "config-theme": "Thema",
"config-nojs": "NoJS-Links anzeigen", "config-nojs": "NoJS-Links anzeigen",
"config-dark": "Dark Mode", "config-dark": "Dark Mode",
@ -109,6 +121,10 @@
"config-near-help": "Nombre de la Ciudad", "config-near-help": "Nombre de la Ciudad",
"config-block": "Bloquear", "config-block": "Bloquear",
"config-block-help": "Lista de sitios separados por comas", "config-block-help": "Lista de sitios separados por comas",
"config-block-title": "Bloquear por título",
"config-block-title-help": "Usar expresiones regulares",
"config-block-url": "Bloquear por URL",
"config-block-url-help": "Usar expresiones regulares",
"config-theme": "Tema", "config-theme": "Tema",
"config-nojs": "Mostrar Enlaces NoJS", "config-nojs": "Mostrar Enlaces NoJS",
"config-dark": "Modo Oscuro", "config-dark": "Modo Oscuro",
@ -142,6 +158,10 @@
"config-near-help": "Nome della città", "config-near-help": "Nome della città",
"config-block": "Blocca", "config-block": "Blocca",
"config-block-help": "Lista di siti separati da virgole", "config-block-help": "Lista di siti separati da virgole",
"config-block-title": "Blocca per titolo",
"config-block-title-help": "Usa regex",
"config-block-url": "Blocca per url",
"config-block-url-help": "Usa regex",
"config-theme": "Tema", "config-theme": "Tema",
"config-nojs": "Mostra link NoJS", "config-nojs": "Mostra link NoJS",
"config-dark": "Modalità Notte", "config-dark": "Modalità Notte",
@ -164,7 +184,7 @@
"dark": "notte", "dark": "notte",
"system": "impostazioni di sistema" "system": "impostazioni di sistema"
}, },
"lang_pt": { "lang_pt": {
"search": "Pesquisar", "search": "Pesquisar",
"config": "Configuração", "config": "Configuração",
"config-country": "Filtrar Resultados por País", "config-country": "Filtrar Resultados por País",
@ -175,6 +195,10 @@
"config-near-help": "Nome da Cidade", "config-near-help": "Nome da Cidade",
"config-block": "Bloquear", "config-block": "Bloquear",
"config-block-help": "Lista de sites separados por vírgulas", "config-block-help": "Lista de sites separados por vírgulas",
"config-block-title": "Bloco por título",
"config-block-title-help": "Use regex",
"config-block-url": "Bloquear por url",
"config-block-url-help": "Use regex",
"config-theme": "Tema", "config-theme": "Tema",
"config-nojs": "Mostrar Links NoJS", "config-nojs": "Mostrar Links NoJS",
"config-dark": "Modo Escuro", "config-dark": "Modo Escuro",
@ -208,6 +232,10 @@
"config-near-help": "城市名", "config-near-help": "城市名",
"config-block": "屏蔽", "config-block": "屏蔽",
"config-block-help": "逗号分隔的网站列表", "config-block-help": "逗号分隔的网站列表",
"config-block-title": "按网站标题屏蔽",
"config-block-title-help": "使用正则表达式",
"config-block-url": "按网站链接屏蔽",
"config-block-url-help": "使用正则表达式",
"config-theme": "主题", "config-theme": "主题",
"config-nojs": "显示 NoJS 链接", "config-nojs": "显示 NoJS 链接",
"config-dark": "深色模式", "config-dark": "深色模式",
@ -241,6 +269,10 @@
"config-near-help": "නගරයේ නම", "config-near-help": "නගරයේ නම",
"config-block": "අවහිර", "config-block": "අවහිර",
"config-block-help": "අල්ප විරාම වලින් වෙන් වූ අඩවි ලැයිස්තුව", "config-block-help": "අල්ප විරාම වලින් වෙන් වූ අඩවි ලැයිස්තුව",
"config-block-title": "මාතෘකාව අනුව අවහිර කරන්න",
"config-block-title-help": "රෙජෙක්ස් භාවිතා කරන්න",
"config-block-url": "යූආර්එල් මඟින් අවහිර කරන්න",
"config-block-url-help": "රෙජෙක්ස් භාවිතා කරන්න",
"config-theme": "තේමාව", "config-theme": "තේමාව",
"config-nojs": "නෝජේඑස් සබැඳි පෙන්වන්න", "config-nojs": "නෝජේඑස් සබැඳි පෙන්වන්න",
"config-dark": "අඳුරු ආකාරය", "config-dark": "අඳුරු ආකාරය",
@ -274,6 +306,10 @@
"config-near-help": "Nom de ville", "config-near-help": "Nom de ville",
"config-block": "Bloquer", "config-block": "Bloquer",
"config-block-help": "Liste de sites séparés pas des virgules", "config-block-help": "Liste de sites séparés pas des virgules",
"config-block-title": "Bloquer par titre",
"config-block-title-help": "Utiliser l'expression régulière",
"config-block-url": "Bloquer par URL",
"config-block-url-help": "Utiliser l'expression régulière",
"config-theme": "Theme", "config-theme": "Theme",
"config-nojs": "Montrer les liens NoJS", "config-nojs": "Montrer les liens NoJS",
"config-dark": "Mode Sombre", "config-dark": "Mode Sombre",
@ -307,6 +343,10 @@
"config-near-help": "نام شهر", "config-near-help": "نام شهر",
"config-block": "مسدود کردن", "config-block": "مسدود کردن",
"config-block-help": "لیست سایت‌ها با ویرگول جدا می‌شود.", "config-block-help": "لیست سایت‌ها با ویرگول جدا می‌شود.",
"config-block-title": "مسدود کردن بر اساس عنوان",
"config-block-title-help": "از عبارت منظم استفاده کنید",
"config-block-url": "بلوک بر اساس URL",
"config-block-url-help": "از عبارت منظم استفاده کنید",
"config-theme": "پوسته", "config-theme": "پوسته",
"config-nojs": "نمایش پیوند‌های بدون جاوا اسکیریپت", "config-nojs": "نمایش پیوند‌های بدون جاوا اسکیریپت",
"config-dark": "حالت تاریک", "config-dark": "حالت تاریک",
@ -340,6 +380,10 @@
"config-near-help": "Název města", "config-near-help": "Název města",
"config-block": "Blokovat", "config-block": "Blokovat",
"config-block-help": "Čárkami oddělený seznam stránek", "config-block-help": "Čárkami oddělený seznam stránek",
"config-block-title": "Blokovat podle názvu",
"config-block-title-help": "Použijte regulární výraz",
"config-block-url": "Blokovat podle adresy URL",
"config-block-url-help": "Použijte regulární výraz",
"config-theme": "Motiv", "config-theme": "Motiv",
"config-nojs": "Zobrazit NoJS odkazy", "config-nojs": "Zobrazit NoJS odkazy",
"config-dark": "Tmavý motiv", "config-dark": "Tmavý motiv",
@ -373,6 +417,10 @@
"config-near-help": "城市名", "config-near-help": "城市名",
"config-block": "排除", "config-block": "排除",
"config-block-help": "網址列表,以逗號分隔", "config-block-help": "網址列表,以逗號分隔",
"config-block-title": "按標題屏蔽",
"config-block-title-help": "使用正則表達式",
"config-block-url": "按網址屏蔽",
"config-block-url-help": "使用正則表達式",
"config-theme": "主題", "config-theme": "主題",
"config-nojs": "顯示 NoJS 連結", "config-nojs": "顯示 NoJS 連結",
"config-dark": "深色模式", "config-dark": "深色模式",
@ -406,6 +454,10 @@
"config-near-help": "Име на град", "config-near-help": "Име на град",
"config-block": "Блокирани сайтове", "config-block": "Блокирани сайтове",
"config-block-help": "Списък сайтове, разделени със запетая", "config-block-help": "Списък сайтове, разделени със запетая",
"config-block-title": "Блокиране по заглавие",
"config-block-title-help": "Използвайте регулярно изражение",
"config-block-url": "Блокиране по url",
"config-block-url-help": "Използвайте регулярно изражение",
"config-theme": "Стил", "config-theme": "Стил",
"config-nojs": "Показване на връзки без JS", "config-nojs": "Показване на връзки без JS",
"config-dark": "Тъмен режим", "config-dark": "Тъмен режим",
@ -439,6 +491,10 @@
"config-near-help": "शहर का नाम", "config-near-help": "शहर का नाम",
"config-block": "खंड", "config-block": "खंड",
"config-block-help": "अल्पविराम से अलग की गई साइट सूची", "config-block-help": "अल्पविराम से अलग की गई साइट सूची",
"config-block-title": "शीर्षक के अनुसार ब्लॉक करें",
"config-block-title-help": "रेगेक्स का प्रयोग करें",
"config-block-url": "url द्वारा अवरोधित करें",
"config-block-url-help": "रेगेक्स का प्रयोग करें",
"config-theme": "विषय", "config-theme": "विषय",
"config-nojs": "NoJS लिंक दिखाएं", "config-nojs": "NoJS लिंक दिखाएं",
"config-dark": "डार्क मोड", "config-dark": "डार्क मोड",
@ -460,5 +516,5 @@
"light": "रोशनी", "light": "रोशनी",
"dark": "अंधेरा", "dark": "अंधेरा",
"system": "प्रणाली" "system": "प्रणाली"
} }
} }

View File

@ -122,6 +122,14 @@
<label for="config-block">{{ translation['config-block'] }}: </label> <label for="config-block">{{ translation['config-block'] }}: </label>
<input type="text" name="block" id="config-block" placeholder="{{ translation['config-block-help'] }}" value="{{ config.block }}"> <input type="text" name="block" id="config-block" placeholder="{{ translation['config-block-help'] }}" value="{{ config.block }}">
</div> </div>
<div class="config-div config-div-block">
<label for="config-block-title">{{ translation['config-block-title'] }}: </label>
<input type="text" name="block_title" id="config-block" placeholder="{{ translation['config-block-title-help'] }}" value="{{ config.block_title }}">
</div>
<div class="config-div config-div-block">
<label for="config-block-url">{{ translation['config-block-url'] }}: </label>
<input type="text" name="block_url" id="config-block" placeholder="{{ translation['config-block-url-help'] }}" value="{{ config.block_url }}">
</div>
<div class="config-div config-div-nojs"> <div class="config-div config-div-nojs">
<label for="config-nojs">{{ translation['config-nojs'] }}: </label> <label for="config-nojs">{{ translation['config-nojs'] }}: </label>
<input type="checkbox" name="nojs" id="config-nojs" {{ 'checked' if config.nojs else '' }}> <input type="checkbox" name="nojs" id="config-nojs" {{ 'checked' if config.nojs else '' }}>