Share Pastebin
Guest
Public paste!

Karthik Chikmagalur

By: a guest | May 20th, 2009 | Syntax: Python | Size: 3.80 KB | Hits: 187 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/python
  2.  
  3. ##############################################################################
  4. # GCALC - A command-line interface to the Google calculator. Simply passes
  5. #                the command-line arguments to the Google web-site and displays the
  6. #                results.
  7. #
  8. #       eg:
  9. #               gcalc "1 hour 5 minutes in seconds"
  10. #               1 hour 5 minutes = 3900 seconds
  11. #
  12. #               gcalc "100 + 2 * 5"
  13. #               100 + (2 * 5) = 110
  14. #
  15. #               gcalc "41000 yen in british pounds"
  16. #               41,000 Japanese yen = 277.129372 British pounds
  17. #
  18. #
  19. #
  20. # Published under the GNU Public License: http://www.gnu.org/copyleft/gpl.html
  21. #
  22. # $Id: $
  23.  
  24. from httplib import HTTPConnection
  25. import sys, re, urllib, htmlentitydefs
  26.  
  27.  
  28. ##############################################################################
  29. # HTTP Constants
  30.  
  31. GoogleServer = 'www.google.co.uk'
  32. RequestBase  = '/search?num=1&q='
  33. # You get back simpler and smaller html if you pretend to be Lynx
  34. Headers   = { 'User-Agent' : 'Lynx/2.8.6rel.4 libwww-FM/2.14' }
  35.  
  36. ##############################################################################
  37. # Regular Expressions
  38.  
  39. CoarseMatchEx = re.compile ( '.*calc_img\.gif(?P<match>.*)<\/h2>', re.DOTALL )
  40. FineMatchEx   = re.compile ( '.*<b>(?P<match>.*)</b>' )
  41.  
  42. PostProcessExs = [ ( re.compile ( pattern ), subst ) for pattern, subst in ( ( '<sup>', 'E' ), ( '<[^>]*>', '' ) ) ]
  43.  
  44. ##############################################################################
  45. # Script
  46.  
  47. # Construct the request string (the path component of the HTTP request)
  48. calculation = ' '.join ( sys.argv [ 1 : ] ).strip ( )
  49. if not calculation:
  50.         sys.exit ( 'Must provide something to calculate!' )
  51. requeststring = RequestBase + urllib.quote_plus ( calculation )
  52.  
  53. # Connect to the Google server, make the request and retrieve the response
  54. try:
  55.         connection = HTTPConnection ( GoogleServer )
  56.         connection.request ( method = 'GET', url = requeststring, headers = Headers  )
  57.         response = connection.getresponse ( ).read ( ).strip ( )
  58. except Exception, exception:
  59.         sys.exit ( 'Unable to make request "http://%s/%s" - %s' % ( GoogleServer, requeststring, exception ) )
  60.  
  61. # Process the response and display the Google Calculator response, if any
  62. coarsematch = CoarseMatchEx.match ( response )
  63. if coarsematch:
  64.         finematch = FineMatchEx.match ( coarsematch.group ( 'match' ) )
  65.         if finematch:
  66.                 value = finematch.group ( 'match' )
  67.                 # Pass the value through the post processing regular expressions
  68.                 for regex, subst in PostProcessExs: value = regex.sub ( subst, value )
  69.                 # Expand any unicode values to ASCII
  70.                 # &#215; The multiplication symbol seems to be missing from the Python entity list, so I account for that:
  71.                 if '&#215;' in value:
  72.                         value = value.replace ( '&#215;', 'x' )
  73.                 for unicode in htmlentitydefs.codepoint2name:
  74.                         if '&#%d;' % unicode in value:
  75.                                 entity = htmlentitydefs.codepoint2name [ unicode ]
  76.                                 if entity in htmlentitydefs.entitydefs:
  77.                                         value = value.replace ( '&#%d;' % unicode, htmlentitydefs.entitydefs [ entity ] )
  78.                                 else:
  79.                                         value = value.replace ( '&#%d;' % unicode, '' )
  80.  
  81.                 # Expand any entity definitions to ASCII
  82.                 for entity in htmlentitydefs.entitydefs:
  83.                         if '&%s;' % entity in value:
  84.                                 value = value.replace ( '&%s;' % entity, htmlentitydefs.entitydefs [ entity ] )
  85.                 # strip &nbsp;'s that appear in value as raw char 160's, hex A0
  86.                 # These only seem to be used a thousands separators, so I'll be UK centric and replace with a comma but a space would work too.
  87.                 value = value.replace('\xA0', ',')
  88.                 # Done!
  89.                 print value
  90.         else:
  91.                 sys.exit ( 'Warning - Google appears to have changed its page layout, update Fine Match expression' )
  92. else:
  93.         print 'Google Calculator did not understand : %s' % calculation
  94.         #output = open('/tmp/gcalc_response.html', 'w')
  95.         #output.write(response)
  96.         #output.close()
  97.         #print coarsematch
  98.         #print finematch
  99.         #print requeststring