Skip to content

Commit

Permalink
Fixed google calculator using a different method
Browse files Browse the repository at this point in the history
  • Loading branch information
BirdAPI committed Apr 11, 2012
1 parent 517f245 commit cabf4fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -174,7 +174,7 @@ print "dollars -> pesos exchange rate = {0}".format(rate)
dollars -> pesos exchange rate = 13.1580679 dollars -> pesos exchange rate = 13.1580679
``` ```


Perform your own math. The following 2 statements equal: Perform your own math. The following 2 statements are equal:


```python ```python
5.0 * Google.exchange_rate("USD", "EUR") 5.0 * Google.exchange_rate("USD", "EUR")
Expand Down
27 changes: 25 additions & 2 deletions google.py
Expand Up @@ -9,9 +9,13 @@
import urllib2 import urllib2
import sys import sys
import re import re
try:
import json
except ImportError:
import simplejson as json


__author__ = "Anthony Casagrande <birdapi@gmail.com>" __author__ = "Anthony Casagrande <birdapi@gmail.com>"
__version__ = "0.8" __version__ = "0.9"


""" """
Represents a standard google search result Represents a standard google search result
Expand Down Expand Up @@ -136,7 +140,7 @@ def search(query, pages = 1):
Attempts to use google calculator to calculate the result of expr Attempts to use google calculator to calculate the result of expr
""" """
@staticmethod @staticmethod
def calculate(expr): def calculate_old(expr):
url = get_search_url(expr) url = get_search_url(expr)
html = get_html(url) html = get_html(url)
if html: if html:
Expand Down Expand Up @@ -264,6 +268,25 @@ def convert_currency(amount, from_currency, to_currency):
def exchange_rate(from_currency, to_currency): def exchange_rate(from_currency, to_currency):
return Google.convert_currency(1, from_currency, to_currency) return Google.convert_currency(1, from_currency, to_currency)


"""
Attempts to use google calculator to calculate the result of expr
"""
@staticmethod
def calculate(expr):
conn = httplib.HTTPSConnection("www.google.com")
req_url = "/ig/calculator?hl=en&q={0}".format(expr.replace(" ", "%20"))
headers = { "User-Agent": "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101" }
conn.request("GET", req_url, "", headers)
response = conn.getresponse()
j = response.read().decode("utf-8").replace(u"\xa0", "")
conn.close()
j = re.sub(r"{\s*'?(\w)", r'{"\1', j)
j = re.sub(r",\s*'?(\w)", r',"\1', j)
j = re.sub(r"(\w)'?\s*:", r'\1":', j)
j = re.sub(r":\s*'(\w)'\s*([,}])", r':"\1"\2', j)
js = json.loads(j)
return parse_calc_result(js["lhs"] + " = " + js["rhs"])

def normalize_query(query): def normalize_query(query):
return query.strip().replace(":", "%3A").replace("+", "%2B").replace("&", "%26").replace(" ", "+") return query.strip().replace(":", "%3A").replace("+", "%2B").replace("&", "%26").replace(" ", "+")


Expand Down

0 comments on commit cabf4fa

Please sign in to comment.