babel plugin
This commit is contained in:
parent
fb83e4f248
commit
852bebe58c
4
bot.py
4
bot.py
|
@ -123,7 +123,11 @@ while True:
|
|||
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
|
||||
input = Input(*out)
|
||||
for fsig, sieve in bot.plugs['sieve']:
|
||||
try:
|
||||
input = sieve(bot, input, func, args)
|
||||
except Exception, e:
|
||||
print 'filter error:', e
|
||||
input = None
|
||||
if input == None:
|
||||
break
|
||||
if input == None:
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
import yaml
|
||||
import urllib
|
||||
|
||||
import hook
|
||||
|
||||
languages = 'en ja de he es ko ru en zh en'.split();
|
||||
language_pairs = zip(languages[:-1], languages[1:])
|
||||
|
||||
def babel_gen(inp):
|
||||
req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \
|
||||
'?v=1.0&q=%s&langpair=%s'
|
||||
|
||||
yield 'en', inp
|
||||
for slang, tlang in language_pairs:
|
||||
inp = inp.encode('utf8')
|
||||
print slang, tlang, inp
|
||||
json = urllib.urlopen(req_url % (urllib.quote(inp, safe=''),
|
||||
slang + '%7C' + tlang)).read()
|
||||
parsed = yaml.load(json)
|
||||
if not 200 <= parsed['responseStatus'] < 300:
|
||||
raise IOError, 'error with the translation server'
|
||||
inp = parsed['responseData']['translatedText']
|
||||
yield tlang, inp
|
||||
|
||||
@hook.command
|
||||
def babel(inp):
|
||||
try:
|
||||
return list(babel_gen(inp))[-1][1]
|
||||
except IOError:
|
||||
return 'error with the translation server'
|
||||
|
||||
@hook.command
|
||||
def babelexp(inp):
|
||||
try:
|
||||
babels = list(babel_gen(inp))
|
||||
except IOError:
|
||||
return 'error with the translation server'
|
||||
|
||||
out = u''
|
||||
for lang, text in babels:
|
||||
out += '%s:"%s", ' % (lang, text.decode('utf8'))
|
||||
|
||||
if len(out) > 300:
|
||||
out = out[:150] + ' ... ' + out[-150:]
|
||||
|
||||
return out[:-2]
|
|
@ -25,7 +25,7 @@ def command(func=None, hook=None, **kwargs):
|
|||
raise ValueError, \
|
||||
'commands must take 1 or 2 arguments: (inp) or (bot, input)'
|
||||
args.setdefault('name', func.func_name)
|
||||
args.setdefault('hook', args['name'] + r'\s*(.*)')
|
||||
args.setdefault('hook', args['name'] + r'(?:\s+|$)(.*)')
|
||||
_hook_add(func, ['command', (_make_sig(func), func, args)])
|
||||
return func
|
||||
|
||||
|
|
Loading…
Reference in New Issue