From 4bf013ccd08c3f6092c2cf8822b6e312c0615443 Mon Sep 17 00:00:00 2001 From: Ben Busby <33362396+benbusby@users.noreply.github.com> Date: Sat, 23 May 2020 14:23:20 -0600 Subject: [PATCH] Improved handling of default country Also added fallback 1px empty gif as a replacement for images that fail to load --- app/models/config.py | 1 + app/request.py | 2 +- app/routes.py | 29 +++++++++++++++++++---------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/models/config.py b/app/models/config.py index 2da1695..0976400 100644 --- a/app/models/config.py +++ b/app/models/config.py @@ -51,6 +51,7 @@ class Config: ] COUNTRIES = [ + {'name': 'Default (use server location)', 'value': ''}, {'name': 'Afghanistan', 'value': 'countryAF'}, {'name': 'Albania', 'value': 'countryAL'}, {'name': 'Algeria', 'value': 'countryDZ'}, diff --git a/app/request.py b/app/request.py index e1daf59..07b6351 100644 --- a/app/request.py +++ b/app/request.py @@ -46,7 +46,7 @@ def gen_query(query, args, config, near_city=None): param_dict['start'] = '&start=' + args.get('start') # Search for results near a particular city, if available - if near_city is not None: + if near_city: param_dict['near'] = '&near=' + urlparse.quote(near_city) # Set language for results (lr) and interface (hl) diff --git a/app/routes.py b/app/routes.py index 4c465fc..3f0d04c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,6 +3,7 @@ from app.filter import Filter, get_first_link from app.models.config import Config from app.request import Request, gen_query import argparse +import base64 from bs4 import BeautifulSoup from cryptography.fernet import Fernet, InvalidToken from flask import g, make_response, request, redirect, render_template, send_file @@ -10,6 +11,7 @@ from functools import wraps import io import json import os +from pycurl import error as pycurl_error import urllib.parse as urlparse import waitress @@ -163,17 +165,24 @@ def imgres(): def tmp(): cipher_suite = Fernet(app.secret_key) img_url = cipher_suite.decrypt(request.args.get('image_url').encode()).decode() - file_data = g.user_request.send(base_url=img_url, return_bytes=True) - tmp_mem = io.BytesIO() - tmp_mem.write(file_data) - tmp_mem.seek(0) - return send_file( - tmp_mem, - as_attachment=True, - attachment_filename='tmp.png', - mimetype='image/png' - ) + try: + file_data = g.user_request.send(base_url=img_url, return_bytes=True) + tmp_mem = io.BytesIO() + tmp_mem.write(file_data) + tmp_mem.seek(0) + + return send_file( + tmp_mem, + as_attachment=True, + attachment_filename='tmp.png', + mimetype='image/png' + ) + except pycurl_error: + pass + + empty_gif = base64.b64decode('R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==') + return send_file(io.BytesIO(empty_gif), mimetype='image/gif') @app.route('/window')