Google: Add translate command (needs testing)

https://github.com/myano/jenni/blob/master/modules/translate.py
is where I got the idea from. Say thanks to sbp and yano too! :)

Conflicts:
	plugins/Google/plugin.py
This commit is contained in:
nyuszika7h 2012-08-30 23:41:40 +02:00 committed by Valentin Lorentz
parent 33048545ea
commit b2fdf83496

View File

@ -236,50 +236,41 @@ class Google(callbacks.PluginRegexp):
s = ', '.join([format('%s: %i', bold(s), i) for (i, s) in results])
irc.reply(s)
_gtranslateUrl='http://ajax.googleapis.com/ajax/services/language/translate'
def translate(self, irc, msg, args, fromLang, toLang, text):
"""<from-language> [to] <to-language> <text>
@internationalizeDocstring
def translate(self, irc, msg, args, sourceLang, targetLang, text):
"""<source language> [to] <target language> <text>
Returns <text> translated from <from-language> into <to-language>.
Beware that translating to or from languages that use multi-byte
characters may result in some very odd results.
Returns <text> translated from <source language> into <target
language>.
"""
channel = msg.args[0]
ref = self.registryValue('referer')
if not ref:
ref = 'http://%s/%s' % (dynamic.irc.server,
dynamic.irc.nick)
headers = utils.web.defaultHeaders
headers['Referer'] = ref
opts = {'q': text, 'v': '1.0'}
lang = conf.supybot.plugins.Google.defaultLanguage
if fromLang.capitalize() in lang.transLangs:
fromLang = lang.transLangs[fromLang.capitalize()]
elif lang.normalize('lang_'+fromLang)[5:] \
not in lang.transLangs.values():
irc.errorInvalid('from language', fromLang,
format('Valid languages are: %L',
lang.transLangs.keys()))
else:
fromLang = lang.normalize('lang_'+fromLang)[5:]
if toLang.capitalize() in lang.transLangs:
toLang = lang.transLangs[toLang.capitalize()]
elif lang.normalize('lang_'+toLang)[5:] \
not in lang.transLangs.values():
irc.errorInvalid('to language', toLang,
format('Valid languages are: %L',
lang.transLangs.keys()))
else:
toLang = lang.normalize('lang_'+toLang)[5:]
opts['langpair'] = '%s|%s' % (fromLang, toLang)
fd = utils.web.getUrlFd('%s?%s' % (self._gtranslateUrl,
urllib.urlencode(opts)),
headers)
json = simplejson.load(fd)
fd.close()
if json['responseStatus'] != 200:
raise callbacks.Error, 'We broke The Google!'
irc.reply(json['responseData']['translatedText'].encode('utf-8'))
headers['User-Agent'] = ('Mozilla/5.0 (X11; U; Linux i686) '
'Gecko/20071127 Firefox/2.0.0.11')
sourceLang = urllib.quote(sourceLang)
targetLang = urllib.quote(targetLang)
text = urllib.quote(text)
result = utils.web.getUrlFd('http://translate.google.com/translate_a/t'
'?client=t&hl=en&sl=%s&tl=%s&multires=1'
'&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text='
'%s' % (sourceLang, targetLang, text),
headers).read()
while ',,' in result:
result = result.replace(',,', ',null,')
data = json.loads(result)
try:
language = data[2]
except:
language = 'unknown'
irc.reply(''.join(x[0] for x in data[0]), language)
translate = wrap(translate, ['something', 'to', 'something', 'text'])
def googleSnarfer(self, irc, msg, match):