brave.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Brave (General, news, videos, images)
  4. """
  5. from urllib.parse import urlencode
  6. from lxml import html
  7. from searx.utils import extract_text, eval_xpath, eval_xpath_list
  8. import chompjs, json
  9. import re
  10. about = {
  11. "website": 'https://search.brave.com/',
  12. "wikidata_id": 'Q22906900',
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'HTML',
  17. }
  18. base_url = "https://search.brave.com/"
  19. paging = False
  20. categories = ['images', 'videos', 'news'] # images, videos, news
  21. def request(query, params):
  22. args = {
  23. 'q': query,
  24. 'spellcheck': 1,
  25. }
  26. params["url"] = f"{base_url}{categories[0]}?{urlencode(args)}"
  27. def get_image_results(text):
  28. results = []
  29. datastr = ""
  30. for line in text.split("\n"):
  31. if "const data = " in line:
  32. datastr = line.replace("const data = ", "").strip()[:-1]
  33. break
  34. json_data = chompjs.parse_js_object(datastr)
  35. for result in json_data[1]["data"]["body"]["response"]["results"]:
  36. results.append(
  37. {
  38. 'template': 'images.html',
  39. 'url': result['url'],
  40. 'thumbnail_src': result['thumbnail']['src'],
  41. 'img_src': result['properties']['url'],
  42. 'content': result['description'],
  43. 'title': result['title'],
  44. 'source': result['source'],
  45. 'img_format': result['properties']['format'],
  46. }
  47. )
  48. return results
  49. def response(resp):
  50. dom = html.fromstring(resp.text)
  51. match categories[0]:
  52. case 'images':
  53. return get_image_results(resp.text)
  54. case _:
  55. return []