eric6/WebBrowser/OpenSearch/OpenSearchEngine.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the open search engine.
8 """
9
10 from __future__ import unicode_literals
11
12 import re
13 import json
14
15 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QLocale, QUrl, QUrlQuery, \
16 QByteArray, QBuffer, QIODevice, QObject
17 from PyQt5.QtGui import QImage
18 from PyQt5.QtNetwork import QNetworkRequest, QNetworkAccessManager, \
19 QNetworkReply
20
21 from UI.Info import Program
22
23 import Preferences
24 import Utilities
25
26
27 class OpenSearchEngine(QObject):
28 """
29 Class implementing the open search engine.
30
31 @signal imageChanged() emitted after the icon has been changed
32 @signal suggestions(list of strings) emitted after the suggestions have
33 been received
34 """
35 imageChanged = pyqtSignal()
36 suggestions = pyqtSignal(list)
37
38 def __init__(self, parent=None):
39 """
40 Constructor
41
42 @param parent reference to the parent object (QObject)
43 """
44 super(OpenSearchEngine, self).__init__(parent)
45
46 self.__suggestionsReply = None
47 self.__networkAccessManager = None
48 self._name = ""
49 self._description = ""
50 self._searchUrlTemplate = ""
51 self._suggestionsUrlTemplate = ""
52 self._searchParameters = [] # list of two tuples
53 self._suggestionsParameters = [] # list of two tuples
54 self._imageUrl = ""
55 self.__image = QImage()
56 self.__iconMoved = False
57 self.__searchMethod = "get"
58 self.__suggestionsMethod = "get"
59 self.__requestMethods = {
60 "get": QNetworkAccessManager.GetOperation,
61 "post": QNetworkAccessManager.PostOperation,
62 }
63
64 self.__replies = []
65
66 @classmethod
67 def parseTemplate(cls, searchTerm, searchTemplate):
68 """
69 Class method to parse a search template.
70
71 @param searchTerm term to search for (string)
72 @param searchTemplate template to be parsed (string)
73 @return parsed template (string)
74 """
75 locale = QLocale(Preferences.getWebBrowser("SearchLanguage"))
76 language = locale.name().replace("_", "-")
77 country = locale.name().split("_")[0].lower()
78
79 result = searchTemplate
80 result = result.replace("{count}", "20")
81 result = result.replace("{startIndex}", "0")
82 result = result.replace("{startPage}", "0")
83 result = result.replace("{language}", language)
84 result = result.replace("{country}", country)
85 result = result.replace("{inputEncoding}", "UTF-8")
86 result = result.replace("{outputEncoding}", "UTF-8")
87 result = result.replace(
88 "{searchTerms}",
89 bytes(QUrl.toPercentEncoding(searchTerm)).decode())
90 result = re.sub(r"""\{([^\}]*:|)source\??\}""", Program, result)
91
92 return result
93
94 @pyqtSlot(result=str)
95 def name(self):
96 """
97 Public method to get the name of the engine.
98
99 @return name of the engine (string)
100 """
101 return self._name
102
103 def setName(self, name):
104 """
105 Public method to set the engine name.
106
107 @param name name of the engine (string)
108 """
109 self._name = name
110
111 def description(self):
112 """
113 Public method to get the description of the engine.
114
115 @return description of the engine (string)
116 """
117 return self._description
118
119 def setDescription(self, description):
120 """
121 Public method to set the engine description.
122
123 @param description description of the engine (string)
124 """
125 self._description = description
126
127 def searchUrlTemplate(self):
128 """
129 Public method to get the search URL template of the engine.
130
131 @return search URL template of the engine (string)
132 """
133 return self._searchUrlTemplate
134
135 def setSearchUrlTemplate(self, searchUrlTemplate):
136 """
137 Public method to set the engine search URL template.
138
139 The URL template is processed according to the specification:
140 <a
141 href="http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_URL_template_syntax">
142 http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_URL_template_syntax</a>
143
144 A list of template parameters currently supported and what they are
145 replaced with:
146 <table>
147 <tr><td><b>Parameter</b></td><td><b>Value</b></td></tr>
148 <tr><td>{count}</td><td>20</td></tr>
149 <tr><td>{startIndex}</td><td>0</td></tr>
150 <tr><td>{startPage}</td><td>0</td></tr>
151 <tr><td>{language}</td>
152 <td>the default language code (RFC 3066)</td></tr>
153 <tr><td>{country}</td>
154 <td>the default country code (first part of language)</td></tr>
155 <tr><td>{inputEncoding}</td><td>UTF-8</td></tr>
156 <tr><td>{outputEncoding}</td><td>UTF-8</td></tr>
157 <tr><td>{searchTerms}</td><td>the string supplied by the user</td></tr>
158 <tr><td>{*:source}</td>
159 <td>application name, QCoreApplication::applicationName()</td></tr>
160 </table>
161
162 @param searchUrlTemplate search URL template of the engine (string)
163 """
164 self._searchUrlTemplate = searchUrlTemplate
165
166 def searchUrl(self, searchTerm):
167 """
168 Public method to get a URL ready for searching.
169
170 @param searchTerm term to search for (string)
171 @return URL (QUrl)
172 """
173 if not self._searchUrlTemplate:
174 return QUrl()
175
176 ret = QUrl.fromEncoded(
177 self.parseTemplate(searchTerm, self._searchUrlTemplate)
178 .encode("utf-8"))
179
180 if self.__searchMethod != "post":
181 urlQuery = QUrlQuery(ret)
182 for parameter in self._searchParameters:
183 urlQuery.addQueryItem(
184 parameter[0],
185 self.parseTemplate(searchTerm, parameter[1]))
186 ret.setQuery(urlQuery)
187
188 return ret
189
190 def providesSuggestions(self):
191 """
192 Public method to check, if the engine provides suggestions.
193
194 @return flag indicating suggestions are provided (boolean)
195 """
196 return self._suggestionsUrlTemplate != ""
197
198 def suggestionsUrlTemplate(self):
199 """
200 Public method to get the search URL template of the engine.
201
202 @return search URL template of the engine (string)
203 """
204 return self._suggestionsUrlTemplate
205
206 def setSuggestionsUrlTemplate(self, suggestionsUrlTemplate):
207 """
208 Public method to set the engine suggestions URL template.
209
210 @param suggestionsUrlTemplate suggestions URL template of the
211 engine (string)
212 """
213 self._suggestionsUrlTemplate = suggestionsUrlTemplate
214
215 def suggestionsUrl(self, searchTerm):
216 """
217 Public method to get a URL ready for suggestions.
218
219 @param searchTerm term to search for (string)
220 @return URL (QUrl)
221 """
222 if not self._suggestionsUrlTemplate:
223 return QUrl()
224
225 ret = QUrl.fromEncoded(QByteArray(self.parseTemplate(
226 searchTerm, self._suggestionsUrlTemplate).encode("utf-8")))
227
228 if self.__searchMethod != "post":
229 urlQuery = QUrlQuery(ret)
230 for parameter in self._suggestionsParameters:
231 urlQuery.addQueryItem(
232 parameter[0],
233 self.parseTemplate(searchTerm, parameter[1]))
234 ret.setQuery(urlQuery)
235
236 return ret
237
238 def searchParameters(self):
239 """
240 Public method to get the search parameters of the engine.
241
242 @return search parameters of the engine (list of two tuples)
243 """
244 return self._searchParameters[:]
245
246 def setSearchParameters(self, searchParameters):
247 """
248 Public method to set the engine search parameters.
249
250 @param searchParameters search parameters of the engine
251 (list of two tuples)
252 """
253 self._searchParameters = searchParameters[:]
254
255 def suggestionsParameters(self):
256 """
257 Public method to get the suggestions parameters of the engine.
258
259 @return suggestions parameters of the engine (list of two tuples)
260 """
261 return self._suggestionsParameters[:]
262
263 def setSuggestionsParameters(self, suggestionsParameters):
264 """
265 Public method to set the engine suggestions parameters.
266
267 @param suggestionsParameters suggestions parameters of the
268 engine (list of two tuples)
269 """
270 self._suggestionsParameters = suggestionsParameters[:]
271
272 def searchMethod(self):
273 """
274 Public method to get the HTTP request method used to perform search
275 requests.
276
277 @return HTTP request method (string)
278 """
279 return self.__searchMethod
280
281 def setSearchMethod(self, method):
282 """
283 Public method to set the HTTP request method used to perform search
284 requests.
285
286 @param method HTTP request method (string)
287 """
288 requestMethod = method.lower()
289 if requestMethod not in self.__requestMethods:
290 return
291
292 self.__searchMethod = requestMethod
293
294 def suggestionsMethod(self):
295 """
296 Public method to get the HTTP request method used to perform
297 suggestions requests.
298
299 @return HTTP request method (string)
300 """
301 return self.__suggestionsMethod
302
303 def setSuggestionsMethod(self, method):
304 """
305 Public method to set the HTTP request method used to perform
306 suggestions requests.
307
308 @param method HTTP request method (string)
309 """
310 requestMethod = method.lower()
311 if requestMethod not in self.__requestMethods:
312 return
313
314 self.__suggestionsMethod = requestMethod
315
316 def imageUrl(self):
317 """
318 Public method to get the image URL of the engine.
319
320 @return image URL of the engine (string)
321 """
322 return self._imageUrl
323
324 def setImageUrl(self, imageUrl):
325 """
326 Public method to set the engine image URL.
327
328 @param imageUrl image URL of the engine (string)
329 """
330 self._imageUrl = imageUrl
331
332 def setImageUrlAndLoad(self, imageUrl):
333 """
334 Public method to set the engine image URL.
335
336 @param imageUrl image URL of the engine (string)
337 """
338 self.setImageUrl(imageUrl)
339 self.__iconMoved = False
340 self.loadImage()
341
342 def loadImage(self):
343 """
344 Public method to load the image of the engine.
345 """
346 if self.__networkAccessManager is None or not self._imageUrl:
347 return
348
349 reply = self.__networkAccessManager.get(
350 QNetworkRequest(QUrl.fromEncoded(self._imageUrl.encode("utf-8"))))
351 reply.finished.connect(lambda: self.__imageObtained(reply))
352 self.__replies.append(reply)
353
354 def __imageObtained(self, reply):
355 """
356 Private slot to receive the image of the engine.
357
358 @param reply reference to the network reply
359 @type QNetworkReply
360 """
361 response = reply.readAll()
362
363 reply.close()
364 if reply in self.__replies:
365 self.__replies.remove(reply)
366 reply.deleteLater()
367
368 if response.isEmpty():
369 return
370
371 if response.startsWith(b"<html>") or response.startsWith(b"HTML"):
372 self.__iconMoved = True
373 self.__image = QImage()
374 else:
375 self.__image.loadFromData(response)
376 self.imageChanged.emit()
377
378 def image(self):
379 """
380 Public method to get the image of the engine.
381
382 @return image of the engine (QImage)
383 """
384 if not self.__iconMoved and self.__image.isNull():
385 self.loadImage()
386
387 return self.__image
388
389 def setImage(self, image):
390 """
391 Public method to set the image of the engine.
392
393 @param image image to be set (QImage)
394 """
395 if not self._imageUrl:
396 imageBuffer = QBuffer()
397 imageBuffer.open(QIODevice.ReadWrite)
398 if image.save(imageBuffer, "PNG"):
399 self._imageUrl = "data:image/png;base64,{0}".format(
400 bytes(imageBuffer.buffer().toBase64()).decode())
401
402 self.__image = QImage(image)
403 self.imageChanged.emit()
404
405 def isValid(self):
406 """
407 Public method to check, if the engine is valid.
408
409 @return flag indicating validity (boolean)
410 """
411 return self._name and self._searchUrlTemplate
412
413 def __eq__(self, other):
414 """
415 Special method implementing the == operator.
416
417 @param other reference to an open search engine (OpenSearchEngine)
418 @return flag indicating equality (boolean)
419 """
420 if not isinstance(other, OpenSearchEngine):
421 return NotImplemented
422
423 return self._name == other._name and \
424 self._description == other._description and \
425 self._imageUrl == other._imageUrl and \
426 self._searchUrlTemplate == other._searchUrlTemplate and \
427 self._suggestionsUrlTemplate == other._suggestionsUrlTemplate and \
428 self._searchParameters == other._searchParameters and \
429 self._suggestionsParameters == other._suggestionsParameters
430
431 def __lt__(self, other):
432 """
433 Special method implementing the < operator.
434
435 @param other reference to an open search engine (OpenSearchEngine)
436 @return flag indicating less than (boolean)
437 """
438 if not isinstance(other, OpenSearchEngine):
439 return NotImplemented
440
441 return self._name < other._name
442
443 def requestSuggestions(self, searchTerm):
444 """
445 Public method to request suggestions.
446
447 @param searchTerm term to get suggestions for (string)
448 """
449 if not searchTerm or not self.providesSuggestions():
450 return
451
452 if self.__networkAccessManager is None:
453 return
454
455 if self.__suggestionsReply is not None:
456 self.__suggestionsReply.finished.disconnect(
457 self.__suggestionsObtained)
458 self.__suggestionsReply.abort()
459 self.__suggestionsReply.deleteLater()
460 self.__suggestionsReply = None
461
462 if self.__suggestionsMethod not in self.__requestMethods:
463 # ignore
464 return
465
466 if self.__suggestionsMethod == "get":
467 self.__suggestionsReply = self.networkAccessManager().get(
468 QNetworkRequest(self.suggestionsUrl(searchTerm)))
469 else:
470 parameters = []
471 for parameter in self._suggestionsParameters:
472 parameters.append(parameter[0] + "=" + parameter[1])
473 data = "&".join(parameters)
474 self.__suggestionsReply = self.networkAccessManager().post(
475 QNetworkRequest(self.suggestionsUrl(searchTerm)), data)
476 self.__suggestionsReply.finished.connect(
477 self.__suggestionsObtained)
478
479 def __suggestionsObtained(self):
480 """
481 Private slot to receive the suggestions.
482 """
483 if self.__suggestionsReply.error() == QNetworkReply.NoError:
484 buffer = bytes(self.__suggestionsReply.readAll())
485 response = Utilities.decodeBytes(buffer)
486 response = response.strip()
487
488 self.__suggestionsReply.close()
489 self.__suggestionsReply.deleteLater()
490 self.__suggestionsReply = None
491
492 if len(response) == 0:
493 return
494
495 try:
496 result = json.loads(response)
497 except ValueError:
498 return
499
500 try:
501 suggestions = result[1]
502 except IndexError:
503 return
504
505 self.suggestions.emit(suggestions)
506
507 def networkAccessManager(self):
508 """
509 Public method to get a reference to the network access manager object.
510
511 @return reference to the network access manager object
512 (QNetworkAccessManager)
513 """
514 return self.__networkAccessManager
515
516 def setNetworkAccessManager(self, networkAccessManager):
517 """
518 Public method to set the reference to the network access manager.
519
520 @param networkAccessManager reference to the network access manager
521 object (QNetworkAccessManager)
522 """
523 self.__networkAccessManager = networkAccessManager

eric ide

mercurial