Merge branch 'benbusby:main' into main

This commit is contained in:
hryxl 2022-03-03 00:24:29 +03:00 committed by GitHub
commit dbe20c5f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 9 deletions

View File

@ -349,7 +349,12 @@ class Filter:
if any(url in link['href'] for url in unsupported_g_pages): if any(url in link['href'] for url in unsupported_g_pages):
# FIXME: The "Shopping" tab requires further filtering (see #136) # FIXME: The "Shopping" tab requires further filtering (see #136)
# Temporarily removing all links to that tab for now. # Temporarily removing all links to that tab for now.
link.decompose() parent = link.parent
while parent:
p_cls = parent.attrs.get('class') or []
if parent.name == 'footer' or f'{GClasses.footer}' in p_cls:
link.decompose()
parent = parent.parent
return return
# Replace href with only the intended destination (no "utm" type tags) # Replace href with only the intended destination (no "utm" type tags)

View File

@ -11,6 +11,7 @@ class GClasses(Enum):
""" """
main_tbm_tab = 'KP7LCb' main_tbm_tab = 'KP7LCb'
images_tbm_tab = 'n692Zd' images_tbm_tab = 'n692Zd'
footer = 'TuS8Ad'
def __str__(self): def __str__(self):
return self.value return self.value

View File

@ -1,6 +1,6 @@
from app.models.config import Config from app.models.config import Config
from datetime import datetime from datetime import datetime
import xml.etree.ElementTree as ET from defusedxml import ElementTree as ET
import random import random
import requests import requests
from requests import Response, ConnectionError from requests import Response, ConnectionError

View File

@ -304,8 +304,8 @@ def search():
search_util = Search(request, g.user_config, g.session_key) search_util = Search(request, g.user_config, g.session_key)
query = search_util.new_search_query() query = search_util.new_search_query()
bang = resolve_bang(query=query, bangs_dict=bang_json) bang = resolve_bang(query, bang_json, url_for('.index'))
if bang != '': if bang:
return redirect(bang) return redirect(bang)
# Redirect to home if invalid/blank search # Redirect to home if invalid/blank search

View File

@ -38,7 +38,7 @@ def gen_bangs_json(bangs_file: str) -> None:
print('* Finished creating ddg bangs json') print('* Finished creating ddg bangs json')
def resolve_bang(query: str, bangs_dict: dict) -> str: def resolve_bang(query: str, bangs_dict: dict, fallback: str) -> str:
"""Transform's a user's query to a bang search, if an operator is found """Transform's a user's query to a bang search, if an operator is found
Args: Args:
@ -59,8 +59,14 @@ def resolve_bang(query: str, bangs_dict: dict) -> str:
if operator not in split_query \ if operator not in split_query \
and operator[1:] + operator[0] not in split_query: and operator[1:] + operator[0] not in split_query:
continue continue
return bangs_dict[operator]['url'].replace(
'{}', bang_query = query.replace(
query.replace(operator if operator in split_query operator if operator in split_query else operator[1:] +
else operator[1:] + operator[0], '').strip(), 1) operator[0], ''
).strip()
if bang_query:
return bangs_dict[operator]['url'].replace('{}', bang_query, 1)
else:
return fallback
return '' return ''

View File

@ -6,6 +6,7 @@ cffi==1.15.0
chardet==3.0.4 chardet==3.0.4
click==8.0.3 click==8.0.3
cryptography==3.3.2 cryptography==3.3.2
defusedxml==0.7.1
Flask==1.1.1 Flask==1.1.1
Flask-Session==0.4.0 Flask-Session==0.4.0
idna==2.9 idna==2.9

View File

@ -19,6 +19,7 @@ include_package_data = True
install_requires= install_requires=
beautifulsoup4 beautifulsoup4
cryptography cryptography
defusedxml
Flask Flask
Flask-Session Flask-Session
python-dotenv python-dotenv

View File

@ -47,6 +47,11 @@ def test_ddg_bang(client):
assert rv._status_code == 302 assert rv._status_code == 302
assert rv.headers.get('Location').startswith('https://github.com') assert rv.headers.get('Location').startswith('https://github.com')
# Ensure bang without content doesn't redirect to the result
rv = client.get(f'/{Endpoint.search}?q=!gh')
assert rv._status_code == 302
assert not rv.headers.get('Location').startswith('https://github.com')
def test_config(client): def test_config(client):
rv = client.post(f'/{Endpoint.config}', data=demo_config) rv = client.post(f'/{Endpoint.config}', data=demo_config)