unit_converter.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Calculate mathematical expressions using ack#eval
  3. """
  4. from flask_babel import gettext
  5. from searx.data import WIKIDATA_UNITS
  6. name = "Unit converter plugin"
  7. description = gettext("Convert between units")
  8. default_on = True
  9. CONVERT_KEYWORDS = ["in", "to", "as"]
  10. def _convert(from_value, source_si_factor, target_si_factor):
  11. return from_value * source_si_factor / target_si_factor
  12. def _parse_text_and_convert(search, splitted_query):
  13. if len(splitted_query) != 2 or splitted_query[0].strip() == "" or splitted_query[1].strip() == "":
  14. return
  15. from_value = ""
  16. from_unit_key = ""
  17. # only parse digits as value that belong together
  18. read_alpha = False
  19. for c in splitted_query[0]:
  20. if not read_alpha and (c in ("-", ".") or str.isdigit(c)):
  21. from_value += c
  22. read_alpha = True
  23. elif c != " ":
  24. from_unit_key += c
  25. to_unit_key = splitted_query[1].strip()
  26. from_unit = None
  27. to_unit = None
  28. for unit in WIKIDATA_UNITS.values():
  29. if unit['symbol'] == from_unit_key:
  30. from_unit = unit
  31. if unit['symbol'] == to_unit_key:
  32. to_unit = unit
  33. if from_unit and to_unit:
  34. break
  35. if from_unit is None or to_unit is None or to_unit.get('si_name') != from_unit.get('si_name'):
  36. return
  37. result = _convert(float(from_value), from_unit['to_si_factor'], to_unit['to_si_factor'])
  38. search.result_container.answers['conversion'] = {'answer': f"{result:g} {to_unit['symbol']}"}
  39. def post_search(_request, search):
  40. # only convert between units on the first page
  41. if search.search_query.pageno > 1:
  42. return True
  43. query = search.search_query.query
  44. query_parts = query.split(" ")
  45. if len(query_parts) < 3:
  46. return True
  47. for query_part in query_parts:
  48. for keyword in CONVERT_KEYWORDS:
  49. if query_part == keyword:
  50. keyword_split = query.split(keyword, 1)
  51. _parse_text_and_convert(search, keyword_split)
  52. return True
  53. return True