From 3724ba37cd02a2bf57012839f84c627eb5df65a2 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Tue, 27 Dec 2011 14:48:24 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Fixed=20translation=20module,=20with=20the?= =?UTF-8?q?=20help=20of=20der=20H=C3=B6rmi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/translate.py | 45 ++++++++++++++++++++++++-------------------- project | 2 +- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/modules/translate.py b/modules/translate.py index d5ae41f..209e5bd 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -11,22 +11,30 @@ http://inamidst.com/phenny/ import re, urllib import web -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 = web.json(bytes) - try: return result['responseData']['language'] - except Exception: return None +def translate(text, input='auto', output='en'): + import urllib2, json + opener = urllib2.build_opener() + opener.addheaders = [( + 'User-Agent', 'Mozilla/5.0' + + '(X11; U; Linux i686)' + + 'Gecko/20071127 Firefox/2.0.0.11' + )] -def translate(text, input, output): - uri = 'http://ajax.googleapis.com/ajax/services/language/translate' - q = urllib.quote(text) - pair = input + '%7C' + output - bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair) - result = web.json(bytes) - try: return result['responseData']['translatedText'].encode('cp1252') - except Exception: return None + input, output = urllib.quote(input), urllib.quote(output) + text = urllib.quote(text) + + result = opener.open('http://translate.google.com/translate_a/t?' + + ('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) + + ('&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text=%s' % text)).read() + + while ',,' in result: + result = result.replace(',,', ',null,') + data = json.loads(result) + + try: language = data[-2][0][0] + except: language = '?' + + return ''.join(x[0] for x in data[0]), language def tr(phenny, context): """Translates a phrase, with an optional language hint.""" @@ -37,15 +45,12 @@ def tr(phenny, context): if (len(phrase) > 350) and (not context.admin): return phenny.reply('Phrase must be under 350 characters.') - input = input or detect(phrase) - if not input: - err = 'Unable to guess your crazy moon language, sorry.' - return phenny.reply(err) + input = input or 'auto' input = input.encode('utf-8') output = (output or 'en').encode('utf-8') if input != output: - msg = translate(phrase, input, output) + msg, input = translate2(phrase, input, output) if isinstance(msg, str): msg = msg.decode('utf-8') if msg: diff --git a/project b/project index 21ff142..47b52fd 100755 --- a/project +++ b/project @@ -13,7 +13,7 @@ function commit() { git commit -a && git push origin master } -# log - Show a log of recent updates +# history - Show a log of recent updates function history() { git log --pretty=oneline --no-merges -10 } From b2fe5b31e271f711cda6f2d730a51abba5244131 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Tue, 27 Dec 2011 14:50:21 +0000 Subject: [PATCH 2/2] Typo2 --- modules/translate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/translate.py b/modules/translate.py index 209e5bd..968cd63 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -50,7 +50,7 @@ def tr(phenny, context): output = (output or 'en').encode('utf-8') if input != output: - msg, input = translate2(phrase, input, output) + msg, input = translate(phrase, input, output) if isinstance(msg, str): msg = msg.decode('utf-8') if msg: