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

Code before:
    if opensearch_url.endswith('/'):
        opensearch_url = opensearch_url[:-1]

    template = render_template('opensearch.xml',
                               main_url=opensearch_url,
                               request_type='get' if g.user_config.get_only else 'post')
    response = make_response(template)
    response.headers['Content-Type'] = 'application/xml'
    return response


@app.route('/search', methods=['GET', 'POST'])
def search():
    request_params = request.args if request.method == 'GET' else request.form
    q = request_params.get('q')
    
    if q is None or len(q) == 0:
        return redirect('/')
    else:
        # Attempt to decrypt if this is an internal link
        try:
            q = Fernet(app.secret_key).decrypt(q.encode()).decode()
        except InvalidToken:
            pass

    feeling_lucky = q.startswith("! ")

    if feeling_lucky:
        q = q[2:]

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
    full_query = gen_query(q, request_params, content_filter.near, language=g.user_config.lang)
    get_body = g.user_request.send(query=full_query)

    results = content_filter.reskin(get_body)
    dirty_soup = BeautifulSoup(results, 'html.parser')

    if feeling_lucky:
        redirect_url = content_filter.get_first_url(dirty_soup)
        return redirect(redirect_url, 303) # Using 303 so the browser performs a GET request for the URL
    else:
        formatted_results = content_filter.clean(dirty_soup)



    return render_template('display.html', query=urlparse.unquote(q), response=formatted_results)



Review comment (suggestion):
```suggestion
    feeling_lucky = q.startswith('! ')
```

Needs single quotes to match the rest of the python formatting

Code after:
    if opensearch_url.endswith('/'):
        opensearch_url = opensearch_url[:-1]

    template = render_template('opensearch.xml',
                               main_url=opensearch_url,
                               request_type='get' if g.user_config.get_only else 'post')
    response = make_response(template)
    response.headers['Content-Type'] = 'application/xml'
    return response


@app.route('/search', methods=['GET', 'POST'])
def search():
    request_params = request.args if request.method == 'GET' else request.form
    q = request_params.get('q')
    
    if q is None or len(q) == 0:
        return redirect('/')
    else:
        # Attempt to decrypt if this is an internal link
        try:
            q = Fernet(app.secret_key).decrypt(q.encode()).decode()
        except InvalidToken:
            pass

    feeling_lucky = q.startswith('! ')

    if feeling_lucky: # Well do you, punk?
        q = q[2:]

    user_agent = request.headers.get('User-Agent')
    mobile = 'Android' in user_agent or 'iPhone' in user_agent

    content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key)
    full_query = gen_query(q, request_params, content_filter.near, language=g.user_config.lang)
    get_body = g.user_request.send(query=full_query)

    results = content_filter.reskin(get_body)
    dirty_soup = BeautifulSoup(results, 'html.parser')

    if feeling_lucky:
        redirect_url = get_first_link(dirty_soup)
        return redirect(redirect_url, 303) # Using 303 so the browser performs a GET request for the URL
    else:
        formatted_results = content_filter.clean(dirty_soup)



    return render_template('display.html', query=urlparse.unquote(q), response=formatted_results)