diff --git a/.gitignore b/.gitignore index 86c05721..d5843b61 100644 --- a/.gitignore +++ b/.gitignore @@ -117,6 +117,9 @@ ENV/ env.bak/ venv.bak/ +# VS Code config +.vscode/ + # Spyder project settings .spyderproject .spyproject diff --git a/lute/bing/routes.py b/lute/bing/routes.py index 92eac938..fa60d267 100644 --- a/lute/bing/routes.py +++ b/lute/bing/routes.py @@ -7,12 +7,43 @@ import hashlib import re import urllib.request -from flask import Blueprint, request, Response, render_template, jsonify, current_app +from flask import ( + Blueprint, + request, + Response, + render_template, + jsonify, + current_app, + url_for, +) bp = Blueprint("bing", __name__, url_prefix="/bing") +@bp.route( + "/search_page///", methods=["GET"] +) +def bing_search_page(langid, text, searchstring): + """ + Load initial empty search page, passing real URL for subsequent ajax call to get images. + + Sometimes Bing image searches block or fail, so providing the initial empty search page + lets the user know work is in progress. The user can therefore interact with the page + immediately. The template for this route then makes an ajax call to the "bing_search()" + method below which actually does the search. + """ + + # Create URL for bing_search and pass into template. + search_url = url_for( + "bing.bing_search", langid=langid, text=text, searchstring=searchstring + ) + + return render_template( + "imagesearch/index.html", langid=langid, text=text, search_url=search_url + ) + + @bp.route("/search///", methods=["GET"]) def bing_search(langid, text, searchstring): "Do an image search." @@ -36,7 +67,10 @@ def bing_search(langid, text, searchstring): content = s.read().decode("utf-8") except urllib.error.URLError as e: content = "" - error_msg = e.reason + error_msg = str(e.reason) + except Exception as e: # pylint: disable=broad-exception-caught + content = "" + error_msg = str(e) # Sample data returned by bing image search: # @@ -62,13 +96,13 @@ def build_struct(image): # Also bing seems to throttle images if the count is higher (??). images = images[:25] - return render_template( - "imagesearch/index.html", - langid=langid, - text=text, - images=images, - error_message=error_msg, - ) + ret = { + "langid": langid, + "text": text, + "images": images, + "error_message": error_msg, + } + return jsonify(ret) def _get_dir_and_filename(langid, text): diff --git a/lute/static/js/dict-tabs.js b/lute/static/js/dict-tabs.js index 6c0624f0..80ceabbc 100644 --- a/lute/static/js/dict-tabs.js +++ b/lute/static/js/dict-tabs.js @@ -132,7 +132,7 @@ class ImageLookupButton extends GeneralLookupButton { const raw_bing_url = 'https://www.bing.com/images/search?q=[LUTE]&form=HDRSC2&first=1&tsc=ImageHoverTitle'; const binghash = raw_bing_url.replace('https://www.bing.com/images/search?', ''); - const url = `/bing/search/${LookupButton.LANG_ID}/${encodeURIComponent(use_text)}/${encodeURIComponent(binghash)}`; + const url = `/bing/search_page/${LookupButton.LANG_ID}/${encodeURIComponent(use_text)}/${encodeURIComponent(binghash)}`; iframe.setAttribute("src", url); }; // end handler diff --git a/lute/templates/imagesearch/index.html b/lute/templates/imagesearch/index.html index ef5c21ff..82890ba8 100644 --- a/lute/templates/imagesearch/index.html +++ b/lute/templates/imagesearch/index.html @@ -26,25 +26,12 @@ then paste from your clipboard.

- {% for image in images %} - - {{ image['html'] | safe }} - - {% endfor %} - - {% if error_message != "" %} -

Error contacting image server: {{ error_message }}

- {% endif %} +

Searching ...

+