[kupfer: 1/3] plugin.translate: update plugin after change google api
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer: 1/3] plugin.translate: update plugin after change google api
- Date: Wed, 18 Nov 2009 18:43:23 +0000 (UTC)
commit 4f5a20749d80cdb741a2ddc0afaac331ed21ae63
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date: Wed Nov 18 17:37:31 2009 +0100
plugin.translate: update plugin after change google api
kupfer/plugin/google_translate.py | 36 ++++++++++++++++++++++--------------
1 files changed, 22 insertions(+), 14 deletions(-)
---
diff --git a/kupfer/plugin/google_translate.py b/kupfer/plugin/google_translate.py
index a45e884..54a06cd 100644
--- a/kupfer/plugin/google_translate.py
+++ b/kupfer/plugin/google_translate.py
@@ -27,7 +27,7 @@ except ImportError:
_GOOGLE_TRANSLATE_HOST = 'translate.google.com'
_GOOGLE_TRANSLATE_PATH = '/translate_a/t?client=t&sl=auto&ie=UTF-8'
-_GOOGLE_TRANS_LANG_PATH = '/translate_t'
+_GOOGLE_TRANS_LANG_PATH = '/#'
_HEADER = {
'Content-type':'application/x-www-form-urlencoded',
@@ -61,15 +61,20 @@ def _translate(text, lang):
encoding = _parse_encoding_header(resp)
response_data = response_data.decode(encoding, 'replace')
resp = json_decoder(response_data)
- if len(resp) == 2:
- yield resp[0], ""
- elif len(resp) == 3:
- result, _lang, other_trans = resp
- yield result, ""
-
- for other in other_trans:
- for translation in other[1:]:
- yield translation, other[0]
+ if resp:
+ sentences = resp.get('sentences')
+ if not sentences:
+ return
+ translation = sentences[0].get('trans')
+ if not translation:
+ return
+ yield translation, ''
+ dictionary = resp.get('dict')
+ if dictionary:
+ for term in dictionary:
+ pos = term.get('pos')
+ for t in term.get('terms'):
+ yield t, pos
except (httplib.HTTPException, ValueError), err:
pretty.print_error(__name__, '_translate error', repr(text), lang, err)
@@ -79,6 +84,9 @@ def _translate(text, lang):
conn.close()
+_RE_GET_LANG_SELECT = re.compile(
+ r'\<select[\w\d\s="\']*name=tl[\w\d\s="\']*\>(.*)\<\/select\>',
+ re.UNICODE|re.MULTILINE|re.IGNORECASE)
_RE_GET_LANG = re.compile(r"""\<option[ \w]+ value="([\w\-]+)"\> # code 'zh-TW'
([^<]+?) # match localized lang name
\</option\>
@@ -102,10 +110,10 @@ def _load_languages():
resp.reason))
result = resp.read().decode(_parse_encoding_header(resp), "replace")
- result = result[result.index('select name=tl'):]
- result = result[:result.index("</select>")]
- for key, name in _RE_GET_LANG.findall(result):
- yield key, name
+ result = _RE_GET_LANG_SELECT.findall(result)
+ if result:
+ for key, name in _RE_GET_LANG.findall(result[0]):
+ yield key, name
except (httplib.HTTPException, ValueError), err:
pretty.print_error(__name__, '_load_languages error', type(err), err)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]