-
Notifications
You must be signed in to change notification settings - Fork 56
/
api.go
90 lines (74 loc) · 1.88 KB
/
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
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
package gtranslate
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
"golang.org/x/text/language"
)
const gt = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dj=1&dt=t&ie=UTF-8&q=%s"
func translateOld(text, from, to string, withVerification bool) (string, error) {
if withVerification {
if _, err := language.Parse(from); err != nil && from != "auto" {
log.Println("[WARNING], '" + from + "' is a invalid language, switching to 'auto'")
from = "auto"
}
if _, err := language.Parse(to); err != nil {
log.Println("[WARNING], '" + to + "' is a invalid language, switching to 'en'")
to = "en"
}
}
raw, err := rawTranslate(text, from, to)
if err != nil {
return "", err
}
resp := new(struct {
Sentences []struct {
Trans string `json:"trans"`
Orig string `json:"orig"`
Backend int `json:"backend"`
} `json:"sentences"`
Src string `json:"src"`
Spell struct {
} `json:"spell"`
})
err = json.Unmarshal(raw, resp)
if err != nil {
return "", err
}
responseText := ""
responseText = resp.Sentences[0].Trans
return responseText, nil
}
func rawTranslate(text, from, to string) ([]byte, error) {
r, err := getGoogleTranslate(text, from, to)
if err != nil {
if err == http.ErrHandlerTimeout {
return nil, errBadNetwork
}
return nil, err
}
if r.StatusCode != http.StatusOK {
// oh noooo
return nil, errBadRequest
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
return data, nil
}
func getGoogleTranslate(text, from, to string, customClient ...*http.Client) (*http.Response, error) {
client := http.DefaultClient
if len(customClient) != 0 {
client = customClient[0]
log.Println("[WARNING] Using custom proxy")
}
client.Timeout = 40 * time.Second
text = url.PathEscape(text)
uri := fmt.Sprintf(gt, from, to, text)
return client.Get(uri)
}