WebBrowser/ImageSearch/ImageSearchEngine.py

Wed, 27 Apr 2016 19:44:22 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 27 Apr 2016 19:44:22 +0200
changeset 4956
1496516b0f2c
parent 4955
8a966fc19ce3
child 4957
55c9bb589b0a
permissions
-rw-r--r--

Finished implementing the image search functionality for the new web browser.

# -*- coding: utf-8 -*-

# Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the image search engine.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import QObject, QUrl

import Preferences


class ImageSearchEngine(QObject):
    """
    Class implementing the image search engine.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent object
        @type QObject
        """
        super(ImageSearchEngine, self).__init__(parent)
        
        self.__searchEngineNames = ["Google", "TinEye", "Yandex"]
        
        self.__searchEngine = Preferences.getWebBrowser("ImmageSearchEngine")
    
    def searchEngine(self):
        """
        Public method to get the name of the current search engine.
        
        @return name of the current search engine
        @rtype str
        """
        return self.__searchEngine
    
    def setSearchEngine(self, searchEngine):
        """
        Public method to set the current search engine.
        
        @param searchEngine name of the search engine
        @type str
        """
        self.__searchEngine = searchEngine
        Preferences.setWebBrowser("ImmageSearchEngine", searchEngine)
    
    def searchEngineNames(self):
        """
        Public method to get the list of supported search engines.
        
        @return list of supported search engines
        @rtype list of str
        """
        return self.__searchEngineNames[:]
    
    def getSearchQuery(self, imageUrl, searchEngine=None):
        """
        Public method to get the image search query URL.
        
        @param imageUrl URL of the image to search for
        @type QUrl
        @param searchEngine name of the image search engine to be used
        @type str
        @return search query URL
        @rtype QUrl
        """
        if not searchEngine:
            searchEngine = self.__searchEngine
        
        searchEngine_l = searchEngine.lower()
        if searchEngine_l == "google":
            return QUrl("https://www.google.com/searchbyimage?"
                        "site=search&image_url={0}".format(
                            imageUrl.toString()))
        elif searchEngine_l == "yandex":
            return QUrl("https://yandex.com/images/search?"
                        "&img_url={0}&rpt=imageview".format(
                            imageUrl.toString()))
        elif searchEngine_l == "tineye":
            return QUrl("http://www.tineye.com/search?url={0}".format(
                imageUrl.toString()))
        else:
            return QUrl()

eric ide

mercurial