|
|
|
@ -102,9 +102,15 @@ class Search:
|
|
|
|
|
except InvalidToken:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# Strip leading '! ' for "feeling lucky" queries
|
|
|
|
|
self.feeling_lucky = q.startswith('! ')
|
|
|
|
|
self.query = q[2:] if self.feeling_lucky else q
|
|
|
|
|
# Strip '!' for "feeling lucky" queries
|
|
|
|
|
if match := re.search("(^|\s)!($|\s)", q):
|
|
|
|
|
self.feeling_lucky = True
|
|
|
|
|
start, end = match.span()
|
|
|
|
|
self.query = " ".join([seg for seg in [q[:start], q[end:]] if seg])
|
|
|
|
|
else:
|
|
|
|
|
self.feeling_lucky = False
|
|
|
|
|
self.query = q
|
|
|
|
|
|
|
|
|
|
# Check for possible widgets
|
|
|
|
|
self.widget = "ip" if re.search("([^a-z0-9]|^)my *[^a-z0-9] *(ip|internet protocol)" +
|
|
|
|
|
"($|( *[^a-z0-9] *(((addres|address|adres|" +
|
|
|
|
@ -161,22 +167,25 @@ class Search:
|
|
|
|
|
if g.user_request.tor_valid:
|
|
|
|
|
html_soup.insert(0, bsoup(TOR_BANNER, 'html.parser'))
|
|
|
|
|
|
|
|
|
|
formatted_results = content_filter.clean(html_soup)
|
|
|
|
|
if self.feeling_lucky:
|
|
|
|
|
return get_first_link(html_soup)
|
|
|
|
|
else:
|
|
|
|
|
formatted_results = content_filter.clean(html_soup)
|
|
|
|
|
|
|
|
|
|
# Append user config to all search links, if available
|
|
|
|
|
param_str = ''.join('&{}={}'.format(k, v)
|
|
|
|
|
for k, v in
|
|
|
|
|
self.request_params.to_dict(flat=True).items()
|
|
|
|
|
if self.config.is_safe_key(k))
|
|
|
|
|
for link in formatted_results.find_all('a', href=True):
|
|
|
|
|
link['rel'] = "nofollow noopener noreferrer"
|
|
|
|
|
if 'search?' not in link['href'] or link['href'].index(
|
|
|
|
|
'search?') > 1:
|
|
|
|
|
continue
|
|
|
|
|
link['href'] += param_str
|
|
|
|
|
|
|
|
|
|
return str(formatted_results)
|
|
|
|
|
if lucky_link := get_first_link(formatted_results):
|
|
|
|
|
return lucky_link
|
|
|
|
|
|
|
|
|
|
# Fall through to regular search if unable to find link
|
|
|
|
|
self.feeling_lucky = False
|
|
|
|
|
|
|
|
|
|
# Append user config to all search links, if available
|
|
|
|
|
param_str = ''.join('&{}={}'.format(k, v)
|
|
|
|
|
for k, v in
|
|
|
|
|
self.request_params.to_dict(flat=True).items()
|
|
|
|
|
if self.config.is_safe_key(k))
|
|
|
|
|
for link in formatted_results.find_all('a', href=True):
|
|
|
|
|
link['rel'] = "nofollow noopener noreferrer"
|
|
|
|
|
if 'search?' not in link['href'] or link['href'].index(
|
|
|
|
|
'search?') > 1:
|
|
|
|
|
continue
|
|
|
|
|
link['href'] += param_str
|
|
|
|
|
|
|
|
|
|
return str(formatted_results)
|
|
|
|
|
|
|
|
|
|