summaryrefslogtreecommitdiff
path: root/searx/answerers/random
diff options
context:
space:
mode:
authorAdam Tauber <asciimoo@gmail.com>2016-11-19 20:53:51 +0100
committerAdam Tauber <asciimoo@gmail.com>2016-11-19 20:53:51 +0100
commit971ed0abd159625bd01d0b3ca52c90b394711d77 (patch)
tree952a32dea19318981f959f073a68e731c6c017ea /searx/answerers/random
parent55dc538398090e437c5e495dddad983a7870d09b (diff)
[enh] add quick answer functionality with an example answerer
Diffstat (limited to 'searx/answerers/random')
-rw-r--r--searx/answerers/random/answerer.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/searx/answerers/random/answerer.py b/searx/answerers/random/answerer.py
new file mode 100644
index 000000000..510d9f5be
--- /dev/null
+++ b/searx/answerers/random/answerer.py
@@ -0,0 +1,50 @@
+import random
+import string
+from flask_babel import gettext
+
+# required answerer attribute
+# specifies which search query keywords triggers this answerer
+keywords = ('random',)
+
+random_int_max = 2**31
+
+random_string_letters = string.lowercase + string.digits + string.uppercase
+
+
+def random_string():
+ return u''.join(random.choice(random_string_letters)
+ for _ in range(random.randint(8, 32)))
+
+
+def random_float():
+ return unicode(random.random())
+
+
+def random_int():
+ return unicode(random.randint(-random_int_max, random_int_max))
+
+
+random_types = {u'string': random_string,
+ u'int': random_int,
+ u'float': random_float}
+
+
+# required answerer function
+# can return a list of results (any result type) for a given query
+def answer(query):
+ parts = query.query.split()
+ if len(parts) != 2:
+ return []
+
+ if parts[1] not in random_types:
+ return []
+
+ return [{'answer': random_types[parts[1]]()}]
+
+
+# required answerer function
+# returns information about the answerer
+def self_info():
+ return {'name': gettext('Random value generator'),
+ 'description': gettext('Generate different random values'),
+ 'examples': [u'random {}'.format(x) for x in random_types]}