Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block title or url by regex #473

Merged
merged 7 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion app/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class Filter:
def __init__(self, user_key: str, mobile=False, config=None) -> None:
if config is None:
config = {}

else:
self.config = config
YadominJinta marked this conversation as resolved.
Show resolved Hide resolved
self.near = config['near'] if 'near' in config else ''
self.dark = config['dark'] if 'dark' in config else False
self.nojs = config['nojs'] if 'nojs' in config else False
Expand Down Expand Up @@ -87,6 +88,8 @@ def encrypt_path(self, path, is_element=False) -> str:
def clean(self, soup) -> BeautifulSoup:
self.main_divs = soup.find('div', {'id': 'main'})
self.remove_ads()
self.remove_block_titles()
self.remove_block_url()
self.collapse_sections()
self.update_styling(soup)

Expand Down Expand Up @@ -134,6 +137,28 @@ def remove_ads(self) -> None:
if has_ad_content(_.text)]
_ = div.decompose() if len(div_ads) else None

def remove_block_titles(self) -> None:
if not self.main_divs:
return
if self.config.block_title == '':
return
block_title = re.compile(self.config.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.config.block_url == '':
return
block_url = re.compile(self.config.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:
"""Collapses long result sections ("people also asked", "related
searches", etc) into "details" elements
Expand Down
3 changes: 3 additions & 0 deletions app/models/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app.utils.misc import read_config_bool
from flask import current_app
import os
import re


class Config:
Expand All @@ -14,6 +15,8 @@ def __init__(self, **kwargs):
open(os.path.join(app_config['STATIC_FOLDER'],
'css/variables.css')).read())
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.theme = os.getenv('WHOOGLE_CONFIG_THEME', '')
self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE')
Expand Down
4 changes: 4 additions & 0 deletions app/static/settings/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"config-near-help": "City Name",
"config-block": "Block",
"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",
YadominJinta marked this conversation as resolved.
Show resolved Hide resolved
"config-theme": "Theme",
"config-nojs": "Show NoJS Links",
"config-dark": "Dark Mode",
Expand Down
8 changes: 8 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@
<label for="config-block">{{ translation['config-block'] }}: </label>
<input type="text" name="block" id="config-block" placeholder="{{ translation['config-block-help'] }}" value="{{ config.block }}">
</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">
<label for="config-nojs">{{ translation['config-nojs'] }}: </label>
<input type="checkbox" name="nojs" id="config-nojs" {{ 'checked' if config.nojs else '' }}>
Expand Down