2020-04-29 00:19:34 +00:00
|
|
|
from app.request import VALID_PARAMS
|
2021-03-08 17:22:04 +00:00
|
|
|
from app.utils.results import *
|
2021-03-24 19:13:52 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from bs4.element import ResultSet, Tag
|
2020-04-29 00:19:34 +00:00
|
|
|
from cryptography.fernet import Fernet
|
2020-04-10 20:52:27 +00:00
|
|
|
import re
|
|
|
|
import urllib.parse as urlparse
|
|
|
|
from urllib.parse import parse_qs
|
|
|
|
|
2020-06-02 18:54:47 +00:00
|
|
|
|
2020-04-15 23:41:53 +00:00
|
|
|
class Filter:
|
2021-04-01 04:23:30 +00:00
|
|
|
def __init__(self, user_key: str, mobile=False, config=None) -> None:
|
2020-04-15 23:41:53 +00:00
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
|
2020-05-12 23:15:53 +00:00
|
|
|
self.near = config['near'] if 'near' in config else ''
|
2020-04-17 00:50:31 +00:00
|
|
|
self.dark = config['dark'] if 'dark' in config else False
|
2020-04-16 16:01:02 +00:00
|
|
|
self.nojs = config['nojs'] if 'nojs' in config else False
|
2020-05-15 22:10:31 +00:00
|
|
|
self.new_tab = config['new_tab'] if 'new_tab' in config else False
|
2020-07-26 17:53:59 +00:00
|
|
|
self.alt_redirect = config['alts'] if 'alts' in config else False
|
2020-04-16 16:01:02 +00:00
|
|
|
self.mobile = mobile
|
2021-04-01 04:23:30 +00:00
|
|
|
self.user_key = user_key
|
2020-06-02 18:54:47 +00:00
|
|
|
self.main_divs = ResultSet('')
|
|
|
|
self._elements = 0
|
2020-04-15 23:41:53 +00:00
|
|
|
|
2020-04-24 02:59:43 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return getattr(self, name)
|
|
|
|
|
2020-06-02 18:54:47 +00:00
|
|
|
@property
|
|
|
|
def elements(self):
|
|
|
|
return self._elements
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def reskin(self, page: str) -> str:
|
2020-04-15 23:41:53 +00:00
|
|
|
# Aesthetic only re-skinning
|
2020-04-17 00:50:31 +00:00
|
|
|
if self.dark:
|
2020-12-17 21:06:47 +00:00
|
|
|
page = page.replace(
|
|
|
|
'fff', '000').replace(
|
|
|
|
'202124', 'ddd').replace(
|
|
|
|
'1967D2', '3b85ea')
|
2020-04-15 23:41:53 +00:00
|
|
|
|
|
|
|
return page
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def encrypt_path(self, path, is_element=False) -> str:
|
2020-06-02 18:54:47 +00:00
|
|
|
# Encrypts path to avoid plaintext results in logs
|
|
|
|
if is_element:
|
2020-12-17 21:06:47 +00:00
|
|
|
# Element paths are encrypted separately from text, to allow key
|
|
|
|
# regeneration once all items have been served to the user
|
2021-04-01 04:23:30 +00:00
|
|
|
enc_path = Fernet(self.user_key).encrypt(path.encode()).decode()
|
2020-06-02 18:54:47 +00:00
|
|
|
self._elements += 1
|
|
|
|
return enc_path
|
|
|
|
|
2021-04-01 04:23:30 +00:00
|
|
|
return Fernet(self.user_key).encrypt(path.encode()).decode()
|
2020-06-02 18:54:47 +00:00
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def clean(self, soup) -> BeautifulSoup:
|
2020-06-02 18:54:47 +00:00
|
|
|
self.main_divs = soup.find('div', {'id': 'main'})
|
|
|
|
self.remove_ads()
|
|
|
|
self.fix_question_section()
|
2020-04-29 00:19:34 +00:00
|
|
|
self.update_styling(soup)
|
2020-06-02 18:54:47 +00:00
|
|
|
|
|
|
|
for img in [_ for _ in soup.find_all('img') if 'src' in _.attrs]:
|
|
|
|
self.update_element_src(img, 'image/png')
|
|
|
|
|
|
|
|
for audio in [_ for _ in soup.find_all('audio') if 'src' in _.attrs]:
|
|
|
|
self.update_element_src(audio, 'audio/mpeg')
|
|
|
|
|
|
|
|
for link in soup.find_all('a', href=True):
|
|
|
|
self.update_link(link)
|
2020-04-29 00:19:34 +00:00
|
|
|
|
|
|
|
input_form = soup.find('form')
|
2020-04-29 00:59:33 +00:00
|
|
|
if input_form is not None:
|
|
|
|
input_form['method'] = 'POST'
|
2020-04-29 00:19:34 +00:00
|
|
|
|
2020-04-29 15:46:18 +00:00
|
|
|
# Ensure no extra scripts passed through
|
|
|
|
for script in soup('script'):
|
|
|
|
script.decompose()
|
|
|
|
|
2020-06-07 20:06:49 +00:00
|
|
|
# Update default footer and header
|
|
|
|
footer = soup.find('footer')
|
2020-05-24 20:03:11 +00:00
|
|
|
if footer:
|
2020-06-07 20:06:49 +00:00
|
|
|
# Remove divs that have multiple links beyond just page navigation
|
2020-12-17 21:06:47 +00:00
|
|
|
[_.decompose() for _ in footer.find_all('div', recursive=False)
|
|
|
|
if len(_.find_all('a', href=True)) > 3]
|
2020-04-29 15:46:18 +00:00
|
|
|
|
2020-05-24 20:03:11 +00:00
|
|
|
header = soup.find('header')
|
|
|
|
if header:
|
|
|
|
header.decompose()
|
|
|
|
|
2020-04-15 23:41:53 +00:00
|
|
|
return soup
|
2020-04-29 00:19:34 +00:00
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def remove_ads(self) -> None:
|
|
|
|
"""Removes ads found in the list of search result divs
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None (The soup object is modified directly)
|
|
|
|
"""
|
2020-06-02 18:54:47 +00:00
|
|
|
if not self.main_divs:
|
2020-04-29 16:03:34 +00:00
|
|
|
return
|
2020-04-29 00:19:34 +00:00
|
|
|
|
2020-06-02 18:54:47 +00:00
|
|
|
for div in [_ for _ in self.main_divs.find_all('div', recursive=True)]:
|
2020-12-17 21:06:47 +00:00
|
|
|
div_ads = [_ for _ in div.find_all('span', recursive=True)
|
|
|
|
if has_ad_content(_.text)]
|
|
|
|
_ = div.decompose() if len(div_ads) else None
|
2020-04-29 00:19:34 +00:00
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def fix_question_section(self) -> None:
|
|
|
|
"""Collapses the "People Also Asked" section into a "details" element
|
|
|
|
|
|
|
|
These sections are typically the only sections in the results page that
|
|
|
|
are structured as <div><h2>Title</h2><div>...</div></div>, so they are
|
|
|
|
extracted by checking all result divs for h2 children.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None (The soup object is modified directly)
|
|
|
|
"""
|
2020-06-02 18:54:47 +00:00
|
|
|
if not self.main_divs:
|
|
|
|
return
|
|
|
|
|
2020-12-15 16:09:48 +00:00
|
|
|
question_divs = [_ for _ in self.main_divs.find_all(
|
|
|
|
'div', recursive=False
|
2020-12-17 21:06:47 +00:00
|
|
|
) if len(_.find_all('h2')) > 0]
|
2020-12-15 16:09:48 +00:00
|
|
|
|
|
|
|
if len(question_divs) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Wrap section in details element to allow collapse/expand
|
2020-12-29 23:43:42 +00:00
|
|
|
details = BeautifulSoup(features='html.parser').new_tag('details')
|
|
|
|
summary = BeautifulSoup(features='html.parser').new_tag('summary')
|
2020-12-15 16:09:48 +00:00
|
|
|
summary.string = question_divs[0].find('h2').text
|
|
|
|
question_divs[0].find('h2').decompose()
|
|
|
|
details.append(summary)
|
|
|
|
question_divs[0].wrap(details)
|
|
|
|
|
2020-06-07 20:06:49 +00:00
|
|
|
for question_div in question_divs:
|
2020-12-15 16:09:48 +00:00
|
|
|
questions = [_ for _ in question_div.find_all(
|
|
|
|
'div', recursive=True
|
2020-12-17 21:06:47 +00:00
|
|
|
) if _.text.endswith('?')]
|
2020-12-15 16:09:48 +00:00
|
|
|
|
2020-06-02 18:54:47 +00:00
|
|
|
for question in questions:
|
|
|
|
question['style'] = 'padding: 10px; font-style: italic;'
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def update_element_src(self, element: Tag, mime: str) -> None:
|
|
|
|
"""Encrypts the original src of an element and rewrites the element src
|
|
|
|
to use the "/element?src=" pass-through.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None (The soup element is modified directly)
|
|
|
|
|
|
|
|
"""
|
2021-02-20 20:04:32 +00:00
|
|
|
src = element['src']
|
|
|
|
|
|
|
|
if src.startswith('//'):
|
|
|
|
src = 'https:' + src
|
|
|
|
|
|
|
|
if src.startswith(LOGO_URL):
|
2020-06-02 18:54:47 +00:00
|
|
|
# Re-brand with Whoogle logo
|
2020-10-29 15:09:31 +00:00
|
|
|
element['src'] = 'static/img/logo.png'
|
2020-06-02 18:54:47 +00:00
|
|
|
element['style'] = 'height:40px;width:162px'
|
|
|
|
return
|
2021-02-20 20:04:32 +00:00
|
|
|
elif src.startswith(GOOG_IMG) or GOOG_STATIC in src:
|
2020-06-02 18:54:47 +00:00
|
|
|
element['src'] = BLANK_B64
|
|
|
|
return
|
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
element['src'] = 'element?url=' + self.encrypt_path(
|
2021-02-20 20:04:32 +00:00
|
|
|
src,
|
2020-12-17 21:06:47 +00:00
|
|
|
is_element=True) + '&type=' + urlparse.quote(mime)
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def update_styling(self, soup) -> None:
|
|
|
|
""""""
|
2020-04-29 00:19:34 +00:00
|
|
|
# Remove unnecessary button(s)
|
|
|
|
for button in soup.find_all('button'):
|
|
|
|
button.decompose()
|
|
|
|
|
|
|
|
# Remove svg logos
|
|
|
|
for svg in soup.find_all('svg'):
|
|
|
|
svg.decompose()
|
|
|
|
|
|
|
|
# Update logo
|
|
|
|
logo = soup.find('a', {'class': 'l'})
|
|
|
|
if logo and self.mobile:
|
2020-12-17 21:06:47 +00:00
|
|
|
logo['style'] = ('display:flex; justify-content:center; '
|
|
|
|
'align-items:center; color:#685e79; '
|
|
|
|
'font-size:18px; ')
|
2020-04-29 00:19:34 +00:00
|
|
|
|
|
|
|
# Fix search bar length on mobile
|
|
|
|
try:
|
|
|
|
search_bar = soup.find('header').find('form').find('div')
|
|
|
|
search_bar['style'] = 'width: 100%;'
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2021-03-24 19:13:52 +00:00
|
|
|
def update_link(self, link: Tag) -> None:
|
|
|
|
"""Update internal link paths with encrypted path, otherwise remove
|
|
|
|
unnecessary redirects and/or marketing params from the url
|
|
|
|
|
|
|
|
Args:
|
|
|
|
link: A bs4 Tag element to inspect and update
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None (the tag is updated directly)
|
|
|
|
|
|
|
|
"""
|
2020-06-02 18:54:47 +00:00
|
|
|
# Replace href with only the intended destination (no "utm" type tags)
|
|
|
|
href = link['href'].replace('https://www.google.com', '')
|
2020-10-29 15:09:31 +00:00
|
|
|
if 'advanced_search' in href or 'tbm=shop' in href:
|
2020-12-17 21:06:47 +00:00
|
|
|
# FIXME: The "Shopping" tab requires further filtering (see #136)
|
2020-10-25 17:52:30 +00:00
|
|
|
# Temporarily removing all links to that tab for now.
|
2020-06-02 18:54:47 +00:00
|
|
|
link.decompose()
|
|
|
|
return
|
|
|
|
elif self.new_tab:
|
|
|
|
link['target'] = '_blank'
|
|
|
|
|
|
|
|
result_link = urlparse.urlparse(href)
|
2020-12-17 21:06:47 +00:00
|
|
|
query_link = parse_qs(
|
|
|
|
result_link.query
|
|
|
|
)['q'][0] if '?q=' in href else ''
|
2020-06-02 18:54:47 +00:00
|
|
|
|
|
|
|
if query_link.startswith('/'):
|
2020-12-17 21:06:47 +00:00
|
|
|
# Internal google links (i.e. mail, maps, etc) should still
|
|
|
|
# be forwarded to Google
|
2020-06-02 18:54:47 +00:00
|
|
|
link['href'] = 'https://google.com' + query_link
|
|
|
|
elif '/search?q=' in href:
|
2020-12-17 21:06:47 +00:00
|
|
|
# "li:1" implies the query should be interpreted verbatim,
|
|
|
|
# which is accomplished by wrapping the query in double quotes
|
2020-07-26 17:53:59 +00:00
|
|
|
if 'li:1' in href:
|
|
|
|
query_link = '"' + query_link + '"'
|
2020-10-29 15:09:31 +00:00
|
|
|
new_search = 'search?q=' + self.encrypt_path(query_link)
|
2020-06-02 18:54:47 +00:00
|
|
|
|
|
|
|
query_params = parse_qs(urlparse.urlparse(href).query)
|
|
|
|
for param in VALID_PARAMS:
|
2020-12-17 21:06:47 +00:00
|
|
|
if param not in query_params:
|
|
|
|
continue
|
|
|
|
param_val = query_params[param][0]
|
2020-06-02 18:54:47 +00:00
|
|
|
new_search += '&' + param + '=' + param_val
|
|
|
|
link['href'] = new_search
|
|
|
|
elif 'url?q=' in href:
|
|
|
|
# Strip unneeded arguments
|
|
|
|
link['href'] = filter_link_args(query_link)
|
|
|
|
|
|
|
|
# Add no-js option
|
|
|
|
if self.nojs:
|
2021-03-08 17:22:04 +00:00
|
|
|
append_nojs(link)
|
2020-06-02 18:54:47 +00:00
|
|
|
else:
|
|
|
|
link['href'] = href
|
|
|
|
|
2020-07-26 17:53:59 +00:00
|
|
|
# Replace link location if "alts" config is enabled
|
|
|
|
if self.alt_redirect:
|
2020-12-17 21:06:47 +00:00
|
|
|
# Search and replace all link descriptions
|
|
|
|
# with alternative location
|
2020-07-26 17:53:59 +00:00
|
|
|
link['href'] = get_site_alt(link['href'])
|
2020-12-17 21:06:47 +00:00
|
|
|
link_desc = link.find_all(
|
|
|
|
text=re.compile('|'.join(SITE_ALTS.keys())))
|
2020-07-26 17:53:59 +00:00
|
|
|
if len(link_desc) == 0:
|
|
|
|
return
|
2020-06-02 18:54:47 +00:00
|
|
|
|
2020-07-26 17:53:59 +00:00
|
|
|
# Replace link destination
|
|
|
|
link_desc[0].replace_with(get_site_alt(link_desc[0]))
|