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

Code before:
import json
import pickle
import urllib.parse as urlparse
import uuid
from functools import wraps

import waitress
from app import app
from app.models.config import Config
from app.request import Request, TorError
from app.utils.bangs import resolve_bang
from app.utils.misc import read_config_bool, get_client_ip
from app.utils.results import add_ip_card
from app.utils.results import bold_search_terms
from app.utils.search import *
from app.utils.session import generate_user_key, valid_user_session
from bs4 import BeautifulSoup as bsoup
from flask import jsonify, make_response, request, redirect, render_template, \
    send_file, session, url_for
from requests import exceptions, get

# Load DDG bang json files only on init
bang_json = json.load(open(app.config['BANG_FILE']))

# Get the newest version of WHOOGLE
link_releases = "https://github.com/benbusby/whoogle-search/releases"
update = bsoup(get(link_releases).text, "html.parser")
newest_version = update.select_one('[class="Link--primary"]').string[1:]


def auth_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization

        # Skip if username/password not set
        whoogle_user = os.getenv('WHOOGLE_USER', '')
        whoogle_pass = os.getenv('WHOOGLE_PASS', '')
        if (not whoogle_user or not whoogle_pass) or (
                auth
                and whoogle_user == auth.username
                and whoogle_pass == auth.password):
            return f(*args, **kwargs)
        else:
            return make_response('Not logged in', 401, {
                'WWW-Authenticate': 'Basic realm="Login Required"'})

    return decorated


@app.before_request

Review comment (suggestion):
App related constants like this should be in `app/__init__.py`, for example: `app.config['RELEASES_URL']` or something along those lines.

Code after:
import json
import pickle
import urllib.parse as urlparse
import uuid
from functools import wraps

import waitress
from app import app
from app.models.config import Config
from app.request import Request, TorError
from app.utils.bangs import resolve_bang
from app.utils.misc import read_config_bool, get_client_ip
from app.utils.results import add_ip_card
from app.utils.results import bold_search_terms
from app.utils.search import *
from app.utils.session import generate_user_key, valid_user_session
from bs4 import BeautifulSoup as bsoup
from flask import jsonify, make_response, request, redirect, render_template, \
    send_file, session, url_for
from requests import exceptions, get

# Load DDG bang json files only on init
bang_json = json.load(open(app.config['BANG_FILE']))


# Check the newest version of WHOOGLE
update = bsoup(get(app.config['RELEASES_URL']).text, 'html.parser')
newest_version = update.select_one('[class="Link--primary"]').string[1:]
current_version = int(''.join(filter(str.isdigit,
                                     app.config['VERSION_NUMBER'])))
newest_version = int(''.join(filter(str.isdigit, newest_version)))
newest_version = '' if current_version >= newest_version \
                    else newest_version


def auth_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization

        # Skip if username/password not set
        whoogle_user = os.getenv('WHOOGLE_USER', '')
        whoogle_pass = os.getenv('WHOOGLE_PASS', '')
        if (not whoogle_user or not whoogle_pass) or (
                auth
                and whoogle_user == auth.username
                and whoogle_pass == auth.password):
            return f(*args, **kwargs)
        else:
            return make_response('Not logged in', 401, {
                'WWW-Authenticate': 'Basic realm="Login Required"'})