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.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.4 KB  |  128 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import deskbar.Handler as deskbar
  5. import deskbar.Match as deskbar
  6. import deskbar
  7. from deskbar.Utils import strip_html
  8. import os
  9. import cgi
  10. import gobject
  11. import gnomevfs
  12. from os.path import expanduser, exists, join
  13. from gettext import gettext as _
  14. from deskbar.defs import VERSION
  15.  
  16. try:
  17.     from SOAPpy import WSDL
  18. except:
  19.     pass
  20.  
  21. GOOGLE_WSDL = expanduser('~/.gnome2/deskbar-applet/GoogleSearch.wsdl')
  22. GOOGLE_API_KEY = expanduser('~/.gnome2/deskbar-applet/Google.key')
  23. MAX_QUERIES = 10
  24. QUERY_DELAY = 1
  25. HELP_TEXT = _('You need a Google account to use Google Live.  To get one, go to http://api.google.com/\n\nWhen you have created your account, you should recieve a Google API key by mail.  Place this key in the file\n\n~/.gnome2/deskbar-applet/Google.key\n\nIf 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.\n\nNow download the developers kit and extract the GoogleSearch.wsdl file from it.  Copy this file to\n\n~/.gnome2/deskbar-applet/GoogleSearch.wsdl')
  26.  
  27. def _on_more_information(dialog):
  28.     deskbar.Utils.more_information_dialog(dialog, _('Setting Up Google Live'), HELP_TEXT)
  29.  
  30.  
  31. def _check_requirements():
  32.     
  33.     try:
  34.         WSDL = WSDL
  35.         import SOAPpy
  36.     except:
  37.         return (deskbar.Handler.HANDLER_IS_NOT_APPLICABLE, _('You need to install the SOAPpy python module.'), None)
  38.  
  39.     if not exists(GOOGLE_WSDL):
  40.         return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, _('You need the Google WSDL file.'), _on_more_information)
  41.     
  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. class GoogleMatch(deskbar.Match.Match):
  55.     
  56.     def __init__(self, handler, name, url, **args):
  57.         deskbar.Match.Match.__init__(self, handler, name = name, **args)
  58.         self.url = url
  59.  
  60.     
  61.     def get_verb(self):
  62.         return '%(name)s'
  63.  
  64.     
  65.     def action(self, text = None):
  66.         gnomevfs.url_show(self.url)
  67.  
  68.     
  69.     def get_category(self):
  70.         return 'web'
  71.  
  72.     
  73.     def get_hash(self, text = None):
  74.         return self.url
  75.  
  76.  
  77.  
  78. class GoogleLiveHandler(deskbar.Handler.AsyncHandler):
  79.     '''
  80. \tThis handler requires the user to have a valid Google account, a Google
  81. \tAPI key and a GoogleSearch.wsdl file. The file locations are specified
  82. \tabove.
  83. \t
  84. \tIt uses SOAPpy to interact with Googles SOAP inteface.
  85. \t'''
  86.     
  87.     def __init__(self):
  88.         deskbar.Handler.AsyncHandler.__init__(self, 'google.png')
  89.         self.server = None
  90.         self.api_key = None
  91.  
  92.     
  93.     def initialize(self):
  94.         self.server = WSDL.Proxy(GOOGLE_WSDL)
  95.         
  96.         try:
  97.             proxy = os.environ['http_proxy']
  98.             if proxy.startswith('http://'):
  99.                 proxy = proxy[len('http://'):]
  100.             
  101.             if proxy.endswith('/'):
  102.                 proxy = proxy[:len(proxy) - 1]
  103.             
  104.             self.server.soapproxy.http_proxy = proxy
  105.             print "Using http_proxy '%s' for google live" % proxy
  106.         except KeyError:
  107.             pass
  108.  
  109.         api_key_file = file(GOOGLE_API_KEY)
  110.         self.api_key = api_key_file.readline()
  111.         api_key_file.close()
  112.  
  113.     
  114.     def query(self, qstring):
  115.         '''Behold the true power of the AsyncHandler!'''
  116.         qmax = min(deskbar.DEFAULT_RESULTS_PER_HANDLER, MAX_QUERIES)
  117.         self.check_query_changed(timeout = QUERY_DELAY)
  118.         print 'Query google for:', qstring
  119.         results = self.server.doGoogleSearch(self.api_key, qstring, 0, qmax, True, '', False, '', 'utf-8', 'utf-8')
  120.         print 'Got google answer for:', qstring
  121.         self.check_query_changed()
  122.         matches = [ GoogleMatch(self, cgi.escape(strip_html(r.title.encode('utf-8'))), r.URL.encode('utf-8')) for r in results.resultElements[:qmax - 1] ]
  123.         self.check_query_changed()
  124.         print 'Returning google answer for:', qstring
  125.         return matches
  126.  
  127.  
  128.