diff options
author | Sean B. Palmer <http://inamidst.com/sbp/> | 2008-05-31 11:46:48 +0100 |
---|---|---|
committer | Sean B. Palmer <http://inamidst.com/sbp/> | 2008-05-31 11:46:48 +0100 |
commit | 68f2036546c286bb864dbc2ade251041bf7e6be9 (patch) | |
tree | 739dab7e082b9187674a2cd2ee10afd7c86eae9c /modules | |
parent | 87b9866c7c2ebeaa3c1c010176fa3bc24e017300 (diff) | |
download | bot-68f2036546c286bb864dbc2ade251041bf7e6be9.tar.gz bot-68f2036546c286bb864dbc2ade251041bf7e6be9.tar.bz2 bot-68f2036546c286bb864dbc2ade251041bf7e6be9.zip |
Fix for Windows, and some more helpful error messages.
Diffstat (limited to 'modules')
-rwxr-xr-x | modules/translate.py | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/modules/translate.py b/modules/translate.py index cf960aa..06efa62 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -37,13 +37,8 @@ def translate(text, input, output): pair = input + '%7C' + output bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair) result = json(bytes) - try: msg = result['responseData']['translatedText'] - except Exception: - msg = 'The %s to %s translation failed, sorry!' % (input, output) - else: - msg = msg.encode('cp1252').replace(''', "'") - msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output) - return msg + try: return result['responseData']['translatedText'].encode('cp1252') + except Exception: return None def tr(phenny, context): """Translates a phrase, with an optional language hint.""" @@ -63,6 +58,11 @@ def tr(phenny, context): if input != output: msg = translate(phrase, input, output) + if msg: + 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) + phenny.reply(msg) else: phenny.reply('Ehwhatnow?') @@ -70,5 +70,25 @@ tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$' tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?' tr.priority = 'low' +def mangle(phenny, input): + phrase = input.group(2).encode('utf-8') + for lang in ['fr', 'de', 'es', 'it', 'ja']: + backup = phrase + phrase = translate(phrase, 'en', lang) + if not phrase: + phrase = backup + break + __import__('time').sleep(0.5) + + backup = phrase + phrase = translate(phrase, lang, 'en') + if not phrase: + phrase = backup + break + __import__('time').sleep(0.5) + + phenny.reply(phrase or 'ERRORS SRY') +mangle.commands = ['mangle'] + if __name__ == '__main__': print __doc__.strip() |