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

Code before:
        ref_element = f_link.find_parent(class_="ZINbbc xpd O9g5cc" +
                                                " uUPGi")

        # Inserting the element
        ref_element.insert_before(ip_tag)
    return html_soup


def check_currency(response: str):
    """Check whether the results have currency conversion

    Args:
        response: Search query Result

    Returns:
        list

    """
    soup = BeautifulSoup(response, 'html.parser')
    a = soup.find(class_='ZINbbc xpd O9g5cc uUPGi')
    tag = a.find('a')
    if tag and tag['href'] == 'https://g.co/gfd':
        currency1 = soup.find(class_="xUrNXd UMOHqf")
        currency2 = soup.select_one('.kCrYT'
                                    ' .BNeawe.iBp4i.AP7Wnd '
                                    '.BNeawe.iBp4i.AP7Wnd')
        currency1 = currency1.text.rstrip('=').split(' ', 1)
        currency2 = currency2.text.split(' ', 1)
        return [float(currency1[0]), currency1[1],
                float(currency2[0]), currency2[1]]
    return None


def add_currency_b(soup: BeautifulSoup,
                   conversion_details: list) -> BeautifulSoup:
    """Adds the currency conversion boxes
    to response of the search query

    Args:
        soup: Parsed search result
        conversion_details: list of currency
        related information

    Returns:
        BeautifulSoup
    """
    # Element before which the code will be changed
    # (This is the 'disclaimer' link)
    element1 = soup.select_one('[class="nXE3Ob"]')

    # Creating the conversion factor

Review comment (suggestion):
Relying on this many obfuscated classes from Google makes me a bit nervous, since they could change any of these and kill this feature as a result...but I'm not sure of a better alternative. 

Could you maybe try finding the `a` tag with the link related to currency, using something like:

```py
currency_link = soup.find('a', {'href': 'https://g.co/gfd'})
```

and then maybe if that's found, traverse up the `div` tree using `<element>.parent` (swapping `<element>` for the previous parent result) until one has the `ZINbbc` class? `ZINbbc` seems to be the main class that Google uses for result divs, so if/when they change that one, it would be an easy thing to replace with whatever they change it to. 

Code after:
        ref_element = f_link.find_parent(class_="ZINbbc xpd O9g5cc" +
                                                " uUPGi")

        # Inserting the element
        ref_element.insert_before(ip_tag)
    return html_soup


def check_currency(response: str) -> dict:
    """Check whether the results have currency conversion

    Args:
        response: Search query Result

    Returns:
        dict: Consists of currency names and values

    """
    soup = BeautifulSoup(response, 'html.parser')
    currency_link = soup.find('a', {'href': 'https://g.co/gfd'})
    if currency_link:
        while 'class' not in currency_link.attrs or \
                'ZINbbc' not in currency_link.attrs['class']:
            currency_link = currency_link.parent
        currency_link = currency_link.find_all(class_='BNeawe')
        currency1 = currency_link[0].text
        currency2 = currency_link[1].text
        currency1 = currency1.rstrip('=').split(' ', 1)
        currency2 = currency2.split(' ', 1)
        if currency2[0][-3] == ',':
            currency1[0] = currency1[0].replace('.', '')
            currency1[0] = currency1[0].replace(',', '.')
            currency2[0] = currency2[0].replace('.', '')
            currency2[0] = currency2[0].replace(',', '.')
        else:
            currency1[0] = currency1[0].replace(',', '')
            currency2[0] = currency2[0].replace(',', '')
        return {'currencyValue1': float(currency1[0]),
                'currencyLabel1': currency1[1],
                'currencyValue2': float(currency2[0]),
                'currencyLabel2': currency2[1]
                }
    return {}


def add_currency_card(soup: BeautifulSoup,
                      conversion_details: dict) -> BeautifulSoup:
    """Adds the currency conversion boxes
    to response of the search query

    Args: