From de61bb6b9396ef6e54de5a43400491b6295d60f7 Mon Sep 17 00:00:00 2001 From: nyuszika7h Date: Thu, 30 Aug 2012 23:41:40 +0200 Subject: [PATCH] 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! :) --- plugins/Google/plugin.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index a3e434079..695da8a96 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -233,6 +233,44 @@ class Google(callbacks.PluginRegexp): s = ', '.join([format('%s: %i', bold(s), i) for (i, s) in results]) irc.reply(s) + @internationalizeDocstring + def translate(self, irc, msg, args, sourceLang, targetLang, text): + """ [to] + + Returns translated from into . + """ + + channel = msg.args[0] + + headers = utils.web.defaultHeaders + 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): r"^google\s+(.*)$" if not self.registryValue('searchSnarfer', msg.args[0]):