[Deskbar] Yahoo! Handler



Hi all,

I wrote a Yahoo! WebSearch Handler based on the Google Live one and on
the documentation provided by Yahoo!
http://developer.yahoo.net/search/web/V1/webSearch.html

Unlike the Google handler, it doesn't require wsdl for python and the
appid key is only to identify the application: it means the key can be
shipped with the handler.

Laurent Goujon
from gettext import gettext as _

import urllib
import gnomevfs
import deskbar.Handler

YAHOO_API_KEY = 'gnome-deskbar-applet'
YAHOO_URL = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch'
MAX_QUERIES = 10
QUERY_DELAY = 1

def _check_requirements():
	return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)

HANDLERS = {
        "YahooHandler" : {
                "name": _("Yahoo! Search"),
                "description": _("Search on Yahoo! as you type"),
		"requirements": _check_requirements
        }
}

class YahooMatch(deskbar.Match.Match):
	def __init__(self, handler, name, url, **args):
                deskbar.Match.Match.__init__ (self, handler, name=name, **args)
                self.url = url

        def get_verb(self):
                return "%(name)s"

        def action(self, text=None):
                gnomevfs.url_show(self.url)

        def get_category(self):
                return "web"

        def get_hash(self, text=None):
                return self.url

class YahooHandler(deskbar.Handler.AsyncHandler):
        def __init__(self):
                deskbar.Handler.AsyncHandler.__init__(self, "generic.png")
		self.server = None
		self.api_key = None
		self.yahoo_url = None
		self.xml_parser = None

        def initialize(self):
		self.api_key = YAHOO_API_KEY
		self.yahoo_url  = YAHOO_URL

		# Creates XML Parser
		import xml.dom.minidom
		self.xml_parser = xml.dom.minidom.parse

        def query(self, qstring, qmax):
		# Just to ensure we don't bork anything
                qmax = min (qmax, MAX_QUERIES)

                # Delay before we query so we *don't* make four queries
                # "s", "sp", "spa", "spam".
                self.check_query_changed (timeout=QUERY_DELAY)
		
		params = {'appid':self.api_key, 'query': qstring, 'results':qmax}
		encoded_params = urllib.urlencode(params, 1)

		stream = urllib.urlopen('%s?%s' % (self.yahoo_url, encoded_params))
		dom = self.xml_parser(stream)
		
		# The Yahoo! search might have taken a long time
                # better check if we're still valid
		self.check_query_changed ()
                return [
                        YahooMatch (self, 
					r.getElementsByTagName("Title")[0].firstChild.data,
                                        r.getElementsByTagName("ClickUrl")[0].firstChild.data
			)
                        for r in dom.getElementsByTagName("Result")[:qmax-1]
                ]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]