@@ -143,6 +143,19 @@ type result struct {
143
143
TransResult []transItem `json:"trans_result"`
144
144
}
145
145
146
+ var glastApiTime int64
147
+ var gFailedCnt int //翻译失败总数量
148
+
149
+ //GetFailedCnt GetFailedCnt
150
+ func GetFailedCnt () int {
151
+ return gFailedCnt
152
+ }
153
+
154
+ //ResetFailedCnt ResetFailedCnt
155
+ func ResetFailedCnt () {
156
+ gFailedCnt = 0
157
+ }
158
+
146
159
//Trans trans
147
160
func Trans (input * transer.TransInput ) * transer.TransOutput {
148
161
output := new (transer.TransOutput )
@@ -160,19 +173,25 @@ func Trans(input *transer.TransInput) *transer.TransOutput {
160
173
values ["appid" ] = []string {input .ID }
161
174
values ["salt" ] = []string {salt }
162
175
values ["sign" ] = []string {signStr }
176
+ crtTime := time .Now ().UnixNano ()
177
+ tmWait := 1e9 - (crtTime - glastApiTime )
178
+ if tmWait > 0 {
179
+ time .Sleep (time .Duration (tmWait )) //免费用户1秒只能请求一次
180
+ }
163
181
res , err := http .PostForm (baiduAPI , values )
164
182
if err != nil {
165
183
fmt .Println (err )
166
184
return output
167
185
}
168
186
defer res .Body .Close ()
187
+ glastApiTime = time .Now ().UnixNano ()
169
188
170
189
body , err := ioutil .ReadAll (res .Body )
171
190
if err != nil {
172
191
fmt .Println (err )
173
192
return output
174
193
}
175
- // fmt.Println(string(body))
194
+ //fmt.Println("ret:" + string(body))
176
195
r := & result {}
177
196
err = json .Unmarshal (body , & r )
178
197
if err != nil {
@@ -183,6 +202,8 @@ func Trans(input *transer.TransInput) *transer.TransOutput {
183
202
// fmt.Println(r.TransResult[0].Src)
184
203
// fmt.Println(r.TransResult[0].Dst)
185
204
output .Result = r .TransResult [0 ].Dst
205
+ } else {
206
+ gFailedCnt ++
186
207
}
187
208
return output
188
209
}
0 commit comments