Added version footer, minor PEP 8 refactoring
parent
38b7b19e2a
commit
c51f186419
|
@ -4,5 +4,9 @@ import os
|
|||
|
||||
app = Flask(__name__, static_folder=os.path.dirname(os.path.abspath(__file__)) + '/static')
|
||||
app.secret_key = Fernet.generate_key()
|
||||
app.config['VERSION_NUMBER'] = '0.1.1'
|
||||
app.config['APP_ROOT'] = os.getenv('APP_ROOT', os.path.dirname(os.path.abspath(__file__)))
|
||||
app.config['STATIC_FOLDER'] = os.getenv('STATIC_FOLDER', os.path.join(app.config['APP_ROOT'], 'static'))
|
||||
app.config['CONFIG_PATH'] = os.getenv('CONFIG_VOLUME', app.config['STATIC_FOLDER']) + '/config.json'
|
||||
|
||||
from app import routes
|
||||
|
|
|
@ -13,6 +13,7 @@ BLANK_B64 = '''
|
|||
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAAD0lEQVR42mNkwAIYh7IgAAVVAAuInjI5AAAAAElFTkSuQmCC
|
||||
'''
|
||||
|
||||
|
||||
def get_first_link(soup):
|
||||
# Replace hrefs with only the intended destination (no "utm" type tags)
|
||||
for a in soup.find_all('a', href=True):
|
||||
|
@ -25,6 +26,7 @@ def get_first_link(soup):
|
|||
if 'url?q=' in href:
|
||||
return filter_link_args(href)
|
||||
|
||||
|
||||
def filter_link_args(query_link):
|
||||
parsed_link = urlparse.urlparse(query_link)
|
||||
link_args = parse_qs(parsed_link.query)
|
||||
|
@ -48,6 +50,7 @@ def filter_link_args(query_link):
|
|||
|
||||
return query_link
|
||||
|
||||
|
||||
class Filter:
|
||||
def __init__(self, mobile=False, config=None, secret_key=''):
|
||||
if config is None:
|
||||
|
@ -109,14 +112,13 @@ class Filter:
|
|||
img_src = img['src']
|
||||
if img_src.startswith('//'):
|
||||
img_src = 'https:' + img_src
|
||||
elif img_src.startswith(LOGO_URL):
|
||||
# Re-brand with Whoogle logo
|
||||
img['src'] = '/static/img/logo.png'
|
||||
img['style'] = 'height:40px;width:162px'
|
||||
continue
|
||||
elif img_src.startswith(GOOG_IMG):
|
||||
# Special rebranding for image search results
|
||||
if img_src.startswith(LOGO_URL):
|
||||
img['src'] = '/static/img/logo.png'
|
||||
img['style'] = 'height:40px;width:162px'
|
||||
else:
|
||||
img['src'] = BLANK_B64
|
||||
|
||||
img['src'] = BLANK_B64
|
||||
continue
|
||||
|
||||
enc_src = Fernet(self.secret_key).encrypt(img_src.encode())
|
||||
|
@ -183,12 +185,11 @@ class Filter:
|
|||
a['href'] = new_search
|
||||
elif 'url?q=' in href:
|
||||
# Strip unneeded arguments
|
||||
query_link = filter_link_args(query_link)
|
||||
a['href'] = query_link
|
||||
a['href'] = filter_link_args(query_link)
|
||||
|
||||
# Add no-js option
|
||||
if self.nojs:
|
||||
gen_nojs(soup, query_link, a)
|
||||
gen_nojs(soup, a['href'], a)
|
||||
else:
|
||||
a['href'] = href
|
||||
|
||||
|
|
|
@ -13,11 +13,6 @@ import os
|
|||
import urllib.parse as urlparse
|
||||
import waitress
|
||||
|
||||
app.config['APP_ROOT'] = os.getenv('APP_ROOT', os.path.dirname(os.path.abspath(__file__)))
|
||||
app.config['STATIC_FOLDER'] = os.getenv('STATIC_FOLDER', os.path.join(app.config['APP_ROOT'], 'static'))
|
||||
|
||||
CONFIG_PATH = os.getenv('CONFIG_VOLUME', app.config['STATIC_FOLDER']) + '/config.json'
|
||||
|
||||
|
||||
def auth_required(f):
|
||||
@wraps(f)
|
||||
|
@ -27,7 +22,8 @@ def auth_required(f):
|
|||
# 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):
|
||||
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"'})
|
||||
|
@ -38,13 +34,14 @@ def auth_required(f):
|
|||
def before_request_func():
|
||||
# Always redirect to https if HTTPS_ONLY is set (otherwise default to false)
|
||||
https_only = os.getenv('HTTPS_ONLY', False)
|
||||
config_path = app.config['CONFIG_PATH']
|
||||
|
||||
if https_only and request.url.startswith('http://'):
|
||||
url = request.url.replace('http://', 'https://', 1)
|
||||
https_url = request.url.replace('http://', 'https://', 1)
|
||||
code = 308
|
||||
return redirect(url, code=code)
|
||||
return redirect(https_url, code=code)
|
||||
|
||||
json_config = json.load(open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else {'url': request.url_root}
|
||||
json_config = json.load(open(config_path)) if os.path.exists(config_path) else {'url': request.url_root}
|
||||
g.user_config = Config(**json_config)
|
||||
|
||||
if not g.user_config.url:
|
||||
|
@ -68,6 +65,7 @@ def index():
|
|||
ua=g.user_request.modified_user_agent,
|
||||
languages=Config.LANGUAGES,
|
||||
current_lang=g.user_config.lang,
|
||||
version_number=app.config['VERSION_NUMBER'],
|
||||
request_type='get' if g.user_config.get_only else 'post')
|
||||
|
||||
|
||||
|
@ -91,7 +89,7 @@ def opensearch():
|
|||
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:
|
||||
|
@ -103,7 +101,7 @@ def search():
|
|||
|
||||
feeling_lucky = q.startswith('! ')
|
||||
|
||||
if feeling_lucky: # Well do you, punk?
|
||||
if feeling_lucky: # Well do you, punk?
|
||||
q = q[2:]
|
||||
|
||||
user_agent = request.headers.get('User-Agent')
|
||||
|
@ -112,18 +110,13 @@ def search():
|
|||
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')
|
||||
dirty_soup = BeautifulSoup(content_filter.reskin(get_body), '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
|
||||
return redirect(get_first_link(dirty_soup), 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)
|
||||
|
||||
|
||||
|
@ -137,7 +130,7 @@ def config():
|
|||
if 'url' not in config_data or not config_data['url']:
|
||||
config_data['url'] = g.user_config.url
|
||||
|
||||
with open(CONFIG_PATH, 'w') as config_file:
|
||||
with open(app.config['CONFIG_PATH'], 'w') as config_file:
|
||||
config_file.write(json.dumps(config_data, indent=4))
|
||||
config_file.close()
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
body {
|
||||
font-family: Avenir, Helvetica, Arial, sans-serif;
|
||||
}
|
||||
|
||||
.logo {
|
||||
width: 80%;
|
||||
display: block;
|
||||
|
@ -117,3 +121,11 @@ button::-moz-focus-inner {
|
|||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
footer {
|
||||
position: fixed;
|
||||
bottom: 0%;
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
z-index: -1;
|
||||
}
|
||||
|
|
|
@ -85,6 +85,8 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<footer>
|
||||
<p>Whoogle Search v{{ version_number }} || <a href="https://github.com/benbusby/whoogle-search">View on GitHub</a></p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
Loading…
Reference in New Issue