diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-02-14 13:01:47 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-02-14 13:01:57 (GMT) |
commit | ed529b78d74e83d52846d4adff7981dc2febfa2e (patch) | |
tree | 2d8f757107e1c05e975a0f4a1e02d22b6178eef7 | |
parent | 73ec930524c1840568528fff31d5d26064fed538 (diff) |
Switch to Apertium
-rw-r--r-- | plugin.py | 29 |
1 files changed, 6 insertions, 23 deletions
@@ -29,6 +29,7 @@ import time import json import urllib import htmllib +import xmlrpclib from supybot.commands import * import supybot.irclib as irclib @@ -42,7 +43,7 @@ import supybot.conf as conf import config -_TRANSLATE_URL = 'http://ajax.googleapis.com/ajax/services/language/translate' +_TRANSLATE_URL = 'http://localhost:6173/RPC2' _QUOTES = "\"" _LANGUAGE_CODES = [i[0] for i in config.LANGUAGES] @@ -64,6 +65,7 @@ class Lingvo(callbacks.Plugin): self.lastStates = {} self._auto_parts = {} self._groups_value = None + self._proxy = xmlrpclib.ServerProxy(_TRANSLATE_URL) @property def _groups(self): @@ -445,22 +447,10 @@ class Lingvo(callbacks.Plugin): if not unquote(quoted_text, replaces, '').strip(): return text - url_opts = { - 'v': '1.0', - 'q': quoted_text, - 'langpair': '%s|%s' % (src_lang, dst_lang), - } - url = '%s?%s' % (_TRANSLATE_URL, urllib.urlencode(url_opts)) + reply = self._proxy.translate(quoted_text, src_lang, dst_lang) + quoted_text = reply.get('translation').encode('utf-8') or quoted_text - fd = utils.web.getUrlFd(url) - reply = json.load(fd) - fd.close() - - if reply['responseStatus'] != 200: - self.log.warn('Bad Google resonce for "%s"' % url) - else: - quoted_text = _unescape(reply['responseData']['translatedText']) - return unquote(quoted_text, replaces) + return unquote(quoted_text, replaces) def _relay_to(self, irc, channel, msg_format, text, src_lang, dst_lang, nicks=None): @@ -607,13 +597,6 @@ def unquote(text, replaces, stub=None): return text -def _unescape(text): - parser = htmllib.HTMLParser(None) - parser.save_bgn() - parser.feed(text.encode('utf-8')) - return parser.save_end() - - def _normalize_channel(name): match = re.match('(.*)-([-A-Za-z]+)$', name) |