.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.external_bang Namespace Reference

Functions

 get_node (external_bangs_db, bang)
 
 get_bang_definition_and_ac (external_bangs_db, bang)
 
 resolve_bang_definition (bang_definition, query)
 
 get_bang_definition_and_autocomplete (bang, external_bangs_db=None)
 
 get_bang_url (search_query, external_bangs_db=None)
 

Variables

 LEAF_KEY = chr(16)
 

Function Documentation

◆ get_bang_definition_and_ac()

searx.external_bang.get_bang_definition_and_ac ( external_bangs_db,
bang )

Definition at line 23 of file external_bang.py.

23def get_bang_definition_and_ac(external_bangs_db, bang):
24 node, before, after = get_node(external_bangs_db, bang)
25
26 bang_definition = None
27 bang_ac_list = []
28 if after != '':
29 for k in node:
30 if k.startswith(after):
31 bang_ac_list.append(before + k)
32 elif isinstance(node, dict):
33 bang_definition = node.get(LEAF_KEY)
34 bang_ac_list = [before + k for k in node.keys() if k != LEAF_KEY]
35 elif isinstance(node, str):
36 bang_definition = node
37 bang_ac_list = []
38
39 return bang_definition, bang_ac_list
40
41

References searx.external_bang.get_node().

Referenced by searx.external_bang.get_bang_definition_and_autocomplete(), and searx.external_bang.get_bang_url().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_bang_definition_and_autocomplete()

searx.external_bang.get_bang_definition_and_autocomplete ( bang,
external_bangs_db = None )

Definition at line 57 of file external_bang.py.

57def get_bang_definition_and_autocomplete(bang, external_bangs_db=None): # pylint: disable=invalid-name
58 if external_bangs_db is None:
59 external_bangs_db = EXTERNAL_BANGS
60
61 bang_definition, bang_ac_list = get_bang_definition_and_ac(external_bangs_db, bang)
62
63 new_autocomplete = []
64 current = [*bang_ac_list]
65 done = set()
66 while len(current) > 0:
67 bang_ac = current.pop(0)
68 done.add(bang_ac)
69
70 current_bang_definition, current_bang_ac_list = get_bang_definition_and_ac(external_bangs_db, bang_ac)
71 if current_bang_definition:
72 _, order = resolve_bang_definition(current_bang_definition, '')
73 new_autocomplete.append((bang_ac, order))
74 for new_bang in current_bang_ac_list:
75 if new_bang not in done and new_bang not in current:
76 current.append(new_bang)
77
78 new_autocomplete.sort(key=lambda t: (-t[1], t[0]))
79 new_autocomplete = list(map(lambda t: t[0], new_autocomplete))
80
81 return bang_definition, new_autocomplete
82
83

References searx.external_bang.get_bang_definition_and_ac(), and searx.external_bang.resolve_bang_definition().

+ Here is the call graph for this function:

◆ get_bang_url()

searx.external_bang.get_bang_url ( search_query,
external_bangs_db = None )
Redirects if the user supplied a correct bang search.
:param search_query: This is a search_query object which contains preferences and the submitted queries.
:return: None if the bang was invalid, else a string of the redirect url.

Definition at line 84 of file external_bang.py.

84def get_bang_url(search_query, external_bangs_db=None):
85 """
86 Redirects if the user supplied a correct bang search.
87 :param search_query: This is a search_query object which contains preferences and the submitted queries.
88 :return: None if the bang was invalid, else a string of the redirect url.
89 """
90 ret_val = None
91
92 if external_bangs_db is None:
93 external_bangs_db = EXTERNAL_BANGS
94
95 if search_query.external_bang:
96 bang_definition, _ = get_bang_definition_and_ac(external_bangs_db, search_query.external_bang)
97 if bang_definition and isinstance(bang_definition, str):
98 ret_val = resolve_bang_definition(bang_definition, search_query.query)[0]
99
100 return ret_val

References searx.external_bang.get_bang_definition_and_ac(), and searx.external_bang.resolve_bang_definition().

+ Here is the call graph for this function:

◆ get_node()

searx.external_bang.get_node ( external_bangs_db,
bang )

Definition at line 10 of file external_bang.py.

10def get_node(external_bangs_db, bang):
11 node = external_bangs_db['trie']
12 after = ''
13 before = ''
14 for bang_letter in bang:
15 after += bang_letter
16 if after in node and isinstance(node, dict):
17 node = node[after]
18 before += after
19 after = ''
20 return node, before, after
21
22

Referenced by searx.external_bang.get_bang_definition_and_ac().

+ Here is the caller graph for this function:

◆ resolve_bang_definition()

searx.external_bang.resolve_bang_definition ( bang_definition,
query )

Definition at line 42 of file external_bang.py.

42def resolve_bang_definition(bang_definition, query):
43 url, rank = bang_definition.split(chr(1))
44 if url.startswith('//'):
45 url = 'https:' + url
46 if query:
47 url = url.replace(chr(2), quote_plus(query))
48 else:
49 # go to main instead of search page
50 o = urlparse(url)
51 url = o.scheme + '://' + o.netloc
52
53 rank = int(rank) if len(rank) > 0 else 0
54 return (url, rank)
55
56

Referenced by searx.external_bang.get_bang_definition_and_autocomplete(), and searx.external_bang.get_bang_url().

+ Here is the caller graph for this function:

Variable Documentation

◆ LEAF_KEY

searx.external_bang.LEAF_KEY = chr(16)

Definition at line 7 of file external_bang.py.