ask.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Ask.com"""
  3. from urllib.parse import urlencode
  4. import dateutil
  5. from lxml import html
  6. from searx import utils
  7. # Metadata
  8. about = {
  9. "website": "https://www.ask.com/",
  10. "wikidata_id": 'Q847564',
  11. "official_api_documentation": None,
  12. "use_official_api": False,
  13. "require_api_key": False,
  14. "results": "HTML",
  15. }
  16. # Engine Configuration
  17. categories = ['general']
  18. paging = True
  19. # Base URL
  20. base_url = "https://www.ask.com/web"
  21. def request(query, params):
  22. query_params = {
  23. "q": query,
  24. "page": params["pageno"],
  25. }
  26. params["url"] = f"{base_url}?{urlencode(query_params)}"
  27. return params
  28. def response(resp):
  29. start_tag = 'window.MESON.initialState = {'
  30. end_tag = '}};'
  31. dom = html.fromstring(resp.text)
  32. script = utils.eval_xpath_getindex(dom, '//script', 0, default=None).text
  33. pos = script.index(start_tag) + len(start_tag) - 1
  34. script = script[pos:]
  35. pos = script.index(end_tag) + len(end_tag) - 1
  36. script = script[:pos]
  37. json_resp = utils.js_variable_to_python(script)
  38. results = []
  39. for item in json_resp['search']['webResults']['results']:
  40. pubdate_original = item.get('pubdate_original')
  41. if pubdate_original:
  42. pubdate_original = dateutil.parser.parse(pubdate_original)
  43. metadata = [item.get(field) for field in ['category_l1', 'catsy'] if item.get(field)]
  44. results.append(
  45. {
  46. "url": item['url'],
  47. "title": item['title'],
  48. "content": item['abstract'],
  49. "publishedDate": pubdate_original,
  50. # "img_src": item.get('image_url') or None, # these are not thumbs / to large
  51. "metadata": ' | '.join(metadata),
  52. }
  53. )
  54. return results