Beispiel #1
0
    def run(self):
        """
        Runs the google command.
        """

        # Google limits their search requests to 2048 bytes, so let's be
        # nice and not send them anything longer than that.
        #
        # See this link for more information:
        #
        #   http://code.google.com/apis/soapsearch/reference.html

        MAX_QUERY_LENGTH = 2048

        if self.parameter != None:
            text = self.parameter
            # '...' gets replaced with current selection
            if "..." in text:
                seldict = selection.get()
                text = text.replace(
                    "...",
                    seldict.get("text", "").strip().strip("\0"))
        else:
            seldict = selection.get()
            text = seldict.get("text", "")

        text = text.strip().strip("\0")
        if not text:
            displayMessage("<p>No text was selected.</p>")
            return

        BASE_URL = "http://www.google.com/search?q=%s"

        if enso.config.PLUGIN_GOOGLE_USE_DEFAULT_LOCALE:
            # Determine the user's default language setting.  Google
            # appears to use the two-letter ISO 639-1 code for setting
            # languages via the 'hl' query argument.
            languageCode, encoding = locale.getdefaultlocale()
            if languageCode:
                language = languageCode.split("_")[0]
            else:
                language = "en"
            BASE_URL = "%s&hl=%s" % (BASE_URL, language)

        # The following is standard convention for transmitting
        # unicode through a URL.
        text = urllib.parse.quote_plus(text.encode("utf-8"))

        finalQuery = BASE_URL % text

        if len(finalQuery) > MAX_QUERY_LENGTH:
            displayMessage("<p>Your query is too long.</p>")
        else:
            # Catch exception, because webbrowser.open sometimes raises exception
            # without any reason
            try:
                webbrowser.open_new_tab(finalQuery)
            except WindowsError as e:
                logging.warning(e)
Beispiel #2
0
    def run( self ):
        """
        Runs the google command.
        """

        # Google limits their search requests to 2048 bytes, so let's be
        # nice and not send them anything longer than that.
        #
        # See this link for more information:
        #
        #   http://code.google.com/apis/soapsearch/reference.html

        MAX_QUERY_LENGTH = 2048

        if self.parameter != None:
            text = self.parameter.decode()
            # '...' gets replaced with current selection
            if "..." in text:
                seldict = selection.get()
                text = text.replace(
                    "...", seldict.get( "text", u"" ).strip().strip("\0"))
        else:
            seldict = selection.get()
            text = seldict.get( "text", u"" )

        text = text.strip().strip("\0")
        if not text:
            displayMessage( "<p>No text was selected.</p>" )
            return

        BASE_URL = "http://www.google.com/search?q=%s"

        if enso.config.PLUGIN_GOOGLE_USE_DEFAULT_LOCALE:
            # Determine the user's default language setting.  Google
            # appears to use the two-letter ISO 639-1 code for setting
            # languages via the 'hl' query argument.
            languageCode, encoding = locale.getdefaultlocale()
            if languageCode:
                language = languageCode.split( "_" )[0]
            else:
                language = "en"
            BASE_URL = "%s&hl=%s" % (BASE_URL, language)

        # The following is standard convention for transmitting
        # unicode through a URL.
        text = urllib.quote_plus( text.encode("utf-8") )

        finalQuery = BASE_URL % text

        if len( finalQuery ) > MAX_QUERY_LENGTH:
            displayMessage( "<p>Your query is too long.</p>" )
        else:
            # Catch exception, because webbrowser.open sometimes raises exception
            # without any reason
            try:
                webbrowser.open_new_tab( finalQuery )
            except WindowsError, e:
                logging.warning(e)
    def run(self):
        """
        Runs the web-search command.
        """

        if self.parameter is not None:
            text = self.parameter.decode()
            # '...' gets replaced with current selection
            if "..." in text:
                seldict = selection.get()
                to_replace = " %s " % seldict.get(
                    "text", u"").strip().strip("\0")
                text = text.replace("...", to_replace)
                text = re.sub(r"\s+", " ", text)
                text = re.sub(r"\r\n", " ", text)
                text = re.sub(r"\n", " ", text)
        else:
            seldict = selection.get()
            text = seldict.get("text", u"")
            text = re.sub(r"\s+", " ", text)

        text = text.strip().strip("\0")
        if not text:
            displayMessage("<p>No text was selected.</p>")
            return

        if len(text) > MAX_QUERY_LENGTH:
            displayMessage("<p>Your query is too long.</p>")
            return

        # For compatibility with older core, use default locale if setting
        # is not used in the config...
        languageCode, _ = locale.getdefaultlocale()
        if languageCode:
            language = languageCode.split("_")[0]
        else:
            language = "en"

        # The following is standard convention for transmitting
        # unicode through a URL.
        text = urllib.quote_plus(text.encode("utf-8"))

        url = self.BASE_URL % {
            "local_tld": LOCAL_TLD, # Used just for Google services
            "langcode": language,
            "query": text,
        }

        # Catch exception, because webbrowser.open sometimes raises exception
        # without any reason
        try:
            webbrowser.open_new_tab(url)
        except Exception as e:
            logging.warning(e)
Beispiel #4
0
    def get_selection(self):
        """
        Retrieves the current selection and returns it as a
        selection dictionary.
        """

        return selection.get()
Beispiel #5
0
    def get_selection(self):
        """
        Retrieves the current selection and returns it as a
        selection dictionary.
        """

        return selection.get()
def cmd_gcalc(ensoapi, query=None):
    u""" Google calculator """

    MAX_QUERY_LENGTH = 2048

    if query is not None:
        query = query.decode()
        # '...' gets replaced with current selection
        if "..." in query:
            seldict = selection.get()
            query = query.replace("...",
                                  seldict.get("text", u"").strip().strip("\0"))
    else:
        seldict = selection.get()
        query = seldict.get("text", u"")

    query = query.strip(" \0\t\r\n")

    if not query:
        ensoapi.display_message(u"No query.")
        return

    if not query.endswith("="):
        query += "="

    if len(query) > MAX_QUERY_LENGTH:
        ensoapi.display_message(u"Your query is too long.")
        return

    start = '<h2 class=r style="font-size:138%"><b>'
    end = '</b>'

    url = "http://www.google.com/search?num=1&q=%s" % urllib.quote_plus(
        query.encode("utf-8"))
    try:
        logging.info(url)
        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'Lynx (textmode)')]
        resp = opener.open(url)
    except urllib2.URLError, e:
        if hasattr(e, 'reason'):
            logging.error("HTTPError %d: %s" % e.reason)
            ensoapi.display_message(u"HTTPError %d: %s" % e.reason)
        elif hasattr(e, 'code'):
            ensoapi.display_message(u"HTTPError #%d" % e.code)
        return
Beispiel #7
0
    def run( self ):
        """
        Runs the google command.
        """

        # Google limits their search requests to 2048 bytes, so let's be
        # nice and not send them anything longer than that.
        #
        # See this link for more information:
        #
        #   http://code.google.com/apis/soapsearch/reference.html

        MAX_QUERY_LENGTH = 2048

        if self.parameter != None:
            text = self.parameter.decode()
        else:
            seldict = selection.get()
            text = seldict.get( "text", u"" )

        text = text.strip()
        if not text:
            displayMessage( "<p>No text was selected.</p>" )
            return

        BASE_URL = "http://www.google.com/search?hl=%s&q=%s"

        # Determine the user's default language setting.  Google
        # appears to use the two-letter ISO 639-1 code for setting
        # languages via the 'hl' query argument.
        languageCode, encoding = locale.getdefaultlocale()
        if languageCode:
            language = languageCode.split( "_" )[0]
        else:
            language = "en"

        # The following is standard convention for transmitting
        # unicode through a URL.
        text = urllib.quote_plus( text.encode("utf-8") )

        finalQuery = BASE_URL % ( language, text )

        if len( finalQuery ) > MAX_QUERY_LENGTH:
            displayMessage( "<p>Your query is too long.</p>" )
        else:
            try:
                webbrowser.open_new_tab( finalQuery )
            except:
                pass
Beispiel #8
0
    def run(self):
        """
        Runs the google command.
        """

        # Google limits their search requests to 2048 bytes, so let's be
        # nice and not send them anything longer than that.
        #
        # See this link for more information:
        #
        #   http://code.google.com/apis/soapsearch/reference.html

        MAX_QUERY_LENGTH = 2048

        if self.parameter != None:
            text = self.parameter.decode()
        else:
            seldict = selection.get()
            text = seldict.get("text", u"")

        text = text.strip()
        if not text:
            displayMessage("<p>No text was selected.</p>")
            return

        BASE_URL = "http://www.google.com/search?hl=%s&q=%s"

        # Determine the user's default language setting.  Google
        # appears to use the two-letter ISO 639-1 code for setting
        # languages via the 'hl' query argument.
        languageCode, encoding = locale.getdefaultlocale()
        if languageCode:
            language = languageCode.split("_")[0]
        else:
            language = "en"

        # The following is standard convention for transmitting
        # unicode through a URL.
        text = urllib.quote_plus(text.encode("utf-8"))

        finalQuery = BASE_URL % (language, text)

        if len(finalQuery) > MAX_QUERY_LENGTH:
            displayMessage("<p>Your query is too long.</p>")
        else:
            try:
                webbrowser.open_new_tab(finalQuery)
            except:
                pass
Beispiel #9
0
 def get_text_selection(default=None):
     """
     Retrieves the current text selection as string.
     Returns default if nothing is selected.
     """
     return selection.get().get("text", default)