logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

answerer.py (1768B)


  1. import hashlib
  2. import random
  3. import string
  4. import sys
  5. import uuid
  6. from flask_babel import gettext
  7. # required answerer attribute
  8. # specifies which search query keywords triggers this answerer
  9. keywords = ('random',)
  10. random_int_max = 2**31
  11. if sys.version_info[0] == 2:
  12. random_string_letters = string.lowercase + string.digits + string.uppercase
  13. else:
  14. unicode = str
  15. random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase
  16. def random_characters():
  17. return [random.choice(random_string_letters)
  18. for _ in range(random.randint(8, 32))]
  19. def random_string():
  20. return u''.join(random_characters())
  21. def random_float():
  22. return unicode(random.random())
  23. def random_int():
  24. return unicode(random.randint(-random_int_max, random_int_max))
  25. def random_sha256():
  26. m = hashlib.sha256()
  27. m.update(b''.join(random_characters()))
  28. return unicode(m.hexdigest())
  29. def random_uuid():
  30. return unicode(uuid.uuid4())
  31. random_types = {b'string': random_string,
  32. b'int': random_int,
  33. b'float': random_float,
  34. b'sha256': random_sha256,
  35. b'uuid': random_uuid}
  36. # required answerer function
  37. # can return a list of results (any result type) for a given query
  38. def answer(query):
  39. parts = query.query.split()
  40. if len(parts) != 2:
  41. return []
  42. if parts[1] not in random_types:
  43. return []
  44. return [{'answer': random_types[parts[1]]()}]
  45. # required answerer function
  46. # returns information about the answerer
  47. def self_info():
  48. return {'name': gettext('Random value generator'),
  49. 'description': gettext('Generate different random values'),
  50. 'examples': [u'random {}'.format(x) for x in random_types]}