-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans_api.go
59 lines (51 loc) · 1.45 KB
/
trans_api.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
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/yrshiben/baidu-translation-workflow/model"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
const transApiHost string = "https://fanyi-api.baidu.com/api/trans/vip/translate"
type TransApi struct {
AppId string
SecurityKey string
}
func NewTransApi(appId string, securityKey string) *TransApi {
return &TransApi{AppId: appId, SecurityKey: securityKey}
}
// from/to: en or zh
func (api *TransApi) GetTransResult(query, from, to string) (*model.Result, error) {
params := api.buildParams(query, from, to)
return get(params)
}
func (api *TransApi) buildParams(query, from, to string) string {
salt := strconv.FormatInt(time.Now().Unix(), 10)
sign := Md5(api.AppId + query + salt + api.SecurityKey)
// ?appid=appid&salt=salt&from=from&to=to&sign=sign&q=query
queryParams := "?appid=" + api.AppId + "&salt=" + salt + "&from=" + from + "&to=" + to + "&sign=" + sign + "&q=" + url.PathEscape(query)
return queryParams
}
func Md5(str string) string {
data := []byte(str)
has := md5.Sum(data)
return fmt.Sprintf("%x", has)
}
func get(query string) (*model.Result, error) {
var queryResult *model.Result
resp, err := http.Get(transApiHost + query)
if err != nil {
return queryResult, err
}
defer resp.Body.Close()
if result, err := ioutil.ReadAll(resp.Body); err != nil {
return queryResult, err
} else {
err = json.Unmarshal(result, &queryResult)
return queryResult, err
}
}