mirror of
https://github.com/searxng/searxng.git
synced 2024-11-05 04:40:11 +01:00
[fix] engine tineye: handle 422 response of not supported img format
Closes: https://github.com/searxng/searxng/issues/1449 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
parent
50d714d829
commit
1540891561
@ -17,6 +17,7 @@ billion images `[tineye.com] <https://tineye.com/how>`_.
|
|||||||
|
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from flask_babel import gettext
|
||||||
|
|
||||||
about = {
|
about = {
|
||||||
"website": 'https://tineye.com',
|
"website": 'https://tineye.com',
|
||||||
@ -28,20 +29,41 @@ about = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine_type = 'online_url_search'
|
engine_type = 'online_url_search'
|
||||||
|
""":py:obj:`searx.search.processors.online_url_search`"""
|
||||||
|
|
||||||
categories = ['general']
|
categories = ['general']
|
||||||
paging = True
|
paging = True
|
||||||
safesearch = False
|
safesearch = False
|
||||||
base_url = 'https://tineye.com'
|
base_url = 'https://tineye.com'
|
||||||
search_string = '/result_json/?page={page}&{query}'
|
search_string = '/result_json/?page={page}&{query}'
|
||||||
|
|
||||||
|
FORMAT_NOT_SUPPORTED = gettext(
|
||||||
|
"Could not read that image url. This may be due to an unsupported file"
|
||||||
|
" format. TinEye only supports images that are JPEG, PNG, GIF, BMP, TIFF or WebP."
|
||||||
|
)
|
||||||
|
"""TinEye error message"""
|
||||||
|
|
||||||
|
NO_SIGNATURE_ERROR = gettext(
|
||||||
|
"The image is too simple to find matches. TinEye requires a basic level of"
|
||||||
|
" visual detail to successfully identify matches."
|
||||||
|
)
|
||||||
|
"""TinEye error message"""
|
||||||
|
|
||||||
|
DOWNLOAD_ERROR = gettext("The image could not be downloaded.")
|
||||||
|
"""TinEye error message"""
|
||||||
|
|
||||||
|
|
||||||
def request(query, params):
|
def request(query, params):
|
||||||
|
"""Build TinEye HTTP request using ``search_urls`` of a :py:obj:`engine_type`."""
|
||||||
|
|
||||||
|
params['raise_for_httperror'] = False
|
||||||
|
|
||||||
if params['search_urls']['data:image']:
|
if params['search_urls']['data:image']:
|
||||||
query = params['search_urls']['data:image']
|
query = params['search_urls']['data:image']
|
||||||
elif params['search_urls']['http']:
|
elif params['search_urls']['http']:
|
||||||
query = params['search_urls']['http']
|
query = params['search_urls']['http']
|
||||||
|
|
||||||
|
logger.debug("query URL: %s", query)
|
||||||
query = urlencode({'url': query})
|
query = urlencode({'url': query})
|
||||||
|
|
||||||
# see https://github.com/TinEye/pytineye/blob/main/pytineye/api.py
|
# see https://github.com/TinEye/pytineye/blob/main/pytineye/api.py
|
||||||
@ -59,45 +81,145 @@ def request(query, params):
|
|||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
def response(resp):
|
def parse_tineye_match(match_json):
|
||||||
results = []
|
"""Takes parsed JSON from the API server and turns it into a :py:obj:`dict`
|
||||||
|
object.
|
||||||
|
|
||||||
# Define wanted results
|
Attributes `(class Match) <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__
|
||||||
json_data = resp.json()
|
|
||||||
number_of_results = json_data['num_matches']
|
|
||||||
|
|
||||||
for i in json_data['matches']:
|
- `image_url`, link to the result image.
|
||||||
image_format = i['format']
|
- `domain`, domain this result was found on.
|
||||||
width = i['width']
|
- `score`, a number (0 to 100) that indicates how closely the images match.
|
||||||
height = i['height']
|
- `width`, image width in pixels.
|
||||||
thumbnail_src = i['image_url']
|
- `height`, image height in pixels.
|
||||||
backlink = i['domains'][0]['backlinks'][0]
|
- `size`, image area in pixels.
|
||||||
url = backlink['backlink']
|
- `format`, image format.
|
||||||
source = backlink['url']
|
- `filesize`, image size in bytes.
|
||||||
title = backlink['image_name']
|
- `overlay`, overlay URL.
|
||||||
img_src = backlink['url']
|
- `tags`, whether this match belongs to a collection or stock domain.
|
||||||
|
|
||||||
# Get and convert published date
|
- `backlinks`, a list of Backlink objects pointing to the original websites
|
||||||
api_date = backlink['crawl_date'][:-3]
|
and image URLs. List items are instances of :py:obj:`dict`, (`Backlink
|
||||||
publishedDate = datetime.fromisoformat(api_date)
|
<https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__):
|
||||||
|
|
||||||
# Append results
|
- `url`, the image URL to the image.
|
||||||
results.append(
|
- `backlink`, the original website URL.
|
||||||
|
- `crawl_date`, the date the image was crawled.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# HINT: there exists an alternative backlink dict in the domains list / e.g.::
|
||||||
|
#
|
||||||
|
# match_json['domains'][0]['backlinks']
|
||||||
|
|
||||||
|
backlinks = []
|
||||||
|
if "backlinks" in match_json:
|
||||||
|
|
||||||
|
for backlink_json in match_json["backlinks"]:
|
||||||
|
if not isinstance(backlink_json, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
|
crawl_date = backlink_json.get("crawl_date")
|
||||||
|
if crawl_date:
|
||||||
|
crawl_date = datetime.fromisoformat(crawl_date[:-3])
|
||||||
|
else:
|
||||||
|
crawl_date = datetime.min
|
||||||
|
|
||||||
|
backlinks.append(
|
||||||
{
|
{
|
||||||
'template': 'images.html',
|
'url': backlink_json.get("url"),
|
||||||
'url': url,
|
'backlink': backlink_json.get("backlink"),
|
||||||
'thumbnail_src': thumbnail_src,
|
'crawl_date': crawl_date,
|
||||||
'source': source,
|
'image_name': backlink_json.get("image_name"),
|
||||||
'title': title,
|
|
||||||
'img_src': img_src,
|
|
||||||
'format': image_format,
|
|
||||||
'widht': width,
|
|
||||||
'height': height,
|
|
||||||
'publishedDate': publishedDate,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Append number of results
|
return {
|
||||||
|
'image_url': match_json.get("image_url"),
|
||||||
|
'domain': match_json.get("domain"),
|
||||||
|
'score': match_json.get("score"),
|
||||||
|
'width': match_json.get("width"),
|
||||||
|
'height': match_json.get("height"),
|
||||||
|
'size': match_json.get("size"),
|
||||||
|
'image_format': match_json.get("format"),
|
||||||
|
'filesize': match_json.get("filesize"),
|
||||||
|
'overlay': match_json.get("overlay"),
|
||||||
|
'tags': match_json.get("tags"),
|
||||||
|
'backlinks': backlinks,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def response(resp):
|
||||||
|
"""Parse HTTP response from TinEye."""
|
||||||
|
results = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
json_data = resp.json()
|
||||||
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
|
msg = "can't parse JSON response // %s" % exc
|
||||||
|
logger.error(msg)
|
||||||
|
json_data = {'error': msg}
|
||||||
|
|
||||||
|
# handle error codes from Tineye
|
||||||
|
|
||||||
|
if resp.is_error:
|
||||||
|
if resp.status_code in (400, 422):
|
||||||
|
|
||||||
|
message = 'HTTP status: %s' % resp.status_code
|
||||||
|
error = json_data.get('error')
|
||||||
|
s_key = json_data.get('suggestions', {}).get('key', '')
|
||||||
|
|
||||||
|
if error and s_key:
|
||||||
|
message = "%s (%s)" % (error, s_key)
|
||||||
|
elif error:
|
||||||
|
message = error
|
||||||
|
|
||||||
|
if s_key == "Invalid image URL":
|
||||||
|
# test https://docs.searxng.org/_static/searxng-wordmark.svg
|
||||||
|
message = FORMAT_NOT_SUPPORTED
|
||||||
|
elif s_key == 'NO_SIGNATURE_ERROR':
|
||||||
|
# test https://pngimg.com/uploads/dot/dot_PNG4.png
|
||||||
|
message = NO_SIGNATURE_ERROR
|
||||||
|
elif s_key == 'Download Error':
|
||||||
|
# test https://notexists
|
||||||
|
message = DOWNLOAD_ERROR
|
||||||
|
|
||||||
|
# see https://github.com/searxng/searxng/pull/1456#issuecomment-1193105023
|
||||||
|
# results.append({'answer': message})
|
||||||
|
logger.error(message)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
resp.raise_for_status()
|
||||||
|
|
||||||
|
# append results from matches
|
||||||
|
|
||||||
|
for match_json in json_data['matches']:
|
||||||
|
|
||||||
|
tineye_match = parse_tineye_match(match_json)
|
||||||
|
if not tineye_match['backlinks']:
|
||||||
|
continue
|
||||||
|
|
||||||
|
backlink = tineye_match['backlinks'][0]
|
||||||
|
results.append(
|
||||||
|
{
|
||||||
|
'template': 'images.html',
|
||||||
|
'url': backlink['backlink'],
|
||||||
|
'thumbnail_src': tineye_match['image_url'],
|
||||||
|
'source': backlink['url'],
|
||||||
|
'title': backlink['image_name'],
|
||||||
|
'img_src': backlink['url'],
|
||||||
|
'format': tineye_match['image_format'],
|
||||||
|
'widht': tineye_match['width'],
|
||||||
|
'height': tineye_match['height'],
|
||||||
|
'publishedDate': backlink['crawl_date'],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# append number of results
|
||||||
|
|
||||||
|
number_of_results = json_data.get('num_matches')
|
||||||
|
if number_of_results:
|
||||||
results.append({'number_of_results': number_of_results})
|
results.append({'number_of_results': number_of_results})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
Loading…
Reference in New Issue
Block a user