eric6/WebBrowser/ImageSearch/ImageSearchEngine.py

Wed, 30 Dec 2020 11:00:44 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:44 +0100
branch
maintenance
changeset 7924
8a96736d465e
parent 7781
607a6098cb44
child 8218
7c09585bd960
permissions
-rw-r--r--

Updated copyright for 2021.

4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7924
8a96736d465e Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the image search engine.
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt5.QtCore import QObject, QUrl
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import Preferences
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
14
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 class ImageSearchEngine(QObject):
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 Class implementing the image search engine.
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 def __init__(self, parent=None):
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Constructor
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 @param parent reference to the parent object
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @type QObject
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 super(ImageSearchEngine, self).__init__(parent)
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
28 self.__searchEngineNames = ["Google", "TinEye", "Yandex"]
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 def searchEngine(self):
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 Public method to get the name of the current search engine.
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
33
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
34 @return name of the current search engine
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
35 @rtype str
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
4957
55c9bb589b0a Added a configuration for the image search to the web browser config page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4956
diff changeset
37 return Preferences.getWebBrowser("ImageSearchEngine")
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 def setSearchEngine(self, searchEngine):
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 Public method to set the current search engine.
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @param searchEngine name of the search engine
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @type str
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
4957
55c9bb589b0a Added a configuration for the image search to the web browser config page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4956
diff changeset
46 Preferences.setWebBrowser("ImageSearchEngine", searchEngine)
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
48 def searchEngineNames(self):
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
49 """
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
50 Public method to get the list of supported search engines.
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
51
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
52 @return list of supported search engines
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
53 @rtype list of str
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
54 """
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
55 return self.__searchEngineNames[:]
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
56
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 def getSearchQuery(self, imageUrl, searchEngine=None):
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 Public method to get the image search query URL.
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 @param imageUrl URL of the image to search for
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 @type QUrl
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 @param searchEngine name of the image search engine to be used
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 @type str
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 @return search query URL
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 @rtype QUrl
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 if not searchEngine:
4957
55c9bb589b0a Added a configuration for the image search to the web browser config page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4956
diff changeset
69 searchEngine = self.searchEngine()
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 searchEngine_l = searchEngine.lower()
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 if searchEngine_l == "google":
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 return QUrl("https://www.google.com/searchbyimage?"
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 "site=search&image_url={0}".format(
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
75 imageUrl.toString()))
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 elif searchEngine_l == "yandex":
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 return QUrl("https://yandex.com/images/search?"
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 "&img_url={0}&rpt=imageview".format(
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
79 imageUrl.toString()))
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 elif searchEngine_l == "tineye":
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 return QUrl("http://www.tineye.com/search?url={0}".format(
4956
1496516b0f2c Finished implementing the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4955
diff changeset
82 imageUrl.toString()))
4955
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 else:
8a966fc19ce3 Started to implement the image search functionality for the new web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 return QUrl()

eric ide

mercurial