home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / deskbar-applet / handlers / google-live.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  4.4 KB  |  140 lines

  1. import deskbar.Handler, deskbar.Match, deskbar
  2. from deskbar.Utils import strip_html
  3. import os, cgi
  4. import gobject
  5. import gnomevfs
  6. from os.path import expanduser, exists, join
  7. from gettext import gettext as _
  8. from deskbar.defs import VERSION
  9.  
  10. try:
  11.     from SOAPpy import WSDL
  12. except:
  13.     pass
  14.     
  15. GOOGLE_WSDL = expanduser('~/.gnome2/deskbar-applet/GoogleSearch.wsdl')
  16. GOOGLE_API_KEY = expanduser('~/.gnome2/deskbar-applet/Google.key')
  17. MAX_QUERIES = 10
  18. QUERY_DELAY = 1
  19.  
  20. HELP_TEXT = _("""You need a Google account to use Google Live.  To get one, go to http://api.google.com/
  21.  
  22. When you have created your account, you should recieve a Google API key by mail.  Place this key in the file
  23.  
  24. ~/.gnome2/deskbar-applet/Google.key
  25.  
  26. If you do not receive an API key (or you have lost it) in your account verification mail, then go to www.google.com/accounts and log in.  Go to api.google.com, click "Create Account" and enter your e-mail address and password.  Your API key will be re-sent.
  27.  
  28. Now download the developers kit and extract the GoogleSearch.wsdl file from it.  Copy this file to
  29.  
  30. ~/.gnome2/deskbar-applet/GoogleSearch.wsdl""")
  31.  
  32. def _on_more_information(dialog):
  33.     deskbar.Utils.more_information_dialog(dialog, _("Setting Up Google Live"), HELP_TEXT)
  34.  
  35. def _check_requirements():
  36.     try:
  37.         from SOAPpy import WSDL
  38.     except:
  39.         return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, _("You need to install the SOAPpy python module."), None)
  40.     if not exists (GOOGLE_WSDL):
  41.         return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, _("You need the Google WSDL file."), _on_more_information)
  42.     if not exists (GOOGLE_API_KEY):
  43.         return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, _("You need a Google API key."), _on_more_information)
  44.     else:
  45.         return (deskbar.Handler.HANDLER_IS_HAPPY, None, None)
  46.         
  47. HANDLERS = {
  48.     "GoogleLiveHandler" : {
  49.         "name": _("Google Search"),
  50.         "description": _("Search Google as you type"),
  51.         "requirements" : _check_requirements,
  52.         "version": VERSION,
  53.     }
  54. }
  55.  
  56.  
  57. class GoogleMatch (deskbar.Match.Match):
  58.     def __init__(self, handler, name, url, **args):
  59.         deskbar.Match.Match.__init__ (self, handler, name=name, **args)
  60.         self.url = url
  61.     
  62.     def get_verb(self):
  63.         return "%(name)s"
  64.         
  65.     def action(self, text=None):
  66.         gnomevfs.url_show(self.url)
  67.         
  68.     def get_category(self):
  69.         return "web"
  70.     
  71.     def get_hash(self, text=None):
  72.         return self.url
  73.  
  74. class GoogleLiveHandler (deskbar.Handler.AsyncHandler):
  75.     """
  76.     This handler requires the user to have a valid Google account, a Google
  77.     API key and a GoogleSearch.wsdl file. The file locations are specified
  78.     above.
  79.     
  80.     It uses SOAPpy to interact with Googles SOAP inteface.
  81.     """
  82.     def __init__ (self):
  83.         deskbar.Handler.AsyncHandler.__init__ (self, "google.png")
  84.         self.server = None
  85.         self.api_key = None
  86.         
  87.     def initialize (self):                        
  88.         self.server = WSDL.Proxy (GOOGLE_WSDL)
  89.         
  90.         try:
  91.             proxy = os.environ['http_proxy']
  92.             if proxy.startswith('http://'):
  93.                 proxy = proxy[len('http://'):]
  94.             if proxy.endswith('/'):
  95.                 proxy = proxy[:len(proxy)-1]
  96.                 
  97.             self.server.soapproxy.http_proxy = proxy
  98.             print "Using http_proxy '%s' for google live" % proxy
  99.         except KeyError:
  100.             pass
  101.             
  102.         api_key_file = file (GOOGLE_API_KEY)
  103.         self.api_key = api_key_file.readline()
  104.         api_key_file.close ()
  105.             
  106.     def query (self, qstring):
  107.         """Behold the true power of the AsyncHandler!"""
  108.         
  109.         # Just to ensure we don't bork anything
  110.         qmax = min (deskbar.DEFAULT_RESULTS_PER_HANDLER, MAX_QUERIES)
  111.         
  112.         # Delay before we query so we *don't* make four queries
  113.         # "s", "sp", "spa", "spam".
  114.         self.check_query_changed (timeout=QUERY_DELAY)
  115.         
  116.         #print "GoogleLive: Querying Google for", qstring
  117.         print 'Query google for:', qstring
  118.         results = self.server.doGoogleSearch (self.api_key, # personal google api key
  119.                         qstring,     # query
  120.                         0, qmax,     # start/end of result list
  121.                         True,         # filter duplicates?
  122.                         "",         # get results from specific country
  123.                         False,         # safe search (filter adult material)
  124.                         "",         # get results in specific language
  125.                         "utf-8", "utf-8") # input/output encodings
  126.         print 'Got google answer for:', qstring
  127.         
  128.         # The google search might have taken a long time
  129.         # better check if we're still valid    
  130.         self.check_query_changed ()
  131.         matches = [
  132.             GoogleMatch (self, cgi.escape(strip_html(r.title.encode("utf-8"))), 
  133.                     #r.snippet.encode("utf-8"),  # We don't use the description
  134.                     r.URL.encode ("utf-8"))
  135.             for r in results.resultElements[:qmax-1]
  136.             ]
  137.         self.check_query_changed ()
  138.         print "Returning google answer for:", qstring
  139.         return matches
  140.