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