.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.search.processors.online.OnlineProcessor Class Reference
+ Inheritance diagram for searx.search.processors.online.OnlineProcessor:
+ Collaboration diagram for searx.search.processors.online.OnlineProcessor:

Public Member Functions

 initialize (self)
 
 get_params (self, search_query, engine_category)
 
 search (self, query, params, result_container, start_time, timeout_limit)
 
 get_default_tests (self)
 
- Public Member Functions inherited from searx.search.processors.abstract.EngineProcessor
 __init__ (self, engine, str engine_name)
 
 has_initialize_function (self)
 
 handle_exception (self, result_container, exception_or_message, suspend=False)
 
 extend_container (self, result_container, start_time, search_results)
 
 extend_container_if_suspended (self, result_container)
 
 get_tests (self)
 

Public Attributes

 engine_name
 
 engine
 
- Public Attributes inherited from searx.search.processors.abstract.EngineProcessor
 engine = engine
 
 engine_name = engine_name
 
 logger = engines[engine_name].logger
 
 suspended_status = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())
 

Static Public Attributes

str engine_type = 'online'
 

Protected Member Functions

 _send_http_request (self, params)
 
 _search_basic (self, query, params)
 
- Protected Member Functions inherited from searx.search.processors.abstract.EngineProcessor
 _extend_container_basic (self, result_container, start_time, search_results)
 

Detailed Description

Processor class for ``online`` engines.

Definition at line 37 of file online.py.

Member Function Documentation

◆ _search_basic()

searx.search.processors.online.OnlineProcessor._search_basic ( self,
query,
params )
protected

Definition at line 134 of file online.py.

134 def _search_basic(self, query, params):
135 # update request parameters dependent on
136 # search-engine (contained in engines folder)
137 self.engine.request(query, params)
138
139 # ignoring empty urls
140 if params['url'] is None:
141 return None
142
143 if not params['url']:
144 return None
145
146 # send request
147 response = self._send_http_request(params)
148
149 # parse the response
150 response.search_params = params
151 return self.engine.response(response)
152

References searx.search.processors.online.OnlineProcessor._send_http_request(), searx.search.processors.abstract.EngineProcessor.engine, searx.search.processors.online.OnlineProcessor.engine, and searx.search.processors.online_dictionary.OnlineDictionaryProcessor.engine.

Referenced by searx.search.processors.offline.OfflineProcessor.search(), and searx.search.processors.online.OnlineProcessor.search().

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

◆ _send_http_request()

searx.search.processors.online.OnlineProcessor._send_http_request ( self,
params )
protected

Definition at line 79 of file online.py.

79 def _send_http_request(self, params):
80 # create dictionary which contain all
81 # information about the request
82 request_args = dict(headers=params['headers'], cookies=params['cookies'], auth=params['auth'])
83
84 # verify
85 # if not None, it overrides the verify value defined in the network.
86 # use False to accept any server certificate
87 # use a path to file to specify a server certificate
88 verify = params.get('verify')
89 if verify is not None:
90 request_args['verify'] = params['verify']
91
92 # max_redirects
93 max_redirects = params.get('max_redirects')
94 if max_redirects:
95 request_args['max_redirects'] = max_redirects
96
97 # allow_redirects
98 if 'allow_redirects' in params:
99 request_args['allow_redirects'] = params['allow_redirects']
100
101 # soft_max_redirects
102 soft_max_redirects = params.get('soft_max_redirects', max_redirects or 0)
103
104 # raise_for_status
105 request_args['raise_for_httperror'] = params.get('raise_for_httperror', True)
106
107 # specific type of request (GET or POST)
108 if params['method'] == 'GET':
109 req = searx.network.get
110 else:
111 req = searx.network.post
112
113 request_args['data'] = params['data']
114
115 # send the request
116 response = req(params['url'], **request_args)
117
118 # check soft limit of the redirect count
119 if len(response.history) > soft_max_redirects:
120 # unexpected redirect : record an error
121 # but the engine might still return valid results.
122 status_code = str(response.status_code or '')
123 reason = response.reason_phrase or ''
124 hostname = response.url.host
125 count_error(
126 self.engine_name,
127 '{} redirects, maximum: {}'.format(len(response.history), soft_max_redirects),
128 (status_code, reason, hostname),
129 secondary=True,
130 )
131
132 return response
133

References searx.search.processors.abstract.EngineProcessor.engine_name, searx.search.processors.offline.OfflineProcessor.engine_name, searx.search.processors.online.OnlineProcessor.engine_name, and searx.format.

Referenced by searx.search.processors.online.OnlineProcessor._search_basic().

+ Here is the caller graph for this function:

◆ get_default_tests()

searx.search.processors.online.OnlineProcessor.get_default_tests ( self)

Reimplemented from searx.search.processors.abstract.EngineProcessor.

Reimplemented in searx.search.processors.online_currency.OnlineCurrencyProcessor, and searx.search.processors.online_dictionary.OnlineDictionaryProcessor.

Definition at line 198 of file online.py.

198 def get_default_tests(self):
199 tests = {}
200
201 tests['simple'] = {
202 'matrix': {'query': ('life', 'computer')},
203 'result_container': ['not_empty'],
204 }
205
206 if getattr(self.engine, 'paging', False):
207 tests['paging'] = {
208 'matrix': {'query': 'time', 'pageno': (1, 2, 3)},
209 'result_container': ['not_empty'],
210 'test': ['unique_results'],
211 }
212 if 'general' in self.engine.categories:
213 # avoid documentation about HTML tags (<time> and <input type="time">)
214 tests['paging']['matrix']['query'] = 'news'
215
216 if getattr(self.engine, 'time_range', False):
217 tests['time_range'] = {
218 'matrix': {'query': 'news', 'time_range': (None, 'day')},
219 'result_container': ['not_empty'],
220 'test': ['unique_results'],
221 }
222
223 if getattr(self.engine, 'traits', False):
224 tests['lang_fr'] = {
225 'matrix': {'query': 'paris', 'lang': 'fr'},
226 'result_container': ['not_empty', ('has_language', 'fr')],
227 }
228 tests['lang_en'] = {
229 'matrix': {'query': 'paris', 'lang': 'en'},
230 'result_container': ['not_empty', ('has_language', 'en')],
231 }
232
233 if getattr(self.engine, 'safesearch', False):
234 tests['safesearch'] = {'matrix': {'query': 'porn', 'safesearch': (0, 2)}, 'test': ['unique_results']}
235
236 return tests

◆ get_params()

searx.search.processors.online.OnlineProcessor.get_params ( self,
search_query,
engine_category )
Returns a set of :ref:`request params <engine request online>` or ``None``
if request is not supported.

Reimplemented from searx.search.processors.abstract.EngineProcessor.

Reimplemented in searx.search.processors.online_currency.OnlineCurrencyProcessor, searx.search.processors.online_dictionary.OnlineDictionaryProcessor, and searx.search.processors.online_url_search.OnlineUrlSearchProcessor.

Definition at line 51 of file online.py.

51 def get_params(self, search_query, engine_category):
52 """Returns a set of :ref:`request params <engine request online>` or ``None``
53 if request is not supported.
54 """
55 params = super().get_params(search_query, engine_category)
56 if params is None:
57 return None
58
59 # add default params
60 params.update(default_request_params())
61
62 # add an user agent
63 params['headers']['User-Agent'] = gen_useragent()
64
65 # add Accept-Language header
66 if self.engine.send_accept_language_header and search_query.locale:
67 ac_lang = search_query.locale.language
68 if search_query.locale.territory:
69 ac_lang = "%s-%s,%s;q=0.9,*;q=0.5" % (
70 search_query.locale.language,
71 search_query.locale.territory,
72 search_query.locale.language,
73 )
74 params['headers']['Accept-Language'] = ac_lang
75
76 self.logger.debug('HTTP Accept-Language: %s', params['headers'].get('Accept-Language', ''))
77 return params
78

References searx.search.processors.online.default_request_params(), searx.search.processors.abstract.EngineProcessor.engine, searx.search.processors.online.OnlineProcessor.engine, searx.search.processors.online_dictionary.OnlineDictionaryProcessor.engine, searx.search.processors.online.OnlineProcessor.get_params(), and searx.search.processors.abstract.EngineProcessor.logger.

Referenced by searx.search.processors.online.OnlineProcessor.get_params().

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

◆ initialize()

searx.search.processors.online.OnlineProcessor.initialize ( self)

Reimplemented from searx.search.processors.abstract.EngineProcessor.

Definition at line 42 of file online.py.

42 def initialize(self):
43 # set timeout for all HTTP requests
44 searx.network.set_timeout_for_thread(self.engine.timeout, start_time=default_timer())
45 # reset the HTTP total time
47 # set the network
49 super().initialize()
50
set_context_network_name(network_name)
Definition __init__.py:39
set_timeout_for_thread(timeout, start_time=None)
Definition __init__.py:34
reset_time_for_thread()
Definition __init__.py:25

References searx.search.processors.abstract.EngineProcessor.engine, searx.search.processors.online.OnlineProcessor.engine, searx.search.processors.online_dictionary.OnlineDictionaryProcessor.engine, searx.network.reset_time_for_thread(), and searx.network.set_timeout_for_thread().

+ Here is the call graph for this function:

◆ search()

searx.search.processors.online.OnlineProcessor.search ( self,
query,
params,
result_container,
start_time,
timeout_limit )

Reimplemented from searx.search.processors.abstract.EngineProcessor.

Definition at line 153 of file online.py.

153 def search(self, query, params, result_container, start_time, timeout_limit):
154 # set timeout for all HTTP requests
155 searx.network.set_timeout_for_thread(timeout_limit, start_time=start_time)
156 # reset the HTTP total time
158 # set the network
160
161 try:
162 # send requests and parse the results
163 search_results = self._search_basic(query, params)
164 self.extend_container(result_container, start_time, search_results)
165 except ssl.SSLError as e:
166 # requests timeout (connect or read)
167 self.handle_exception(result_container, e, suspend=True)
168 self.logger.error("SSLError {}, verify={}".format(e, searx.network.get_network(self.engine_name).verify))
169 except (httpx.TimeoutException, asyncio.TimeoutError) as e:
170 # requests timeout (connect or read)
171 self.handle_exception(result_container, e, suspend=True)
172 self.logger.error(
173 "HTTP requests timeout (search duration : {0} s, timeout: {1} s) : {2}".format(
174 default_timer() - start_time, timeout_limit, e.__class__.__name__
175 )
176 )
177 except (httpx.HTTPError, httpx.StreamError) as e:
178 # other requests exception
179 self.handle_exception(result_container, e, suspend=True)
180 self.logger.exception(
181 "requests exception (search duration : {0} s, timeout: {1} s) : {2}".format(
182 default_timer() - start_time, timeout_limit, e
183 )
184 )
185 except SearxEngineCaptchaException as e:
186 self.handle_exception(result_container, e, suspend=True)
187 self.logger.exception('CAPTCHA')
188 except SearxEngineTooManyRequestsException as e:
189 self.handle_exception(result_container, e, suspend=True)
190 self.logger.exception('Too many requests')
191 except SearxEngineAccessDeniedException as e:
192 self.handle_exception(result_container, e, suspend=True)
193 self.logger.exception('SearXNG is blocked')
194 except Exception as e: # pylint: disable=broad-except
195 self.handle_exception(result_container, e)
196 self.logger.exception('exception : {0}'.format(e))
197

References searx.search.processors.offline.OfflineProcessor._search_basic(), searx.search.processors.online.OnlineProcessor._search_basic(), searx.search.processors.abstract.EngineProcessor.engine_name, searx.search.processors.offline.OfflineProcessor.engine_name, searx.search.processors.online.OnlineProcessor.engine_name, searx.search.processors.abstract.EngineProcessor.extend_container(), searx.format, searx.search.processors.abstract.EngineProcessor.handle_exception(), searx.search.processors.abstract.EngineProcessor.logger, searx.network.reset_time_for_thread(), searx.network.set_context_network_name(), and searx.network.set_timeout_for_thread().

+ Here is the call graph for this function:

Member Data Documentation

◆ engine

◆ engine_name

◆ engine_type

str searx.search.processors.online.OnlineProcessor.engine_type = 'online'
static

Definition at line 40 of file online.py.


The documentation for this class was generated from the following file: