external_bang.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. from searx.data import EXTERNAL_BANGS
  3. def get_node(external_bangs_db, bang):
  4. node = external_bangs_db['trie']
  5. after = ''
  6. before = ''
  7. for bang_letter in bang:
  8. after += bang_letter
  9. if after in node and isinstance(node, dict):
  10. node = node[after]
  11. before += after
  12. after = ''
  13. return node, before, after
  14. def get_bang_definition_and_ac(external_bangs_db, bang):
  15. node, before, after = get_node(external_bangs_db, bang)
  16. bang_definition = None
  17. bang_ac_list = []
  18. if after != '':
  19. for k in node:
  20. if k.startswith(after):
  21. bang_ac_list.append(before + k)
  22. elif isinstance(node, dict):
  23. bang_definition = node.get('*')
  24. bang_ac_list = [before + k for k in node.keys() if k != '*']
  25. elif isinstance(node, str):
  26. bang_definition = node
  27. bang_ac_list = []
  28. return bang_definition, bang_ac_list
  29. def resolve_bang_definition(bang_definition, query):
  30. url, rank = bang_definition.split(chr(1))
  31. url = url.replace(chr(2), query)
  32. if url.startswith('//'):
  33. url = 'https:' + url
  34. rank = int(rank) if len(rank) > 0 else 0
  35. return (url, rank)
  36. def get_bang_definition_and_autocomplete(bang, external_bangs_db=None):
  37. global EXTERNAL_BANGS
  38. if external_bangs_db is None:
  39. external_bangs_db = EXTERNAL_BANGS
  40. bang_definition, bang_ac_list = get_bang_definition_and_ac(external_bangs_db, bang)
  41. new_autocomplete = []
  42. current = [*bang_ac_list]
  43. done = set()
  44. while len(current) > 0:
  45. bang_ac = current.pop(0)
  46. done.add(bang_ac)
  47. current_bang_definition, current_bang_ac_list = get_bang_definition_and_ac(external_bangs_db, bang_ac)
  48. if current_bang_definition:
  49. _, order = resolve_bang_definition(current_bang_definition, '')
  50. new_autocomplete.append((bang_ac, order))
  51. for new_bang in current_bang_ac_list:
  52. if new_bang not in done and new_bang not in current:
  53. current.append(new_bang)
  54. new_autocomplete.sort(key=lambda t: (-t[1], t[0]))
  55. new_autocomplete = list(map(lambda t: t[0], new_autocomplete))
  56. return bang_definition, new_autocomplete
  57. def get_bang_url(search_query, external_bangs_db=None):
  58. """
  59. Redirects if the user supplied a correct bang search.
  60. :param search_query: This is a search_query object which contains preferences and the submitted queries.
  61. :return: None if the bang was invalid, else a string of the redirect url.
  62. """
  63. global EXTERNAL_BANGS
  64. if external_bangs_db is None:
  65. external_bangs_db = EXTERNAL_BANGS
  66. if search_query.external_bang:
  67. bang_definition, _ = get_bang_definition_and_ac(external_bangs_db, search_query.external_bang)
  68. return resolve_bang_definition(bang_definition, search_query.query)[0] if bang_definition else None
  69. return None