Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
livepo committed Oct 30, 2020
0 parents commit cde07aa
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 0 deletions.
95 changes: 95 additions & 0 deletions cmd/request.go
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)
}
}
10 changes: 10 additions & 0 deletions go.mod
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
)
Loading

0 comments on commit cde07aa

Please sign in to comment.