180def parse_web_api(resp):
181 """Parse results from Qwant's API"""
182
183
184 results = []
185
186
187 search_results = loads(resp.text)
188 data = search_results.get('data', {})
189
190
191 if search_results.get('status') != 'success':
192 error_code = data.get('error_code')
193 if error_code == 24:
194 raise SearxEngineTooManyRequestsException()
195 if search_results.get("data", {}).get("error_data", {}).get("captchaUrl") is not None:
196 raise SearxEngineCaptchaException()
197 msg = ",".join(data.get('message', ['unknown']))
198 raise SearxEngineAPIException(f"{msg} ({error_code})")
199
200
201 raise_for_httperror(resp)
202
203 if qwant_categ == 'web':
204
205
206
207 mainline = data.get('result', {}).get('items', {}).get('mainline', {})
208 else:
209
210
211
212 mainline = data.get('result', {}).get('items', [])
213 mainline = [
214 {'type': qwant_categ, 'items': mainline},
215 ]
216
217
218 if not mainline:
219 return []
220
221 for row in mainline:
222 mainline_type = row.get('type', 'web')
223 if mainline_type != qwant_categ:
224 continue
225
226 if mainline_type == 'ads':
227
228 continue
229
230 mainline_items = row.get('items', [])
231 for item in mainline_items:
232
233 title = item.get('title', None)
234 res_url = item.get('url', None)
235
236 if mainline_type == 'web':
237 content = item['desc']
238 results.append(
239 {
240 'title': title,
241 'url': res_url,
242 'content': content,
243 }
244 )
245
246 elif mainline_type == 'news':
247
248 pub_date = item['date']
249 if pub_date is not None:
250 pub_date = datetime.fromtimestamp(pub_date)
251 news_media = item.get('media', [])
252 thumbnail = None
253 if news_media:
254 thumbnail = news_media[0].get('pict', {}).get('url', None)
255 results.append(
256 {
257 'title': title,
258 'url': res_url,
259 'publishedDate': pub_date,
260 'thumbnail': thumbnail,
261 }
262 )
263
264 elif mainline_type == 'images':
265 thumbnail = item['thumbnail']
266 img_src = item['media']
267 results.append(
268 {
269 'title': title,
270 'url': res_url,
271 'template': 'images.html',
272 'thumbnail_src': thumbnail,
273 'img_src': img_src,
274 'resolution': f"{item['width']} x {item['height']}",
275 'img_format': item.get('thumb_type'),
276 }
277 )
278
279 elif mainline_type == 'videos':
280
281
282
283 d, s, c = item.get('desc'), item.get('source'), item.get('channel')
284 content_parts = []
285 if d:
286 content_parts.append(d)
287 if s:
288 content_parts.append("%s: %s " % (gettext("Source"), s))
289 if c:
290 content_parts.append("%s: %s " % (gettext("Channel"), c))
291 content = ' // '.join(content_parts)
292 length = item['duration']
293 if length is not None:
294 length = timedelta(milliseconds=length)
295 pub_date = item['date']
296 if pub_date is not None:
297 pub_date = datetime.fromtimestamp(pub_date)
298 thumbnail = item['thumbnail']
299
300
301 thumbnail = thumbnail.replace('https://s2.qwant.com', 'https://s1.qwant.com', 1)
302 results.append(
303 {
304 'title': title,
305 'url': res_url,
306 'content': content,
307 'iframe_src': get_embeded_stream_url(res_url),
308 'publishedDate': pub_date,
309 'thumbnail': thumbnail,
310 'template': 'videos.html',
311 'length': length,
312 }
313 )
314
315 return results
316
317