diff --git a/app/filter.py b/app/filter.py index 5f3f1e3..f10dcf3 100644 --- a/app/filter.py +++ b/app/filter.py @@ -34,12 +34,13 @@ class Filter: def clean(self, soup): self.remove_ads(soup) - self.sync_images(soup) + self.update_image_paths(soup) self.update_styling(soup) self.update_links(soup) input_form = soup.find('form') - input_form['method'] = 'POST' + if input_form is not None: + input_form['method'] = 'POST' return soup @@ -55,7 +56,7 @@ class Filter: for div in ad_divs: div.decompose() - def sync_images(self, soup): + def update_image_paths(self, soup): for img in [_ for _ in soup.find_all('img') if 'src' in _]: img_src = img['src'] if img_src.startswith('//'): diff --git a/app/request.py b/app/request.py index c02cb00..afc123f 100644 --- a/app/request.py +++ b/app/request.py @@ -10,7 +10,12 @@ 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): @@ -26,33 +31,34 @@ def gen_user_agent(normal_ua): return DESKTOP_UA.format(mozilla, linux, firefox) -def gen_query(q, args, near_city=None): +def gen_query(query, args, near_city=None): # Use :past(hour/day/week/month/year) if available # example search "new restaurants :past month" - tbs = '' - if ':past' in q: - time_range = str.strip(q.split(':past', 1)[-1]) - tbs = '&tbs=qdr:' + str.lower(time_range[0]) + if ':past' in query: + time_range = str.strip(query.split(':past', 1)[-1]) + VALID_PARAMS['tbs'] = '&tbs=qdr:' + str.lower(time_range[0]) # Ensure search query is parsable - q = urlparse.quote(q) + query = urlparse.quote(query) # Pass along type of results (news, images, books, etc) - tbm = '' if 'tbm' in args: - tbm = '&tbm=' + args.get('tbm') + VALID_PARAMS['tbm'] = '&tbm=' + args.get('tbm') # Get results page start value (10 per page, ie page 2 start val = 20) - start = '' if 'start' in args: - start = '&start=' + args.get('start') + VALID_PARAMS['start'] = '&start=' + args.get('start') # Search for results near a particular city, if available - near = '' if near_city is not None: - near = '&near=' + urlparse.quote(near_city) + VALID_PARAMS['near'] = '&near=' + urlparse.quote(near_city) - return q + tbs + tbm + start + near + for val in VALID_PARAMS.values(): + if not val or val is None: + continue + query += val + + return query class Request: diff --git a/app/routes.py b/app/routes.py index cfafd6d..f5e141b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -40,7 +40,6 @@ def opensearch(): @app.route('/search', methods=['GET', 'POST']) def search(): - q = None if request.method == 'GET': q = request.args.get('q') try: @@ -50,7 +49,7 @@ def search(): else: q = request.form['q'] - if q is None or len(q) <= 0: + if q is None or len(q) == 0: return render_template('error.html') user_agent = request.headers.get('User-Agent') diff --git a/app/templates/display.html b/app/templates/display.html index f0f7499..72b1d49 100644 --- a/app/templates/display.html +++ b/app/templates/display.html @@ -2,7 +2,7 @@
- +