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

Code before:
    resp_code = 503 if has_captcha(str(response)) else 200

    # Feature to display IP address
    html_soup = bsoup(response, 'html.parser')
    if (not html_soup.select_one(".EY24We")
            and html_soup.select_one(".OXXup").get_text().lower() == "all"):
        if re.search("([^a-z0-9]|^)my *[^a-z0-9] *(ip|internet protocol)" +
                     "($|( *[^a-z0-9] *(((addres|address|adres|adress)|a)?" +
                     " *$)))", query.lower()):
            # HTML IP card tag
            ip_tag = html_soup.new_tag("div")
            ip_tag["class"] = "ZINbbc xpd O9g5cc uUPGi"

            # What is my IP tag
            q_tag = html_soup.new_tag("div")
            q_tag["class"] = "kCrYT"
            q_tag.string = "What's my IP"

            # Distinction boundary
            line = html_soup.new_tag("div")
            line["class"] = "x54gtf"

            # For IP Address html tag
            ip_address = html_soup.new_tag("div")
            ip_address["class"] = "kCrYT"
            ip_address["style"] = "padding-bottom:0;"
            ip_address.string = request.remote_addr

            # 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["style"] = "padding-top: 0; color: #9e9e9e!important;"

            # Adding all the above html tags to the IP card
            ip_tag.append(q_tag)
            ip_tag.append(line)
            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)

            response = html_soup

    return render_template(

Review comment (suggestion):
For styling of these elements, I'd prefer to define styles in a general CSS file that gets loaded on the results page (like `app/static/css/search.css` for instance). In this case you can keep the default Google defined "kCrYT" class, but maybe add your own human-readable class name like "ip-div" or something for the styles you're using here.

Code after:
    resp_code = 503 if has_captcha(str(response)) else 200

    # Feature to display IP address
    if search_util.check_kw_ip():
        html_soup = bsoup(response, "html.parser")
        response = add_ip_card(html_soup, request.remote_addr)

    return render_template(
        'display.html',
        query=urlparse.unquote(query),
        search_type=search_util.search_type,
        config=g.user_config,
        lingva_url=app.config['TRANSLATE_URL'],
        translation=translation,
        translate_to=translate_to,
        translate_str=query.replace(
            'translate', ''
        ).replace(
            translation['translate'], ''
        ),
        is_translation=any(
            _ in query.lower() for _ in [translation['translate'], 'translate']
        ) and not search_util.search_type,  # Standard search queries only
        response=response,
        version_number=app.config['VERSION_NUMBER'],
        search_header=(render_template(
            'header.html',
            config=g.user_config,
            logo=render_template('logo.html', dark=g.user_config.dark),
            query=urlparse.unquote(query),
            search_type=search_util.search_type,
            mobile=g.user_request.mobile)
                       if 'isch' not in
                          search_util.search_type else '')), resp_code


@app.route('/config', methods=['GET', 'POST', 'PUT'])
@auth_required
def config():
    config_disabled = app.config['CONFIG_DISABLE']
    if request.method == 'GET':
        return json.dumps(g.user_config.__dict__)
    elif request.method == 'PUT' and not config_disabled:
        if 'name' in request.args:
            config_pkl = os.path.join(
                app.config['CONFIG_PATH'],
                request.args.get('name'))
            session['config'] = (pickle.load(open(config_pkl, 'rb'))
                                 if os.path.exists(config_pkl)
                                 else session['config'])
            return json.dumps(session['config'])