diff --git a/app/utils/results.py b/app/utils/results.py index c78f866..2f75646 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -147,8 +147,8 @@ def get_first_link(soup: BeautifulSoup) -> str: # Replace hrefs with only the intended destination (no "utm" type tags) for a in soup.find_all('a', href=True): # Return the first search result URL - if 'url?q=' in a['href']: - return filter_link_args(a['href']) + if a['href'].startswith('http://') or a['href'].startswith('https://'): + return a['href'] return '' diff --git a/app/utils/search.py b/app/utils/search.py index 6e2d62d..59f630d 100644 --- a/app/utils/search.py +++ b/app/utils/search.py @@ -161,22 +161,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)