From 3e404cb52433b949966a0ca3119df0722ae4fdf8 Mon Sep 17 00:00:00 2001 From: Ben Busby <33362396+benbusby@users.noreply.github.com> Date: Wed, 29 Apr 2020 18:53:58 -0600 Subject: [PATCH] Restructured valid params checking, added empty query redirect --- app/request.py | 18 +++++++----------- app/routes.py | 2 ++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/request.py b/app/request.py index afc123f..536b994 100644 --- a/app/request.py +++ b/app/request.py @@ -10,12 +10,7 @@ MOBILE_UA = '{}/5.0 (Android 0; Mobile; rv:54.0) Gecko/54.0 {}/59.0' DESKTOP_UA = '{}/5.0 (X11; {} x86_64; rv:75.0) Gecko/20100101 {}/75.0' # Valid query params -VALID_PARAMS = { - 'tbs': '', - 'tbm': '', - 'start': '', - 'near': '' -} +VALID_PARAMS = ['tbs', 'tbm', 'start', 'near'] def gen_user_agent(normal_ua): @@ -32,28 +27,29 @@ def gen_user_agent(normal_ua): def gen_query(query, args, near_city=None): + param_dict = {key: '' for key in VALID_PARAMS} # Use :past(hour/day/week/month/year) if available # example search "new restaurants :past month" if ':past' in query: time_range = str.strip(query.split(':past', 1)[-1]) - VALID_PARAMS['tbs'] = '&tbs=qdr:' + str.lower(time_range[0]) + param_dict['tbs'] = '&tbs=qdr:' + str.lower(time_range[0]) # Ensure search query is parsable query = urlparse.quote(query) # Pass along type of results (news, images, books, etc) if 'tbm' in args: - VALID_PARAMS['tbm'] = '&tbm=' + args.get('tbm') + param_dict['tbm'] = '&tbm=' + args.get('tbm') # Get results page start value (10 per page, ie page 2 start val = 20) if 'start' in args: - VALID_PARAMS['start'] = '&start=' + args.get('start') + param_dict['start'] = '&start=' + args.get('start') # Search for results near a particular city, if available if near_city is not None: - VALID_PARAMS['near'] = '&near=' + urlparse.quote(near_city) + param_dict['near'] = '&near=' + urlparse.quote(near_city) - for val in VALID_PARAMS.values(): + for val in param_dict.values(): if not val or val is None: continue query += val diff --git a/app/routes.py b/app/routes.py index 43b0901..d14e4e9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -42,6 +42,8 @@ def opensearch(): def search(): if request.method == 'GET': q = request.args.get('q') + if q is None: + return redirect('/') try: q = Fernet(app.secret_key).decrypt(q.encode()).decode() except InvalidToken: