-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
加入url encode,(应该)不会出现错误 Signed-off-by: UlinoyaPed <[email protected]>
- Loading branch information
1 parent
37b4987
commit 2c8b5aa
Showing
6 changed files
with
190 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
.idea | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
#releases | ||
releases/* | ||
*.syso | ||
|
||
#build | ||
!go-winres.exe | ||
!buildcross.bat | ||
!winres/* | ||
|
||
#other | ||
*.log | ||
*.bk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package BaiduTranslate | ||
|
||
import ( | ||
"log" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// 百度翻译开放平台信息 | ||
type BaiduInfo struct { | ||
AppID string | ||
Salt string | ||
SecretKey string | ||
} | ||
|
||
// 自动生盐 | ||
// 入口参数为盐的长度 | ||
func Salt(l int) string { | ||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
bytes := []byte(str) | ||
result := []byte{} | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
for i := 0; i < l; i++ { | ||
result = append(result, bytes[r.Intn(len(bytes))]) | ||
} | ||
return string(result) | ||
} | ||
|
||
// 错误检测,如果有错误输出msg | ||
func checkErr(e error, msg string) { | ||
if e != nil { | ||
log.Println(msg) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package BaiduTranslate | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
func (BaiduInfo *BaiduInfo) NormalTr(Text string, From string, To string) string { | ||
|
||
type TransResult struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
Result [1]struct { | ||
Src string `json:"src"` | ||
Dst string `json:"dst"` | ||
} `json:"trans_result"` | ||
ErrorCode string `json:"error_code"` | ||
ErrorMsg string `json:"error_msg"` | ||
} | ||
|
||
//合并字符串,计算sign | ||
montage := BaiduInfo.AppID + Text + BaiduInfo.Salt + BaiduInfo.SecretKey | ||
ctx := md5.New() | ||
ctx.Write([]byte(montage)) | ||
sign := hex.EncodeToString(ctx.Sum(nil)) | ||
|
||
// 翻译 传入需要翻译的语句 | ||
urlstr := "http://fanyi-api.baidu.com/api/trans/vip/translate?q=" + url.QueryEscape(Text) + "&from=" + From + "&to=" + To + "&appid=" + BaiduInfo.AppID + "&salt=" + BaiduInfo.Salt + "&sign=" + sign | ||
|
||
// 发送GET请求 | ||
resp, err := http.Get(urlstr) | ||
checkErr(err, "HTTP GET出现错误!") | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
|
||
var ts TransResult | ||
_ = json.Unmarshal(body, &ts) | ||
if ts.ErrorCode != "" { | ||
errmsg := "错误码:" + ts.ErrorCode + ",错误信息:" + ts.ErrorMsg | ||
return errmsg | ||
} else { | ||
return ts.Result[0].Dst | ||
} | ||
} | ||
|
||
/* | ||
// 返回结果 | ||
type TransResult struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
Result [1]Result `json:"trans_result"` | ||
ErrorCode string `json:"error_code"` | ||
ErrorMsg string `json:"error_msg"` | ||
Data [1]Data `json:"data"` | ||
} | ||
type Result struct { | ||
Src string `json:"src"` | ||
Dst string `json:"dst"` | ||
} | ||
type Data struct { | ||
Src string `json:"src"` | ||
} | ||
// 生成32位MD5 | ||
func Sign(bi *BaiduInfo) string { | ||
text := bi.AppID + bi.Text + bi.Salt + bi.SecretKey | ||
ctx := md5.New() | ||
ctx.Write([]byte(text)) | ||
return hex.EncodeToString(ctx.Sum(nil)) | ||
} | ||
// 翻译 传入需要翻译的语句 | ||
func (bi *BaiduInfo) Translate() string { | ||
url := "http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + bi.Text + "&from=" + bi.From + "&to=" + bi.To + "&appid=" + bi.AppID + "&salt=" + bi.Salt + "&sign=" + Sign(bi) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Println("网络异常") | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
var ts TransResult | ||
_ = json.Unmarshal(body, &ts) | ||
if ts.ErrorCode != "" { | ||
return ts.ErrorMsg | ||
} else { | ||
return ts.Result[0].Dst | ||
} | ||
} | ||
func (bi *BaiduInfo) Detect() string { | ||
url := "http://api.fanyi.baidu.com/api/trans/vip/language?q=" + bi.Text + "&appid=" + bi.AppID + "&salt=" + bi.Salt + "&sign=" + Sign(bi) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Println("网络异常") | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
var ts TransResult | ||
_ = json.Unmarshal(body, &ts) | ||
if ts.ErrorCode != "" { | ||
return ts.ErrorCode | ||
} else { | ||
return ts.Data[0].Src | ||
} | ||
} | ||
*/ |
This file was deleted.
Oops, something went wrong.