Restructured valid params checking, added empty query redirect
parent
c30f21f950
commit
3e404cb524
|
@ -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'
|
DESKTOP_UA = '{}/5.0 (X11; {} x86_64; rv:75.0) Gecko/20100101 {}/75.0'
|
||||||
|
|
||||||
# Valid query params
|
# Valid query params
|
||||||
VALID_PARAMS = {
|
VALID_PARAMS = ['tbs', 'tbm', 'start', 'near']
|
||||||
'tbs': '',
|
|
||||||
'tbm': '',
|
|
||||||
'start': '',
|
|
||||||
'near': ''
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def gen_user_agent(normal_ua):
|
def gen_user_agent(normal_ua):
|
||||||
|
@ -32,28 +27,29 @@ def gen_user_agent(normal_ua):
|
||||||
|
|
||||||
|
|
||||||
def gen_query(query, args, near_city=None):
|
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
|
# Use :past(hour/day/week/month/year) if available
|
||||||
# example search "new restaurants :past month"
|
# example search "new restaurants :past month"
|
||||||
if ':past' in query:
|
if ':past' in query:
|
||||||
time_range = str.strip(query.split(':past', 1)[-1])
|
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
|
# Ensure search query is parsable
|
||||||
query = urlparse.quote(query)
|
query = urlparse.quote(query)
|
||||||
|
|
||||||
# Pass along type of results (news, images, books, etc)
|
# Pass along type of results (news, images, books, etc)
|
||||||
if 'tbm' in args:
|
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)
|
# Get results page start value (10 per page, ie page 2 start val = 20)
|
||||||
if 'start' in args:
|
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
|
# Search for results near a particular city, if available
|
||||||
if near_city is not None:
|
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:
|
if not val or val is None:
|
||||||
continue
|
continue
|
||||||
query += val
|
query += val
|
||||||
|
|
|
@ -42,6 +42,8 @@ def opensearch():
|
||||||
def search():
|
def search():
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
q = request.args.get('q')
|
q = request.args.get('q')
|
||||||
|
if q is None:
|
||||||
|
return redirect('/')
|
||||||
try:
|
try:
|
||||||
q = Fernet(app.secret_key).decrypt(q.encode()).decode()
|
q = Fernet(app.secret_key).decrypt(q.encode()).decode()
|
||||||
except InvalidToken:
|
except InvalidToken:
|
||||||
|
|
Loading…
Reference in New Issue