Re: [Deskbar] Yahoo! Handler



Thanks for including it so quickly in CVS (and to magnifying my code as
I'm not a python expert).

I also discovered how easy it is to write a search handler and I wanted
to improve a bit my class so here's my improved version. By default, a
web search will be performed but if your query starts with 'image',
'video' or 'news', the specific Yahoo! webservice will be used.

Laurent

Le mercredi 01 f�ier 2006 �5:41 +0100, Raphael Slinckx a �it :
> On Wed, 2006-02-01 at 12:14 +0100, Laurent wrote:
> > Thanks,
> > 
> > I took the favicon from http://search.yahoo.com and convert it into a png. I didn't do it earlier as I wrote the plugin in my home directory and i 
> > didn't know if it was possible to do the same for artwork. I have also no idea is using Yahoo! own search logo is legal or not. Perhaps it might be 
> > seen as fair use perhaps a permission will be needed : http://docs.yahoo.com/info/copyright/permission.html
> > 
> > About application id, one of the core deskbar developper might want to change it if you want to get access to daily usage statistics : http://api.search.yahoo.com/webservices/usage_data
> 
> Thanks laurent !
> 
> I'm adding this to CVS and it will be shipped in the next release. I
> credited you with this email address, if you want another one, feel free
> to send me another one.
> 
> I also changed the API key as you suggested !
> 
> Raf
> 
> _______________________________________________
> deskbar-applet-list mailing list
> deskbar-applet-list gnome org
> http://mail.gnome.org/mailman/listinfo/deskbar-applet-list
from gettext import gettext as _

import urllib, cgi
import gnomevfs
import deskbar.Handler
import xml.dom.minidom

YAHOO_API_KEY = 'deskbar-applet'
MAX_QUERIES = 10
QUERY_DELAY = 1
YAHOO_URLS = {
	'web': 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?%s',
	'image': 'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?%s',
	'news': 'http://api.search.yahoo.com/NewsSearchService/V1/newsSearch?%s',
	'video': 'http://api.search.yahoo.com/VideoSearchService/V1/videoSearch?%s'
}

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

class YahooMatch(deskbar.Match.Match):
	def __init__(self, handler, category, name, url, **args):
		self.category = category
		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 self.category

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

class YahooHandler(deskbar.Handler.AsyncHandler):
	def __init__(self):
		deskbar.Handler.AsyncHandler.__init__(self, "yahoo.png")
		self.server = None

	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)
		

		# Default service
		service = 'web'
		query = qstring
		url = YAHOO_URLS[service]

		x = qstring.find(" ")
		if x!= -1:
			service = qstring[:x].lower()
			try:
				url = YAHOO_URLS[service]
				query = qstring[x+1:]
			except KeyError:
				# We probably don't find the right service
				# rely on the web service using the full query
				service = 'web'
				pass

		print 'Query Yahoo! (%s) for: %s' % (service, query)

		stream = urllib.urlopen(
				url % 
			urllib.urlencode(
				{'appid': YAHOO_API_KEY,
				'query': query,
				'results': qmax}))
		dom = xml.dom.minidom.parse(stream)
		print 'Got Yahoo! (%s) answer for: %s' % (service, query)
				
		# The Yahoo! search might have taken a long time
		# better check if we're still valid
		self.check_query_changed ()
		return [
			YahooMatch (self, service,
					cgi.escape(r.getElementsByTagName("Title")[0].firstChild.data.encode('utf8')),
					r.getElementsByTagName("ClickUrl")[0].firstChild.data.encode('utf8')
			)
			for r in dom.getElementsByTagName("Result")[:qmax-1]
		]


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