.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
answerer.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4import hashlib
5import random
6import string
7import uuid
8from flask_babel import gettext
9
10# required answerer attribute
11# specifies which search query keywords triggers this answerer
12keywords = ('random',)
13
14random_int_max = 2**31
15random_string_letters = string.ascii_lowercase + string.digits + string.ascii_uppercase
16
17
19 return [random.choice(random_string_letters) for _ in range(random.randint(8, 32))]
20
21
23 return ''.join(random_characters())
24
25
27 return str(random.random())
28
29
31 return str(random.randint(-random_int_max, random_int_max))
32
33
35 m = hashlib.sha256()
36 m.update(''.join(random_characters()).encode())
37 return str(m.hexdigest())
38
39
41 return str(uuid.uuid4())
42
43
45 color = "%06x" % random.randint(0, 0xFFFFFF)
46 return f"#{color.upper()}"
47
48
49random_types = {
50 'string': random_string,
51 'int': random_int,
52 'float': random_float,
53 'sha256': random_sha256,
54 'uuid': random_uuid,
55 'color': random_color,
56}
57
58
59# required answerer function
60# can return a list of results (any result type) for a given query
61def answer(query):
62 parts = query.query.split()
63 if len(parts) != 2:
64 return []
65
66 if parts[1] not in random_types:
67 return []
68
69 return [{'answer': random_types[parts[1]]()}]
70
71
72# required answerer function
73# returns information about the answerer
75 return {
76 'name': gettext('Random value generator'),
77 'description': gettext('Generate different random values'),
78 'examples': ['random {}'.format(x) for x in random_types],
79 }