86 """Takes parsed JSON from the API server and turns it into a :py:obj:`dict`
89 Attributes `(class Match) <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__
91 - `image_url`, link to the result image.
92 - `domain`, domain this result was found on.
93 - `score`, a number (0 to 100) that indicates how closely the images match.
94 - `width`, image width in pixels.
95 - `height`, image height in pixels.
96 - `size`, image area in pixels.
97 - `format`, image format.
98 - `filesize`, image size in bytes.
99 - `overlay`, overlay URL.
100 - `tags`, whether this match belongs to a collection or stock domain.
102 - `backlinks`, a list of Backlink objects pointing to the original websites
103 and image URLs. List items are instances of :py:obj:`dict`, (`Backlink
104 <https://github.com/TinEye/pytineye/blob/main/pytineye/api.py>`__):
106 - `url`, the image URL to the image.
107 - `backlink`, the original website URL.
108 - `crawl_date`, the date the image was crawled.
117 if "backlinks" in match_json:
119 for backlink_json
in match_json[
"backlinks"]:
120 if not isinstance(backlink_json, dict):
123 crawl_date = backlink_json.get(
"crawl_date")
125 crawl_date = datetime.strptime(crawl_date,
'%Y-%m-%d')
127 crawl_date = datetime.min
131 'url': backlink_json.get(
"url"),
132 'backlink': backlink_json.get(
"backlink"),
133 'crawl_date': crawl_date,
134 'image_name': backlink_json.get(
"image_name"),
139 'image_url': match_json.get(
"image_url"),
140 'domain': match_json.get(
"domain"),
141 'score': match_json.get(
"score"),
142 'width': match_json.get(
"width"),
143 'height': match_json.get(
"height"),
144 'size': match_json.get(
"size"),
145 'image_format': match_json.get(
"format"),
146 'filesize': match_json.get(
"filesize"),
147 'overlay': match_json.get(
"overlay"),
148 'tags': match_json.get(
"tags"),
149 'backlinks': backlinks,
154 """Parse HTTP response from TinEye."""
158 if resp.status_code
in (400, 422):
159 json_data = resp.json()
160 suggestions = json_data.get(
'suggestions', {})
161 message = f
'HTTP Status Code: {resp.status_code}'
163 if resp.status_code == 422:
164 s_key = suggestions.get(
'key',
'')
165 if s_key ==
"Invalid image URL":
167 message = FORMAT_NOT_SUPPORTED
168 elif s_key ==
'NO_SIGNATURE_ERROR':
170 message = NO_SIGNATURE_ERROR
171 elif s_key ==
'Download Error':
173 message = DOWNLOAD_ERROR
175 logger.warning(
"Unknown suggestion key encountered: %s", s_key)
177 description = suggestions.get(
'description')
178 if isinstance(description, list):
179 message =
','.join(description)
187 resp.raise_for_status()
189 json_data = resp.json()
191 for match_json
in json_data[
'matches']:
194 if not tineye_match[
'backlinks']:
197 backlink = tineye_match[
'backlinks'][0]
200 'template':
'images.html',
201 'url': backlink[
'backlink'],
202 'thumbnail_src': tineye_match[
'image_url'],
203 'source': backlink[
'url'],
204 'title': backlink[
'image_name'],
205 'img_src': backlink[
'url'],
206 'format': tineye_match[
'image_format'],
207 'width': tineye_match[
'width'],
208 'height': tineye_match[
'height'],
209 'publishedDate': backlink[
'crawl_date'],
215 number_of_results = json_data.get(
'num_matches')
216 if number_of_results:
217 results.append({
'number_of_results': number_of_results})