90 """Takes parsed JSON from the API server and turns it into a :py:obj:`dict`
93 Attributes `(class Match) <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__
95 - `image_url`, link to the result image.
96 - `domain`, domain this result was found on.
97 - `score`, a number (0 to 100) that indicates how closely the images match.
98 - `width`, image width in pixels.
99 - `height`, image height in pixels.
100 - `size`, image area in pixels.
101 - `format`, image format.
102 - `filesize`, image size in bytes.
103 - `overlay`, overlay URL.
104 - `tags`, whether this match belongs to a collection or stock domain.
106 - `backlinks`, a list of Backlink objects pointing to the original websites
107 and image URLs. List items are instances of :py:obj:`dict`, (`Backlink
108 <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__):
110 - `url`, the image URL to the image.
111 - `backlink`, the original website URL.
112 - `crawl_date`, the date the image was crawled.
121 if "backlinks" in match_json:
123 for backlink_json
in match_json[
"backlinks"]:
124 if not isinstance(backlink_json, dict):
127 crawl_date = backlink_json.get(
"crawl_date")
129 crawl_date = datetime.strptime(crawl_date,
'%Y-%m-%d')
131 crawl_date = datetime.min
135 'url': backlink_json.get(
"url"),
136 'backlink': backlink_json.get(
"backlink"),
137 'crawl_date': crawl_date,
138 'image_name': backlink_json.get(
"image_name"),
143 'image_url': match_json.get(
"image_url"),
144 'domain': match_json.get(
"domain"),
145 'score': match_json.get(
"score"),
146 'width': match_json.get(
"width"),
147 'height': match_json.get(
"height"),
148 'size': match_json.get(
"size"),
149 'image_format': match_json.get(
"format"),
150 'filesize': match_json.get(
"filesize"),
151 'overlay': match_json.get(
"overlay"),
152 'tags': match_json.get(
"tags"),
153 'backlinks': backlinks,
158 """Parse HTTP response from TinEye."""
161 if resp.status_code
in (400, 422):
162 json_data = resp.json()
163 suggestions = json_data.get(
'suggestions', {})
164 message = f
'HTTP Status Code: {resp.status_code}'
166 if resp.status_code == 422:
167 s_key = suggestions.get(
'key',
'')
168 if s_key ==
"Invalid image URL":
170 message = FORMAT_NOT_SUPPORTED
171 elif s_key ==
'NO_SIGNATURE_ERROR':
173 message = NO_SIGNATURE_ERROR
174 elif s_key ==
'Download Error':
176 message = DOWNLOAD_ERROR
178 logger.warning(
"Unknown suggestion key encountered: %s", s_key)
180 description = suggestions.get(
'description')
181 if isinstance(description, list):
182 message =
','.join(description)
186 logger.error(message)
190 resp.raise_for_status()
193 json_data = resp.json()
195 for match_json
in json_data[
'matches']:
198 if not tineye_match[
'backlinks']:
201 backlink = tineye_match[
'backlinks'][0]
204 'template':
'images.html',
205 'url': backlink[
'backlink'],
206 'thumbnail_src': tineye_match[
'image_url'],
207 'source': backlink[
'url'],
208 'title': backlink[
'image_name'],
209 'img_src': backlink[
'url'],
210 'format': tineye_match[
'image_format'],
211 'widht': tineye_match[
'width'],
212 'height': tineye_match[
'height'],
213 'publishedDate': backlink[
'crawl_date'],
219 number_of_results = json_data.get(
'num_matches')
220 if number_of_results:
221 results.append({
'number_of_results': number_of_results})