Updated the Google Translate plugin

The Google Translate API v1 has been stopped since December 1, 2011. From now on, the API is a paid service only.
The plugin has been updated to support v2 and grab the (mandatory) API key from the bot config file.
This commit is contained in:
stoneLeaf 2012-04-12 16:11:52 +02:00
parent b939a94be7
commit 4770c8c227
1 changed files with 28 additions and 7 deletions

View File

@ -1,8 +1,15 @@
'''
A Google API key is required and retrieved from the bot config file.
Since December 1, 2011, the Google Translate API is a paid service only.
'''
import htmlentitydefs import htmlentitydefs
import re import re
from util import hook, http from util import hook, http
api_key = ""
########### from http://effbot.org/zone/re-sub.htm#unescape-html ############# ########### from http://effbot.org/zone/re-sub.htm#unescape-html #############
@ -32,15 +39,15 @@ def unescape(text):
def goog_trans(text, slang, tlang): def goog_trans(text, slang, tlang):
url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0' url = 'https://www.googleapis.com/language/translate/v2'
parsed = http.get_json(url, q=text, langpair=(slang + '|' + tlang)) parsed = http.get_json(url, key=api_key, q=text, source=slang, target=tlang)
if not 200 <= parsed['responseStatus'] < 300: if not 200 <= parsed['responseStatus'] < 300:
raise IOError('error with the translation server: %d: %s' % ( raise IOError('error with the translation server: %d: %s' % (
parsed['responseStatus'], parsed['responseDetails'])) parsed['responseStatus'], parsed['responseDetails']))
if not slang: if not slang:
return unescape('(%(detectedSourceLanguage)s) %(translatedText)s' % return unescape('(%(detectedSourceLanguage)s) %(translatedText)s' %
(parsed['responseData'])) (parsed['responseData']['data']['translations'][0]))
return unescape(parsed['responseData']['translatedText']) return unescape('%(translatedText)s' % parsed['responseData']['data']['translations'][0])
def match_language(fragment): def match_language(fragment):
@ -57,11 +64,14 @@ def match_language(fragment):
@hook.command @hook.command
def translate(inp): def translate(inp, bot=None):
'.translate [source language [target language]] <sentence> -- translates' \ '.translate [source language [target language]] <sentence> -- translates' \
' <sentence> from source language (default autodetect) to target' \ ' <sentence> from source language (default autodetect) to target' \
' language (default English) using Google Translate' ' language (default English) using Google Translate'
if not hasapikey(bot):
return None
args = inp.split(' ', 2) args = inp.split(' ', 2)
try: try:
@ -69,6 +79,8 @@ def translate(inp):
sl = match_language(args[0]) sl = match_language(args[0])
if not sl: if not sl:
return goog_trans(inp, '', 'en') return goog_trans(inp, '', 'en')
if len(args) == 2:
return goog_trans(args[1], sl, 'en')
if len(args) >= 3: if len(args) >= 3:
tl = match_language(args[1]) tl = match_language(args[1])
if not tl: if not tl:
@ -94,9 +106,12 @@ def babel_gen(inp):
@hook.command @hook.command
def babel(inp): def babel(inp, bot=None):
".babel <sentence> -- translates <sentence> through multiple languages" ".babel <sentence> -- translates <sentence> through multiple languages"
if not hasapikey(bot):
return None
try: try:
return list(babel_gen(inp))[-1][2] return list(babel_gen(inp))[-1][2]
except IOError, e: except IOError, e:
@ -104,9 +119,12 @@ def babel(inp):
@hook.command @hook.command
def babelext(inp): def babelext(inp, bot=None):
".babelext <sentence> -- like .babel, but with more detailed output" ".babelext <sentence> -- like .babel, but with more detailed output"
if not hasapikey(bot):
return None
try: try:
babels = list(babel_gen(inp)) babels = list(babel_gen(inp))
except IOError, e: except IOError, e:
@ -123,6 +141,9 @@ def babelext(inp):
return out return out
def hasapikey(bot):
api_key = bot.config.get("api_keys", {}).get("googletranslate", None)
return api_key
lang_pairs = [ lang_pairs = [
("no", "Norwegian"), ("no", "Norwegian"),