From d33e8241dcd5afd8e7cdfa62163dc112610cf741 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Mon, 14 Feb 2022 11:40:11 -0700 Subject: [PATCH] Fix "my ip" search regression Removes dependency on class names for creating the "my ip" info card in the results list for searches pertaining to the user's public IP. Adds test to prevent this from happening again. Note to anyone reading this and looking to contribute: please avoid using hardcoded class names at all costs. This approach of creating/removing content just results in issues if/when Google decides to introduce/remove class names from the result page. Fixes #657 --- app/utils/results.py | 39 ++++++++++++++++----------------------- test/test_results.py | 10 ++++++++++ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/utils/results.py b/app/utils/results.py index 808a1b5..3f7ceed 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -198,33 +198,26 @@ def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: BeautifulSoup """ - if (not html_soup.select_one(".EY24We") - and html_soup.select_one(".OXXup").get_text().lower() == "all"): - # HTML IP card tag - ip_tag = html_soup.new_tag("div") - ip_tag["class"] = "ZINbbc xpd O9g5cc uUPGi" + # HTML IP card tag + ip_tag = html_soup.new_tag('div') + ip_tag['class'] = 'ZINbbc xpd O9g5cc uUPGi' - # For IP Address html tag - ip_address = html_soup.new_tag("div") - ip_address["class"] = "kCrYT ip-address-div" - ip_address.string = ip + # For IP Address html tag + ip_address = html_soup.new_tag('div') + ip_address['class'] = 'kCrYT ip-address-div' + ip_address.string = ip - # Text below the IP address - ip_text = html_soup.new_tag("div") - ip_text.string = "Your public IP address" - ip_text["class"] = "kCrYT ip-text-div" + # Text below the IP address + ip_text = html_soup.new_tag('div') + ip_text.string = 'Your public IP address' + ip_text['class'] = 'kCrYT ip-text-div' - # Adding all the above html tags to the IP card - ip_tag.append(ip_address) - ip_tag.append(ip_text) + # Adding all the above html tags to the IP card + ip_tag.append(ip_address) + ip_tag.append(ip_text) - # Finding the element before which the IP card would be placed - f_link = html_soup.select_one(".BNeawe.vvjwJb.AP7Wnd") - ref_element = f_link.find_parent(class_="ZINbbc xpd O9g5cc" + - " uUPGi") - - # Inserting the element - ref_element.insert_before(ip_tag) + # Insert the element at the top of the result list + html_soup.select_one('#main').insert_before(ip_tag) return html_soup diff --git a/test/test_results.py b/test/test_results.py index f8036d8..bd3ac00 100644 --- a/test/test_results.py +++ b/test/test_results.py @@ -89,6 +89,16 @@ def test_block_results(client): assert result_site not in 'pinterest.com' +def test_view_my_ip(client): + rv = client.post(f'/{Endpoint.search}', data=dict(q='my ip address')) + assert rv._status_code == 200 + + # Pretty weak test, but better than nothing + str_data = str(rv.data) + assert 'Your public IP address' in str_data + assert '127.0.0.1' in str_data + + def test_recent_results(client): times = { 'past year': 365,