-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoogle_translate.py
137 lines (113 loc) · 4.13 KB
/
google_translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from urllib2 import quote, HTTPError
from hashlib import sha224
from workflow import web, PasswordNotFound, KeychainError
MAX_AGE_CACHE = 60*60*24 # in sec.
API_URL = 'https://translation.googleapis.com/language/translate/v2'
QUICK_LOOK_URL = 'https://translate.google.com/'
ICON_PATH = 'icons'
PROPS = {
'API_KEY': 'google_translate_api_key',
'TARGET_LANG': 'google_target_lang',
}
DEFAULT_ICON = 'google-tr-icon.png'
class GoogleTranslate(object):
def __init__(self, wf, query=None, source_lang='#auto'):
self.wf = wf
self._query = query
self._source_lang = source_lang
def __get_translations(self):
""" Get translation from Google Translate API.
Returns:
A list of translation dictionaries.
Google Translate URL and arguments:
https://translation.googleapis.com/language/translate/v2?
key=API_KEY (required)
&source=<SOURCE_LANG> (optional)
&target=<TARGET_LANG> (required)
&q=<QUERY> (required)
"""
params = dict(key=self.api_key, target=self.target_lang, q=self.query)
r = web.get(API_URL, params)
try:
r.raise_for_status()
except HTTPError as e:
if e.code == 400:
raise RuntimeError('Please make sure that your Google '
'API key is correct /code 400: invalid request/.')
else:
raise e
# Parse the JSON returned and extract the translations.
result = r.json()
translations = result['data']['translations']
return translations
@property
def api_key(self):
try:
__api_key = self.wf.get_password(PROPS['API_KEY'])
return __api_key
except PasswordNotFound as e:
raise e
@api_key.setter
def api_key(self, value):
try:
self.wf.save_password(PROPS['API_KEY'], value)
except KeychainError as e:
raise e
@property
def target_lang(self):
return self.wf.settings.get(PROPS['TARGET_LANG'], None)
@target_lang.setter
def target_lang(self, value):
self.wf.settings[PROPS['TARGET_LANG']] = value
@property
def query(self):
return self._query
@query.setter
def query(self, value):
self._query = value
@property
def source_lang(self):
return self._source_lang
@source_lang.setter
def source_lang(self, value):
self._source_lang = value
def get_translations(self):
cache_key = sha224(('GOOGL' + self.target_lang + self.query)
.encode('utf-8')).hexdigest()
# Get from cache translations newer than MAX_AGE_CACHE
translations = self.wf.cached_data(cache_key,
self.__get_translations,
max_age=MAX_AGE_CACHE)
trans = []
for tr in translations:
trans.append({
'title': tr['translatedText'],
'subtitle': self.query + ' [google]',
'valid': True,
'arg': tr['translatedText'],
'copytext': tr['translatedText'],
'largetext': tr['translatedText'],
'quicklookurl': QUICK_LOOK_URL + self.source_lang
+ '/' + self.target_lang + '/'
+ quote(self.query.encode('utf-8')),
'icon': GoogleTranslate.get_icon(self.target_lang)})
return trans
@staticmethod
def get_icon(lang_code=None):
"""Get language icon.
Arguments:
lang_code: language code
Returns:
Language icon if it exists, else default icon.
"""
default_icon = os.path.join(ICON_PATH, DEFAULT_ICON)
if not lang_code:
return default_icon
icon_file = os.path.join(ICON_PATH, lang_code + '.png')
if os.path.exists(icon_file):
return icon_file
else:
return default_icon