external_bang.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from searx.data import bangs_loader
  2. # bangs data coming from the following url convert to json with
  3. # https://raw.githubusercontent.com/jivesearch/jivesearch/master/bangs/bangs.toml
  4. # https://pseitz.github.io/toml-to-json-online-converter/
  5. # NOTE only use the get_bang_url
  6. bangs_data = {}
  7. for bang in bangs_loader()['bang']:
  8. for trigger in bang["triggers"]:
  9. bangs_data[trigger] = {x: y for x, y in bang.items() if x != "triggers"}
  10. def get_bang_url(search_query):
  11. """
  12. Redirects if the user supplied a correct bang search.
  13. :param search_query: This is a search_query object which contains preferences and the submitted queries.
  14. :return: None if the bang was invalid, else a string of the redirect url.
  15. """
  16. if search_query.external_bang:
  17. query = search_query.query
  18. bang = _get_bang(search_query.external_bang)
  19. if bang and query:
  20. # TODO add region support.
  21. bang_url = bang["regions"]["default"]
  22. return bang_url.replace("{{{term}}}", query)
  23. return None
  24. def _get_bang(user_bang):
  25. """
  26. Searches if the supplied user bang is available. Returns None if not found.
  27. :param user_bang: The parsed user bang. For example yt
  28. :return: Returns a dict with bangs data (check bangs_data.json for the structure)
  29. """
  30. return bangs_data.get(user_bang)