diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index c2364d25a..9f34c6dbc 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -236,6 +236,52 @@ 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): + """ [to] + + Returns translated from into . + Beware that translating to or from languages that use multi-byte + characters may result in some very odd results. + """ + 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')) + translate = wrap(translate, ['something', 'to', 'something', 'text']) + def googleSnarfer(self, irc, msg, match): r"^google\s+(.*)$" if not self.registryValue('searchSnarfer', msg.args[0]): diff --git a/plugins/Google/test.py b/plugins/Google/test.py index 1aa1830ce..788e4e844 100644 --- a/plugins/Google/test.py +++ b/plugins/Google/test.py @@ -54,4 +54,10 @@ class GoogleTestCase(ChannelPluginTestCase): self.assertRegexp('fight supybot moobot', r'.*supybot.*: \d+') self.assertNotError('fight ... !') + def testTranslate(self): + self.assertRegexp('translate en es hello world', 'mundo') + + def testCalcDoesNotHaveExtraSpaces(self): + self.assertNotRegexp('google calc 1000^2', r'\s+,\s+') + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: