This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
microsoft.go
380 lines (322 loc) · 9.1 KB
/
microsoft.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package microsoft
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/nuveo/utils/cache"
)
const (
datamarket = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
scope = "http://api.microsofttranslator.com"
translateURL = "http://api.microsofttranslator.com/v2/Http.svc/Translate"
translateArrayURL = "http://api.microsofttranslator.com/V2/Http.svc/TranslateArray"
detectArrayURL = "http://api.microsofttranslator.com/V2/Http.svc/DetectArray"
grantType = "client_credentials"
xmlArrayTemplate = `<TranslateArrayRequest>
<AppId />
<From>%s</From>
<Options>
<Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" ></Category>
<ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType>
<ReservedFlags xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"></State>
<Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"></Uri>
<User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2"></User>
</Options>
<Texts>
%s
</Texts>
<To>%s</To>
</TranslateArrayRequest>`
templateToTranslate = `<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">%s</string>`
xmlDetectArrayTemplate = `<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">%s</ArrayOfstring>`
)
type Access interface {
GetAccessToken() TokenResponse
}
type Translator interface {
Translate() (string, error)
TranslateArray() ([]string, error)
DetectTextArray() ([]string, error)
CheckTimeout() bool
}
type AuthRequest struct {
ClientID string
ClientSecret string
}
type TextTranslate struct {
Text string
Texts []string
From string
To string
Cache bool
TokenResponse
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn string `json:"expires_in"`
Scope string `json:"scope"`
Timeout time.Time `json:"timeout"`
}
type TranslateResponse struct {
Resp []ArrayResp `xml:"TranslateArrayResponse"`
}
type ArrayResp struct {
Text string `xml:"TranslatedText"`
}
func GetAccessToken(access Access) TokenResponse {
return access.GetAccessToken()
}
func TranslateText(t Translator) (string, error) {
if t.CheckTimeout() == false {
return t.Translate()
}
return "", errors.New("Access token is invalid, please get new token")
}
func TranslateTexts(t Translator) ([]string, error) {
if t.CheckTimeout() == false {
return t.TranslateArray()
}
return []string{}, errors.New("Access token is invalid, please get new token")
}
func DetectText(t Translator) ([]string, error) {
if t.CheckTimeout() == false {
return t.DetectTextArray()
}
return []string{}, errors.New("Access token is invalid, please get new token")
}
func rdbCache() *redis.Redis {
redisUri := os.Getenv("REDIS")
if redisUri == "" {
redisUri = ":6739"
}
conn := redis.Connection{"tcp", redisUri, "7"}
rdb, err := conn.Dial()
if err != nil {
log.Println("Fail to connect: ", err)
}
return rdb
}
// Make a POST request to `datamark` url for getting access token
func (a *AuthRequest) GetAccessToken() TokenResponse {
client := &http.Client{}
postValues := url.Values{}
postValues.Add("client_id", a.ClientID)
postValues.Add("client_secret", a.ClientSecret)
postValues.Add("scope", scope)
postValues.Add("grant_type", grantType)
req, err := http.NewRequest("POST", datamarket, strings.NewReader(postValues.Encode()))
if err != nil {
log.Println(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
var tr TokenResponse
err = json.Unmarshal(body, &tr)
if err != nil {
log.Println(err)
}
now := time.Now()
expiresIn, err := strconv.ParseInt(tr.ExpiresIn, 10, 0)
if err != nil {
log.Println(err)
}
expTime := now.Add(time.Duration(expiresIn) * time.Second)
// 10 min
tr.Timeout = expTime
return tr
}
// Return `t.Text` in `t.From` language translated for `t.To` language
func (t *TextTranslate) Translate() (string, error) {
if t.Cache == true {
rdb := rdbCache()
exists, _ := rdb.HExists(t.Text, t.To)
if exists == true {
result, _ := rdb.HGet(t.Text, t.To)
log.Printf("Getting from cache %s:%s", t.Text, result)
rdb.Conn.Close()
return result, nil
}
}
textEncode := url.Values{}
textEncode.Add("text", t.Text)
text := textEncode.Encode()
url := fmt.Sprintf("%s?%s&from=%s&to=%s", translateURL, text, t.From, t.To)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println(err)
}
authToken := fmt.Sprintf("Bearer %s", t.TokenResponse.AccessToken)
req.Header.Add("Authorization", authToken)
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
type Text struct {
T string `xml:",chardata"`
}
var obj Text
err = xml.Unmarshal(body, &obj)
if t.Cache == true {
rdb := rdbCache()
rdb.HSet(t.Text, t.To, obj.T)
log.Printf("Add to cache %s:[%s] -> %s", t.Text, t.To, obj.T)
rdb.Conn.Close()
}
return obj.T, nil
}
// Return `t.Texts` array in `t.From` language translated for `t.To` language
func (t *TextTranslate) TranslateArray() ([]string, error) {
toTranslate := make([]string, len(t.Texts))
response := []string{}
// Simulate possible indexes of array response from microsoft.
notCached := make(map[string]int)
count := 0
if t.Cache == true {
rdb := rdbCache()
for _, tx := range t.Texts {
exs, _ := rdb.HExists(tx, t.To)
if exs == false {
notCached[tx] = count
ts := fmt.Sprintf(templateToTranslate, tx)
toTranslate = append(toTranslate, ts)
count++
}
}
rdb.Conn.Close()
} else {
for _, text := range t.Texts {
tx := fmt.Sprintf(templateToTranslate, text)
toTranslate = append(toTranslate, tx)
}
}
// If do not need to do translation
if len(notCached) == 0 {
rdb := rdbCache()
for _, tx := range t.Texts {
res, _ := rdb.HGet(tx, t.To)
log.Printf("Get from cache %s: [%s] %s", tx, t.To, res)
if res != "" {
response = append(response, res)
}
}
rdb.Conn.Close()
return response, nil
}
textToTranslate := strings.Join(toTranslate, "\n")
bodyReq := fmt.Sprintf(xmlArrayTemplate, t.From, textToTranslate, t.To)
client := &http.Client{}
req, err := http.NewRequest("POST", translateArrayURL, strings.NewReader(bodyReq))
if err != nil {
log.Println(err)
}
authToken := fmt.Sprintf("Bearer %s", t.TokenResponse.AccessToken)
req.Header.Add("Authorization", authToken)
req.Header.Add("Content-Type", "text/xml")
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
var obj TranslateResponse
err = xml.Unmarshal(body, &obj)
if err != nil {
log.Println(err)
}
if t.Cache == true && len(obj.Resp) > 0 {
rdb := rdbCache()
for key, indx := range notCached {
tx := obj.Resp[indx].Text
log.Printf("Add to cache %s: [%s] %s", key, t.To, tx)
err = rdb.HSet(key, t.To, tx)
if err != nil {
log.Println(err)
}
}
for _, tx := range t.Texts {
res, _ := rdb.HGet(tx, t.To)
log.Printf("Get from cache %s: [%s] %s", tx, t.To, res)
if res != "" {
response = append(response, res)
}
}
rdb.Conn.Close()
}
return response, nil
}
// Detects the language of the text passed in the array `t.Texts`
func (t *TextTranslate) DetectTextArray() ([]string, error) {
response := []string{}
toTranslate := make([]string, len(t.Texts))
for _, text := range t.Texts {
textEncode := url.Values{}
textEncode.Add("text", text)
text := textEncode.Encode()
tx := fmt.Sprintf(templateToTranslate, text)
toTranslate = append(toTranslate, tx)
}
textToTranslate := strings.Join(toTranslate, "\n")
bodyReq := fmt.Sprintf(xmlDetectArrayTemplate, textToTranslate)
client := &http.Client{}
req, err := http.NewRequest("POST", detectArrayURL, strings.NewReader(bodyReq))
if err != nil {
log.Println(err)
}
authToken := fmt.Sprintf("Bearer %s", t.TokenResponse.AccessToken)
req.Header.Add("Authorization", authToken)
req.Header.Add("Content-Type", "text/xml")
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
type LangArray struct {
Array []string `xml:"string"`
}
var obj LangArray
err = xml.Unmarshal(body, &obj)
if err != nil {
log.Println(err)
}
response = append(response, obj.Array...)
return response, nil
}
// Verify if access token is valid
func (t *TokenResponse) CheckTimeout() bool {
if time.Since(t.Timeout) > 0 {
return true
}
return false
}