Skip to content

Commit

Permalink
fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloured-glaze committed Jun 12, 2023
1 parent a474147 commit 2ce2def
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 11 deletions.
143 changes: 143 additions & 0 deletions bdfy/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package bdfy

import (
"encoding/json"
"io"
"math/rand"
"net/http"
"strconv"
"strings"

"github.com/Coloured-glaze/toolbox/str"
)

var (
surl = "https://transmart.qq.com/api/imt"

Form3 = "en"
To3 = "zh"

Lang3 = map[string]string{
"ar": "阿拉伯语", "de": "德语",
"ru": "俄语", "fr": "法语",
"tl": "菲律宾语", "km": "高棉语",
"kr": "韩语", "lo": "老挝语",
"pt": "葡萄牙语", "ja": "日语",
"es": "西班牙语", "it": "意大利语",
"id": "印度尼西亚语", "en": "英语",
"vi": "越南语", "zh": "中文",
}
)

type Ten struct {
Header Header `json:"header"`
Source Source `json:"source"`
Target Target `json:"target"`
}

type Header struct {
Fn string `json:"fn"`
ClientKey string `json:"client_key"`
}

type Source struct {
Lang string `json:"lang"`
TextList []string `json:"text_list"`
}

type Target struct {
Lang string `json:"lang"`
}

// ====================================

type Tresp struct {
Header Header2 `json:"header"`
AutoTranslation []string `json:"auto_translation"`
Msg string `json:"massage"`
}

type Header2 struct {
Type string `json:"type"`
RetCode string `json:"ret_code"`
TimeCost float64 `json:"time_cost"`
RequestID string `json:"request_id"`
}

func Tx(texts []string) (Tresp, error) {
h := Ten{
Header: Header{
Fn: "auto_translation",
// ClientKey: "dHJphbnNtYXJ0X2NyeF9Nb3ppbGxhLzUuMCA2oV2lpVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYg",
ClientKey: "browser-chrome-99.0.4951-Linux-ec27876d-" +
strconv.FormatInt(rand.Int63n(160000000000), 10),
},
Source: Source{
Lang: Form3,
TextList: texts,
},
Target: Target{
Lang: To3,
},
}
tr := Tresp{}
b, err := json.Marshal(h)
if err != nil {
return tr, err
}

client := http.Client{}
req, err := http.NewRequest("POST", surl, strings.NewReader(str.Btos(b)))
if err != nil {
return tr, err
}
req.Header = http.Header{
"Host": []string{"transmart.qq.com"},
"accept": []string{"application/json", "text/plain", "*/*"},
"content-type": []string{"application/json"},
"user-agent": []string{"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.57 Safari/537.36"},
"origin": []string{"https://transmart.qq.com"},
// "sec-fetch-site": []string{"same-origin"},
"sec-fetch-mode": []string{"cors"},
"sec-fetch-dest": []string{"empty"},
// "accept-encoding": []string{"gzip, deflate, br"},
"accept-language": []string{"zh-CN,zh;q=0.9,en;q=0.8"},
// "content-length": []string{"332"},
}
resp, err := client.Do(req)
if err != nil {
return tr, err
}
defer resp.Body.Close()
r, err := io.ReadAll(resp.Body)
if err != nil {
return tr, err
}
// fmt.Printf("%v\n", string(r))
err = json.Unmarshal(r, &tr)
if err != nil {
return tr, err
}
return tr, nil
// s := time.Since(now).Seconds()
}

/* sample:
func run() {
text := "apple"
reply, err := bdfy.Tx([]string{text})
if err != nil {
fmt.Println("Translate error:", err)
return
}
if reply.Msg != "" {
fmt.Println(reply.Msg)
return
}
ra := reply.AutoTranslation
for i := 0; i < len(ra); i++ {
fmt.Println(ra[i])
}
}
*/
20 changes: 20 additions & 0 deletions check/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package check

import (
"errors"
"io"
"os/exec"
)

func Checker(err error) {
if err != nil {
if err == io.EOF {
return
}
// https://pkg.go.dev/os/exec?GOOS=windows
if errors.Is(err, exec.ErrDot) {
return
}
panic(err)
}
}
13 changes: 9 additions & 4 deletions f/forc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"sync"

"github.com/Coloured-glaze/toolbox/check"
"github.com/Coloured-glaze/toolbox/cmd"
)

Expand All @@ -32,8 +33,10 @@ func Forc(g, t int) {

ss, _ := strconv.ParseFloat(strconv.Itoa(int(times)), 64)
sleep := "sleep " + strconv.Itoa(int(ss)) + "s" // shell
start := cmd.Cmd("date '+%s%N'")
starts, _ := strconv.ParseFloat(start, 64)
start, err := cmd.Cmd("date '+%s%N'")
check.Checker(err)
starts, err := strconv.ParseFloat(start, 64)
check.Checker(err)

wg.Add(g)
for i := 0; i < g; i++ {
Expand All @@ -58,7 +61,9 @@ func Forc(g, t int) {
}(ch)
}
wg.Wait()
end := cmd.Cmd("date '+%s%N'")
ends, _ := strconv.ParseFloat(end, 64)
end, err := cmd.Cmd("date '+%s%N'")
check.Checker(err)
ends, err := strconv.ParseFloat(end, 64)
check.Checker(err)
fmt.Printf("%.3f 秒内 %v 个协程加了 %v 次\n", (ends-starts)/1e9, g, j)
}
10 changes: 5 additions & 5 deletions filetool/readir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Files struct {
Suffix string // 文件的后缀(文件类型)
}

// 获取 dir 包含的指定 name 的文件的绝对或相对路径, list 为排除列表
func Readir(dir string, name []string, list []string) ([]Files, int, error) {
// 获取 dir 包含的指定 name 的文件的绝对或相对路径, exclude 为排除列表
func Readir(dir string, name []string, exclude []string) ([]Files, int, error) {
if !IsExist(dir) {
p, _ := os.Getwd()
return nil, 0, fmt.Errorf("当前的路径 %v 未找到 >%v< 文件夹", p, dir)
Expand All @@ -29,8 +29,8 @@ func Readir(dir string, name []string, list []string) ([]Files, int, error) {
if fi.IsDir() { // 忽略目录
return nil
}
if list != nil {
if excluder(list, filename) { // 排除文件
if exclude != nil {
if excluder(exclude, filename) { // 排除文件
return nil
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func addFile(files *[]Files, f *Files, suffix *[]string, num int) int {
return num
}

// f, _, err := ft.Readir("./", []string{".webp"}, nil)
// f, _, err := ft.Readir("./", []string{".webp"}, []string{}, nil)
// if err != nil {
// panic(err)
// }
Expand Down
7 changes: 5 additions & 2 deletions gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/Coloured-glaze/toolbox/check"
"github.com/Coloured-glaze/toolbox/cmd"
ft "github.com/Coloured-glaze/toolbox/filetool"
)
Expand All @@ -28,9 +29,11 @@ func GetRAM() string {
if runtime.GOOS == "windows" {
return "Error! Don't Windows!"
}
R := cmd.Cmd("free -m | awk '/Mem/ {print $2\" \"$3}'")
R, err := cmd.Cmd("free -m | awk '/Mem/ {print $2\" \"$3}'")
check.Checker(err)
if len(R) == 0 {
R = cmd.Cmd("free -m | awk '/内存/ {print $2\" \"$3}'")
R, err = cmd.Cmd("free -m | awk '/内存/ {print $2\" \"$3}'")
check.Checker(err)
}
if len(R) > 2 {
sp := strings.Split(R, " ")
Expand Down
5 changes: 5 additions & 0 deletions js/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func Analyze(a any, config string) error {
return nil
}

// 加载json配置
func Load(a any, config string) error {
return Analyze(a, config)
}

// 保存json配置. 类型, 配置名, 权限: 例如 0600
func Save(a any, config string, perm os.FileMode) error {
b, err := json.Marshal(a)
Expand Down

0 comments on commit 2ce2def

Please sign in to comment.