Skip to content

Commit

Permalink
Add note re searching, report failures to UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Jan 31, 2025
1 parent a32b8c3 commit 4780be0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lute/bing/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,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:
# <img class="mimg vimgld" ... data-src="https:// ...">
Expand All @@ -93,14 +96,13 @@ def build_struct(image):
# Also bing seems to throttle images if the count is higher (??).
images = images[:25]

return jsonify(
{
"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):
Expand Down
14 changes: 13 additions & 1 deletion lute/templates/imagesearch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
then paste from your clipboard.
</p>

<p id="image_search_feedback"><i>Searching ...</i></p>

</div>
</body>

Expand Down Expand Up @@ -135,6 +137,14 @@
type: 'GET',
dataType: 'json',
success: function(data) {
if (data.error_message !== "") {
const msg = `Error: ${data.error_message}`;
console.error(msg);
$("#image_search_feedback").text(msg);
return;
}

$("#image_search_feedback").hide();
if (data.images.length === 0) {
const p = document.createElement('p');
p.textContent = 'No images found.';
Expand All @@ -150,7 +160,9 @@
});
},
error: function(xhr, status, error) {
console.error(`Error ${error}; ${status}; ${xhr.responseText}`);
const msg = `Error ${error}; ${status}; ${xhr.responseText}`;
console.error(msg);
$("#image_search_feedback").val(msg);
}
});
});
Expand Down

0 comments on commit 4780be0

Please sign in to comment.