mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-23 19:22:45 +01:00
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:
parent
33048545ea
commit
b2fdf83496
@ -236,50 +236,41 @@ class Google(callbacks.PluginRegexp):
|
|||||||
s = ', '.join([format('%s: %i', bold(s), i) for (i, s) in results])
|
s = ', '.join([format('%s: %i', bold(s), i) for (i, s) in results])
|
||||||
irc.reply(s)
|
irc.reply(s)
|
||||||
|
|
||||||
_gtranslateUrl='http://ajax.googleapis.com/ajax/services/language/translate'
|
@internationalizeDocstring
|
||||||
def translate(self, irc, msg, args, fromLang, toLang, text):
|
def translate(self, irc, msg, args, sourceLang, targetLang, text):
|
||||||
"""<from-language> [to] <to-language> <text>
|
"""<source language> [to] <target language> <text>
|
||||||
|
|
||||||
Returns <text> translated from <from-language> into <to-language>.
|
Returns <text> translated from <source language> into <target
|
||||||
Beware that translating to or from languages that use multi-byte
|
language>.
|
||||||
characters may result in some very odd results.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
channel = msg.args[0]
|
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 = utils.web.defaultHeaders
|
||||||
headers['Referer'] = ref
|
headers['User-Agent'] = ('Mozilla/5.0 (X11; U; Linux i686) '
|
||||||
opts = {'q': text, 'v': '1.0'}
|
'Gecko/20071127 Firefox/2.0.0.11')
|
||||||
lang = conf.supybot.plugins.Google.defaultLanguage
|
|
||||||
if fromLang.capitalize() in lang.transLangs:
|
sourceLang = urllib.quote(sourceLang)
|
||||||
fromLang = lang.transLangs[fromLang.capitalize()]
|
targetLang = urllib.quote(targetLang)
|
||||||
elif lang.normalize('lang_'+fromLang)[5:] \
|
|
||||||
not in lang.transLangs.values():
|
text = urllib.quote(text)
|
||||||
irc.errorInvalid('from language', fromLang,
|
|
||||||
format('Valid languages are: %L',
|
result = utils.web.getUrlFd('http://translate.google.com/translate_a/t'
|
||||||
lang.transLangs.keys()))
|
'?client=t&hl=en&sl=%s&tl=%s&multires=1'
|
||||||
else:
|
'&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text='
|
||||||
fromLang = lang.normalize('lang_'+fromLang)[5:]
|
'%s' % (sourceLang, targetLang, text),
|
||||||
if toLang.capitalize() in lang.transLangs:
|
headers).read()
|
||||||
toLang = lang.transLangs[toLang.capitalize()]
|
|
||||||
elif lang.normalize('lang_'+toLang)[5:] \
|
while ',,' in result:
|
||||||
not in lang.transLangs.values():
|
result = result.replace(',,', ',null,')
|
||||||
irc.errorInvalid('to language', toLang,
|
data = json.loads(result)
|
||||||
format('Valid languages are: %L',
|
|
||||||
lang.transLangs.keys()))
|
try:
|
||||||
else:
|
language = data[2]
|
||||||
toLang = lang.normalize('lang_'+toLang)[5:]
|
except:
|
||||||
opts['langpair'] = '%s|%s' % (fromLang, toLang)
|
language = 'unknown'
|
||||||
fd = utils.web.getUrlFd('%s?%s' % (self._gtranslateUrl,
|
|
||||||
urllib.urlencode(opts)),
|
irc.reply(''.join(x[0] for x in data[0]), language)
|
||||||
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'))
|
|
||||||
translate = wrap(translate, ['something', 'to', 'something', 'text'])
|
translate = wrap(translate, ['something', 'to', 'something', 'text'])
|
||||||
|
|
||||||
def googleSnarfer(self, irc, msg, match):
|
def googleSnarfer(self, irc, msg, match):
|
||||||
|
Loading…
Reference in New Issue
Block a user