-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
livepo
committed
Oct 30, 2020
0 parents
commit cde07aa
Showing
5 changed files
with
419 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/levigross/grequests" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"github.com/doun/terminal/color" | ||
) | ||
|
||
|
||
var ( | ||
APPID = "20201027000599716" | ||
SECRET = "qQ7qefdSXnlfYRGTv_Rq" | ||
SALT = "hello" | ||
) | ||
|
||
|
||
func MD5(str string) string { | ||
h := md5.New() | ||
h.Write([]byte(str)) | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} | ||
|
||
|
||
type Result struct { | ||
Src string `json:"src"` | ||
Dst string `Json:"dst"` | ||
} | ||
|
||
type RespStruct struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
TransResult []Result `json:"trans_result"` | ||
} | ||
|
||
|
||
func GuessLanguange(Q string) string { | ||
for _, c := range Q { | ||
if c > 1000 { | ||
return "en" | ||
} | ||
} | ||
return "zh" | ||
} | ||
|
||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "bb", | ||
Short: "bb is a terminal translate app", | ||
Long: `bb uses baidu translate api, support English to Chinese or Chinese to English, | ||
e.g: | ||
bb 中国 | ||
bb Hello | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
Q := strings.Join(args, " ") | ||
ro := &grequests.RequestOptions{ | ||
Params: map[string]string{ | ||
"q": Q, | ||
"from": "auto", | ||
"to": GuessLanguange(Q), | ||
"appid": APPID, | ||
"salt": SALT, | ||
"sign": MD5(APPID + Q + SALT + SECRET), | ||
}, | ||
} | ||
resp, err := grequests.Get("https://fanyi-api.baidu.com/api/trans/vip/translate", ro) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
var respStruct RespStruct | ||
if err := json.Unmarshal([]byte(resp.String()), &respStruct); err != nil { | ||
fmt.Println(err) | ||
} else { | ||
for _, result := range respStruct.TransResult { | ||
color.Println("@{wK}" + result.Src) | ||
fmt.Println("") | ||
color.Println("@{bK}" + result.Dst) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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,10 @@ | ||
module bb | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/doun/terminal v0.0.0-20121006134212-44529d95bd71 | ||
github.com/levigross/grequests v0.0.0-20190908174114-253788527a1a | ||
github.com/spf13/cobra v1.1.1 | ||
golang.org/x/net v0.0.0-20201026091529-146b70c837a4 // indirect | ||
) |
Oops, something went wrong.