Repository: benbusby/whoogle-search
File: app/utils/results.py (Python)

Code before:

    def replace_any_case(element: NavigableString, target_word: str) -> None:
        # Replace all instances of the word, but maintaining the same case in
        # the replacement
        element.replace_with(
            element.replace(
                target_word.lower(), f'<b>{target_word.lower()}</b>'
            ).replace(
                target_word.capitalize(), f'<b>{target_word.capitalize()}</b>'
            ).replace(
                target_word.title(), f'<b>{target_word.title()}</b>'
            ).replace(
                target_word.upper(), f'<b>{target_word.upper()}</b>'
            )
        )

    # Split all words out of query, grouping the ones wrapped in quotes
    for word in re.split(r'\s+(?=[^"]*(?:"[^"]*"[^"]*)*$)', query):
        word = re.sub(r'[^A-Za-z0-9 ]+', '', word)
        target = response.find_all(
            text=re.compile(r'' + re.escape(word), re.I))
        for nav_str in target:
            replace_any_case(nav_str, word)

    return response
  
def has_ad_content(element: str) -> bool:
    """Inspects an HTML element for ad related content

    Args:
        element: The HTML element to inspect

    Returns:
        bool: True/False for the element containing an ad

    """
    return (element.upper() in (value.upper() for value in BLACKLIST)
            or 'ⓘ' in element)


def get_first_link(soup: BeautifulSoup) -> str:
    """Retrieves the first result link from the query response

    Args:
        soup: The BeautifulSoup response body

    Returns:
        str: A str link to the first result

    """
    # Replace hrefs with only the intended destination (no "utm" type tags)

Review comment (suggestion):
Formatting (trailing whitespace and missing 2nd line break before `has_ad_content`)

```suggestion


```

Code after:
    response = BeautifulSoup(response, 'html.parser')

    def replace_any_case(element: NavigableString, target_word: str) -> None:
        # Replace all instances of the word, but maintaining the same case in
        # the replacement
        element.replace_with(
            element.replace(
                target_word.lower(), f'<b>{target_word.lower()}</b>'
            ).replace(
                target_word.capitalize(), f'<b>{target_word.capitalize()}</b>'
            ).replace(
                target_word.title(), f'<b>{target_word.title()}</b>'
            ).replace(
                target_word.upper(), f'<b>{target_word.upper()}</b>'
            )
        )

    # Split all words out of query, grouping the ones wrapped in quotes
    for word in re.split(r'\s+(?=[^"]*(?:"[^"]*"[^"]*)*$)', query):
        word = re.sub(r'[^A-Za-z0-9 ]+', '', word)
        target = response.find_all(
            text=re.compile(r'' + re.escape(word), re.I))
        for nav_str in target:
            replace_any_case(nav_str, word)

    return response


def has_ad_content(element: str) -> bool:
    """Inspects an HTML element for ad related content

    Args:
        element: The HTML element to inspect

    Returns:
        bool: True/False for the element containing an ad

    """
    return (element.upper() in (value.upper() for value in BLACKLIST)
            or 'ⓘ' in element)


def get_first_link(soup: BeautifulSoup) -> str:
    """Retrieves the first result link from the query response

    Args:
        soup: The BeautifulSoup response body

    Returns:
        str: A str link to the first result