diff options
author | Sean B. Palmer <sbp@aldebaran.local> | 2010-11-13 11:55:04 +0000 |
---|---|---|
committer | Sean B. Palmer <sbp@aldebaran.local> | 2010-11-13 11:55:04 +0000 |
commit | 5ebe01386d46f0a3a50b2b02416d8e7ee222af7e (patch) | |
tree | 7128595c9a945d507f47d8177d8871ddc88169dc /modules/translate.py | |
parent | ca95157acbe6e340573130e45a851b39e547159d (diff) | |
download | bot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.tar.gz bot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.tar.bz2 bot-5ebe01386d46f0a3a50b2b02416d8e7ee222af7e.zip |
Services fixes, and a standard JSON API.
Diffstat (limited to 'modules/translate.py')
-rwxr-xr-x | modules/translate.py | 18 |
1 files changed, 3 insertions, 15 deletions
diff --git a/modules/translate.py b/modules/translate.py index baa430d..e637d01 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -11,23 +11,11 @@ http://inamidst.com/phenny/ import re, urllib import web -r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$') -r_string = re.compile(r'("(\\.|[^"\\])*")') -env = {'__builtins__': None, 'null': None, - 'true': True, 'false': False} - -def json(text): - """Evaluate JSON text safely (we hope).""" - if r_json.match(r_string.sub('', text)): - text = r_string.sub(lambda m: 'u' + m.group(1), text) - return eval(text.strip(' \t\r\n'), env, {}) - raise ValueError('Input must be serialised JSON.') - def detect(text): uri = 'http://ajax.googleapis.com/ajax/services/language/detect' q = urllib.quote(text) bytes = web.get(uri + '?q=' + q + '&v=1.0') - result = json(bytes) + result = web.json(bytes) try: return result['responseData']['language'] except Exception: return None @@ -36,7 +24,7 @@ def translate(text, input, output): q = urllib.quote(text) pair = input + '%7C' + output bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair) - result = json(bytes) + result = web.json(bytes) try: return result['responseData']['translatedText'].encode('cp1252') except Exception: return None @@ -59,7 +47,7 @@ def tr(phenny, context): if input != output: msg = translate(phrase, input, output) if msg: - msg = msg.replace(''', "'") + msg = web.decode(msg) # msg.replace(''', "'") msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) else: msg = 'The %s to %s translation failed, sorry!' % (input, output) |