2020-04-29 03:19:34 +03:00
|
|
|
from app.request import VALID_PARAMS
|
2020-04-10 23:52:27 +03:00
|
|
|
from bs4 import BeautifulSoup
|
2020-04-29 03:19:34 +03:00
|
|
|
from cryptography.fernet import Fernet
|
2020-04-10 23:52:27 +03:00
|
|
|
import re
|
|
|
|
import urllib.parse as urlparse
|
|
|
|
from urllib.parse import parse_qs
|
|
|
|
|
2020-04-27 03:48:40 +03:00
|
|
|
SKIP_ARGS = ['ref_src', 'utm']
|
2020-05-04 04:32:47 +03:00
|
|
|
FULL_RES_IMG = '<br/><a href="{}">Full Image</a>'
|
|
|
|
GOOG_IMG = '/images/branding/searchlogo/1x/googlelogo'
|
|
|
|
LOGO_URL = GOOG_IMG + '_desk'
|
|
|
|
BLANK_B64 = '''
|
|
|
|
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAAD0lEQVR42mNkwAIYh7IgAAVVAAuInjI5AAAAAElFTkSuQmCC
|
|
|
|
'''
|
2020-04-27 03:48:40 +03:00
|
|
|
|
2020-05-20 20:02:30 +03:00
|
|
|
|
2020-05-18 19:28:23 +03:00
|
|
|
def get_first_link(soup):
|
|
|
|
# Replace hrefs with only the intended destination (no "utm" type tags)
|
|
|
|
for a in soup.find_all('a', href=True):
|
|
|
|
href = a['href'].replace('https://www.google.com', '')
|
|
|
|
|
|
|
|
result_link = urlparse.urlparse(href)
|
|
|
|
query_link = parse_qs(result_link.query)['q'][0] if '?q=' in href else ''
|
|
|
|
|
|
|
|
# Return the first search result URL
|
|
|
|
if 'url?q=' in href:
|
|
|
|
return filter_link_args(href)
|
|
|
|
|
2020-05-20 20:02:30 +03:00
|
|
|
|
2020-05-18 19:28:23 +03:00
|
|
|
def filter_link_args(query_link):
|
|
|
|
parsed_link = urlparse.urlparse(query_link)
|
|
|
|
link_args = parse_qs(parsed_link.query)
|
|
|
|
safe_args = {}
|
|
|
|
|
|
|
|
if len(link_args) == 0 and len(parsed_link) > 0:
|
|
|
|
return query_link
|
|
|
|
|
|
|
|
for arg in link_args.keys():
|
|
|
|
if arg in SKIP_ARGS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
safe_args[arg] = link_args[arg]
|
|
|
|
|
|
|
|
# Remove original link query and replace with filtered args
|
|
|
|
query_link = query_link.replace(parsed_link.query, '')
|
|
|
|
if len(safe_args) > 0:
|
|
|
|
query_link = query_link + urlparse.urlencode(safe_args, doseq=True)
|
|
|
|
else:
|
|
|
|
query_link = query_link.replace('?', '')
|
|
|
|
|
|
|
|
return query_link
|
2020-04-16 02:41:53 +03:00
|
|
|
|
2020-05-20 20:02:30 +03:00
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
class Filter:
|
2020-04-29 03:19:34 +03:00
|
|
|
def __init__(self, mobile=False, config=None, secret_key=''):
|
2020-04-16 02:41:53 +03:00
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
|
2020-05-13 02:15:53 +03:00
|
|
|
self.near = config['near'] if 'near' in config else ''
|
2020-04-17 03:50:31 +03:00
|
|
|
self.dark = config['dark'] if 'dark' in config else False
|
2020-04-16 19:01:02 +03:00
|
|
|
self.nojs = config['nojs'] if 'nojs' in config else False
|
2020-05-16 01:10:31 +03:00
|
|
|
self.new_tab = config['new_tab'] if 'new_tab' in config else False
|
2020-04-16 19:01:02 +03:00
|
|
|
self.mobile = mobile
|
2020-04-29 03:19:34 +03:00
|
|
|
self.secret_key = secret_key
|
2020-04-16 02:41:53 +03:00
|
|
|
|
2020-04-24 05:59:43 +03:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return getattr(self, name)
|
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
def reskin(self, page):
|
|
|
|
# Aesthetic only re-skinning
|
2020-05-05 03:00:43 +03:00
|
|
|
page = page.replace('>G<', '>Wh<')
|
2020-04-16 02:41:53 +03:00
|
|
|
pattern = re.compile('4285f4|ea4335|fbcc05|34a853|fbbc05', re.IGNORECASE)
|
|
|
|
page = pattern.sub('685e79', page)
|
2020-04-17 03:50:31 +03:00
|
|
|
if self.dark:
|
2020-04-16 02:41:53 +03:00
|
|
|
page = page.replace('fff', '000').replace('202124', 'ddd').replace('1967D2', '3b85ea')
|
|
|
|
|
|
|
|
return page
|
|
|
|
|
|
|
|
def clean(self, soup):
|
2020-04-29 03:19:34 +03:00
|
|
|
self.remove_ads(soup)
|
2020-04-29 03:59:33 +03:00
|
|
|
self.update_image_paths(soup)
|
2020-04-29 03:19:34 +03:00
|
|
|
self.update_styling(soup)
|
|
|
|
self.update_links(soup)
|
|
|
|
|
|
|
|
input_form = soup.find('form')
|
2020-04-29 03:59:33 +03:00
|
|
|
if input_form is not None:
|
|
|
|
input_form['method'] = 'POST'
|
2020-04-29 03:19:34 +03:00
|
|
|
|
2020-04-29 18:46:18 +03:00
|
|
|
# Ensure no extra scripts passed through
|
|
|
|
for script in soup('script'):
|
|
|
|
script.decompose()
|
|
|
|
|
|
|
|
footer = soup.find('div', id='sfooter')
|
|
|
|
if footer is not None:
|
|
|
|
footer.decompose()
|
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
return soup
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
def remove_ads(self, soup):
|
|
|
|
main_divs = soup.find('div', {'id': 'main'})
|
2020-04-29 19:03:34 +03:00
|
|
|
if main_divs is None:
|
|
|
|
return
|
2020-04-29 18:46:18 +03:00
|
|
|
result_divs = main_divs.find_all('div', recursive=False)
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
# Only ads/sponsored content use classes in the list of result divs
|
|
|
|
ad_divs = [ad_div for ad_div in result_divs if 'class' in ad_div.attrs]
|
|
|
|
for div in ad_divs:
|
|
|
|
div.decompose()
|
|
|
|
|
2020-04-29 03:59:33 +03:00
|
|
|
def update_image_paths(self, soup):
|
2020-04-29 20:18:07 +03:00
|
|
|
for img in [_ for _ in soup.find_all('img') if 'src' in _.attrs]:
|
2020-04-29 03:19:34 +03:00
|
|
|
img_src = img['src']
|
|
|
|
if img_src.startswith('//'):
|
|
|
|
img_src = 'https:' + img_src
|
2020-05-20 20:02:30 +03:00
|
|
|
elif img_src.startswith(LOGO_URL):
|
|
|
|
# Re-brand with Whoogle logo
|
|
|
|
img['src'] = '/static/img/logo.png'
|
|
|
|
img['style'] = 'height:40px;width:162px'
|
|
|
|
continue
|
2020-05-04 04:32:47 +03:00
|
|
|
elif img_src.startswith(GOOG_IMG):
|
2020-05-20 20:02:30 +03:00
|
|
|
img['src'] = BLANK_B64
|
2020-05-04 04:32:47 +03:00
|
|
|
continue
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
enc_src = Fernet(self.secret_key).encrypt(img_src.encode())
|
|
|
|
img['src'] = '/tmp?image_url=' + enc_src.decode()
|
2020-05-04 04:32:47 +03:00
|
|
|
# TODO: Non-mobile image results link to website instead of image
|
|
|
|
# if not self.mobile:
|
|
|
|
# img.append(BeautifulSoup(FULL_RES_IMG.format(img_src), 'html.parser'))
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
def update_styling(self, soup):
|
|
|
|
# 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:
|
|
|
|
logo['style'] = 'display:flex; justify-content:center; align-items:center; color:#685e79; ' \
|
|
|
|
'font-size:18px; '
|
|
|
|
|
|
|
|
# Fix search bar length on mobile
|
|
|
|
try:
|
|
|
|
search_bar = soup.find('header').find('form').find('div')
|
|
|
|
search_bar['style'] = 'width: 100%;'
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Set up dark mode if active
|
|
|
|
if self.dark:
|
2020-05-15 23:17:16 +03:00
|
|
|
soup.find('html')['style'] = 'scrollbar-color: #333 #111;color:#fff !important;background:#000 !important'
|
2020-04-29 03:19:34 +03:00
|
|
|
for input_element in soup.findAll('input'):
|
2020-05-15 23:17:16 +03:00
|
|
|
input_element['style'] = 'color:#fff;background:#000;'
|
|
|
|
|
|
|
|
for span_element in soup.findAll('span'):
|
|
|
|
span_element['style'] = 'color: white;'
|
|
|
|
|
|
|
|
for href_element in soup.findAll('a'):
|
|
|
|
href_element['style'] = 'color: white' if href_element['href'].startswith('/search') else ''
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
def update_links(self, soup):
|
|
|
|
# Replace hrefs with only the intended destination (no "utm" type tags)
|
|
|
|
for a in soup.find_all('a', href=True):
|
2020-04-29 20:18:07 +03:00
|
|
|
href = a['href'].replace('https://www.google.com', '')
|
2020-04-29 03:19:34 +03:00
|
|
|
if '/advanced_search' in href:
|
|
|
|
a.decompose()
|
|
|
|
continue
|
2020-05-16 01:10:31 +03:00
|
|
|
elif self.new_tab:
|
|
|
|
a['target'] = '_blank'
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
result_link = urlparse.urlparse(href)
|
2020-04-29 18:46:18 +03:00
|
|
|
query_link = parse_qs(result_link.query)['q'][0] if '?q=' in href else ''
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
if '/search?q=' in href:
|
|
|
|
enc_result = Fernet(self.secret_key).encrypt(query_link.encode())
|
|
|
|
new_search = '/search?q=' + enc_result.decode()
|
|
|
|
|
2020-04-29 18:46:18 +03:00
|
|
|
query_params = parse_qs(urlparse.urlparse(href).query)
|
2020-04-29 19:03:34 +03:00
|
|
|
for param in VALID_PARAMS:
|
|
|
|
param_val = query_params[param][0] if param in query_params else ''
|
2020-04-29 18:46:18 +03:00
|
|
|
new_search += '&' + param + '=' + param_val
|
2020-04-29 03:19:34 +03:00
|
|
|
a['href'] = new_search
|
2020-04-29 23:46:00 +03:00
|
|
|
elif 'url?q=' in href:
|
2020-04-29 03:19:34 +03:00
|
|
|
# Strip unneeded arguments
|
2020-05-20 20:02:30 +03:00
|
|
|
a['href'] = filter_link_args(query_link)
|
2020-04-29 03:19:34 +03:00
|
|
|
|
|
|
|
# Add no-js option
|
|
|
|
if self.nojs:
|
2020-05-20 20:02:30 +03:00
|
|
|
gen_nojs(soup, a['href'], a)
|
2020-04-29 23:46:00 +03:00
|
|
|
else:
|
|
|
|
a['href'] = href
|
2020-04-29 20:18:07 +03:00
|
|
|
|
2020-04-29 03:19:34 +03:00
|
|
|
|
2020-04-29 18:46:18 +03:00
|
|
|
def gen_nojs(soup, link, sibling):
|
|
|
|
nojs_link = soup.new_tag('a')
|
|
|
|
nojs_link['href'] = '/window?location=' + link
|
|
|
|
nojs_link['style'] = 'display:block;width:100%;'
|
|
|
|
nojs_link.string = 'NoJS Link: ' + nojs_link['href']
|
|
|
|
sibling.append(BeautifulSoup('<br><hr><br>', 'html.parser'))
|
2020-05-18 19:28:23 +03:00
|
|
|
sibling.append(nojs_link)
|