Skip to content

Commit

Permalink
Merge branch 'fix_500_error_offline_images' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Aug 25, 2024
2 parents 470f0e4 + 7ad04dc commit 0348e03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
43 changes: 23 additions & 20 deletions lute/bing/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,44 @@ def bing_search(langid, text, searchstring):
searchparams = searchstring.replace("###", search)
url = "https://www.bing.com/images/search?" + searchparams
content = ""
with urllib.request.urlopen(url) as s:
content = s.read().decode("utf-8")

# Samples
error_msg = ""
try:
with urllib.request.urlopen(url) as s:
content = s.read().decode("utf-8")
except urllib.error.URLError as e:
content = ""
error_msg = e.reason

# Sample data returned by bing image search:
# <img class="mimg vimgld" ... data-src="https:// ...">
# or
# <img class="mimg rms_img" ... src="https://tse4.mm.bing ..." >

pattern = r"(<img .*?>)"
matches = re.findall(pattern, content, re.I)

images = list(matches)

def is_search_img(img):
return not ('src="/' in img) and ("rms_img" in img or "vimgld" in img)

def fix_data_src(img):
return img.replace("data-src=", "src=")

images = [fix_data_src(i) for i in images if is_search_img(i)]

# Reduce image load count so we don't kill subpage loading.
images = images[:25]

def build_struct(image):
src = "missing"
m = re.search(r'src="(.*?)"', image)
normalized_source = image.replace("data-src=", "src=")
m = re.search(r'src="(.*?)"', normalized_source)
if m:
src = m.group(1)
return {"html": image, "src": src}

data = [build_struct(i) for i in images]
raw_images = list(re.findall(r"(<img .*?>)", content, re.I))

images = [build_struct(i) for i in raw_images if is_search_img(i)]

# Reduce image load count so we don't kill subpage loading.
# 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=data
"imagesearch/index.html",
langid=langid,
text=text,
images=images,
error_message=error_msg,
)


Expand Down
4 changes: 4 additions & 0 deletions lute/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,10 @@ div#termimagesearch span > img {
display:inline;
}

div#termimagesearch span.imageAction {
text-decoration: underline;
}

div#termimagesearch span.imageAction:hover {
color: #C00000;
cursor: pointer;
Expand Down
4 changes: 4 additions & 0 deletions lute/templates/imagesearch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
{{ image['html'] | safe }}
</span>
{% endfor %}

{% if error_message != "" %}
<p><i>Error contacting image server: {{ error_message }}</i></p>
{% endif %}
</div>
</body>

Expand Down

0 comments on commit 0348e03

Please sign in to comment.