Skip to content

Commit b0c31f3

Browse files
committed
feat: add emozi
1 parent abb9f0f commit b0c31f3

File tree

9 files changed

+219
-11
lines changed

9 files changed

+219
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
ascii2d 搜图
77
## bilibili
88
b站相关API
9+
## emozi
10+
颜文字抽象转写
911
## huggingface
1012
huggingface API
1113
## neteasemusic

aireply/chatgpt.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"net/http"
88
"strings"
9+
10+
"github.com/FloatTech/floatbox/binary"
911
)
1012

1113
// ChatGPT GPT回复类
@@ -77,12 +79,13 @@ func chat(msg string, apiKey string, url string) string {
7779
FrequencyPenalty: 0,
7880
PresencePenalty: 0,
7981
}
80-
requestData := bytes.NewBuffer(make([]byte, 0, 1024*1024))
82+
requestData := binary.SelectWriter()
83+
defer binary.PutWriter(requestData)
8184
err := json.NewEncoder(requestData).Encode(&requestBody)
8285
if err != nil {
8386
return err.Error()
8487
}
85-
req, err := http.NewRequest("POST", url+"completions", requestData)
88+
req, err := http.NewRequest("POST", url+"completions", (*bytes.Buffer)(requestData))
8689
if err != nil {
8790
return err.Error()
8891
}

emozi/all_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package emozi
2+
3+
import "testing"
4+
5+
func TestAll(t *testing.T) {
6+
usr := Anonymous()
7+
in := "你好,世界!"
8+
out, _, err := usr.Marshal(false, in)
9+
if err != nil {
10+
t.Fatal(err)
11+
}
12+
exp := "🥛‎👔⁡🐴‌👤🌹🐱🐴👩,💦🌞😨🌍➕👴😨👨‍🌾!" //nolint: go-staticcheck
13+
if out != exp {
14+
t.Fatal("expected", exp, "but got", out)
15+
}
16+
out, err = usr.Unmarshal(false, out)
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
exp = "[你|儗]好,世[界|畍]!"
21+
if out != exp {
22+
t.Fatal("expected", exp, "but got", out)
23+
}
24+
}

emozi/api.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package emozi
2+
3+
const api = "https://emozi.seku.su/api/"
4+
5+
type User struct {
6+
name string
7+
pswd string
8+
auth string
9+
}
10+
11+
type encodebody struct {
12+
Random bool `json:"random"`
13+
Text string `json:"text"`
14+
Choice []int `json:"choice"`
15+
}
16+
17+
type encoderesult struct {
18+
Code int `json:"code"`
19+
Message string `json:"message"`
20+
Result struct {
21+
Text string `json:"text"`
22+
Choice []int `json:"choice,omitempty"`
23+
} `json:"result"`
24+
}
25+
26+
type decodebody struct {
27+
Force bool `json:"force"`
28+
Text string `json:"text"`
29+
}
30+
31+
type decoderesult struct {
32+
Code int `json:"code"`
33+
Message string `json:"message"`
34+
Result string `json:"result"`
35+
}
36+
37+
type loginbody struct {
38+
Username string `json:"username"`
39+
Password string `json:"password"`
40+
}

emozi/coder.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package emozi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"net/http"
8+
9+
"github.com/FloatTech/floatbox/binary"
10+
)
11+
12+
func (usr *User) Marshal(randomSameMeaning bool, text string, choices ...int) (string, []int, error) {
13+
w := binary.SelectWriter()
14+
defer binary.PutWriter(w)
15+
err := json.NewEncoder(w).Encode(&encodebody{
16+
Random: randomSameMeaning,
17+
Text: text,
18+
Choice: choices,
19+
})
20+
if err != nil {
21+
return "", nil, err
22+
}
23+
req, err := http.NewRequest("POST", api+"encode", (*bytes.Buffer)(w))
24+
if err != nil {
25+
return "", nil, err
26+
}
27+
req.Header.Set("Content-Type", "application/json")
28+
if usr.auth != "" {
29+
req.Header.Set("Authorization", usr.auth)
30+
}
31+
resp, err := http.DefaultClient.Do(req)
32+
if err != nil {
33+
return "", nil, err
34+
}
35+
defer resp.Body.Close()
36+
r := encoderesult{}
37+
err = json.NewDecoder(resp.Body).Decode(&r)
38+
if err != nil {
39+
return "", nil, err
40+
}
41+
if r.Code != 0 {
42+
return "", nil, errors.New(r.Message)
43+
}
44+
return r.Result.Text, r.Result.Choice, nil
45+
}
46+
47+
func (usr *User) Unmarshal(force bool, text string) (string, error) {
48+
w := binary.SelectWriter()
49+
defer binary.PutWriter(w)
50+
err := json.NewEncoder(w).Encode(&decodebody{
51+
Force: force,
52+
Text: text,
53+
})
54+
if err != nil {
55+
return "", err
56+
}
57+
req, err := http.NewRequest("POST", api+"decode", (*bytes.Buffer)(w))
58+
if err != nil {
59+
return "", err
60+
}
61+
req.Header.Set("Content-Type", "application/json")
62+
if usr.auth != "" {
63+
req.Header.Set("Authorization", usr.auth)
64+
}
65+
resp, err := http.DefaultClient.Do(req)
66+
if err != nil {
67+
return "", err
68+
}
69+
defer resp.Body.Close()
70+
r := decoderesult{}
71+
err = json.NewDecoder(resp.Body).Decode(&r)
72+
if err != nil {
73+
return "", err
74+
}
75+
if r.Code != 0 {
76+
return "", errors.New(r.Message)
77+
}
78+
return r.Result, nil
79+
}

emozi/login.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package emozi
2+
3+
import (
4+
"bytes"
5+
"crypto/md5"
6+
"encoding/hex"
7+
"encoding/json"
8+
"errors"
9+
"net/url"
10+
11+
"github.com/FloatTech/floatbox/binary"
12+
"github.com/FloatTech/floatbox/web"
13+
"github.com/tidwall/gjson"
14+
)
15+
16+
func NewUser(name, pswd string) (usr User, err error) {
17+
usr.name = name
18+
usr.pswd = pswd
19+
return
20+
}
21+
22+
func Anonymous() (usr User) {
23+
return
24+
}
25+
26+
func (usr *User) Login() error {
27+
data, err := web.GetData(api + "getLoginSalt?username=" + url.QueryEscape(usr.name))
28+
if err != nil {
29+
return err
30+
}
31+
r := gjson.ParseBytes(data)
32+
if r.Get("code").Int() != 0 {
33+
return errors.New(r.Get("message").Str)
34+
}
35+
salt := r.Get("result.salt").Str
36+
h := md5.New()
37+
h.Write([]byte(usr.pswd))
38+
h.Write([]byte(salt))
39+
passchlg := hex.EncodeToString(h.Sum(make([]byte, 0, md5.Size)))
40+
w := binary.SelectWriter()
41+
defer binary.PutWriter(w)
42+
err = json.NewEncoder(w).Encode(&loginbody{
43+
Username: usr.name,
44+
Password: passchlg,
45+
})
46+
if err != nil {
47+
return err
48+
}
49+
data, err = web.PostData(api+"login", "application/json", (*bytes.Buffer)(w))
50+
if err != nil {
51+
return err
52+
}
53+
r = gjson.ParseBytes(data)
54+
if r.Get("code").Int() != 0 {
55+
return errors.New(r.Get("message").Str)
56+
}
57+
usr.auth = r.Get("result.token").Str
58+
return nil
59+
}

huggingface/huggingface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type StatusResponse struct {
6363
}
6464

6565
// Push 推送请求
66-
func Push(pushURL string, pushReq PushRequest) (pushRes PushResponse, err error) {
66+
func Push(pushURL string, pushReq *PushRequest) (pushRes PushResponse, err error) {
6767
b, err := json.Marshal(pushReq)
6868
if err != nil {
6969
return
@@ -77,7 +77,7 @@ func Push(pushURL string, pushReq PushRequest) (pushRes PushResponse, err error)
7777
}
7878

7979
// Status 状态请求
80-
func Status(statusURL string, statusReq StatusRequest) (data []byte, err error) {
80+
func Status(statusURL string, statusReq *StatusRequest) (data []byte, err error) {
8181
b, err := json.Marshal(statusReq)
8282
if err != nil {
8383
return

novelai/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ func (nv *NovalAI) Draw(tags string) (seed int, tagsproceeded string, img []byte
6767
}
6868
seed = config.Parameters.Seed
6969
tagsproceeded = tags
70-
buf := bytes.NewBuffer(nil)
71-
err = config.WrtieTo(buf)
70+
buf := binary.SelectWriter()
71+
defer binary.PutWriter(buf)
72+
err = config.WriteJSON(buf)
7273
if err != nil {
7374
return
7475
}
75-
req, err := http.NewRequest("POST", genapi, buf)
76+
req, err := http.NewRequest("POST", genapi, (*bytes.Buffer)(buf))
7677
if err != nil {
7778
return
7879
}
@@ -156,7 +157,7 @@ func (p *Payload) String() string {
156157
return binary.BytesToString(b)
157158
}
158159

159-
// WrtieTo ...
160-
func (p *Payload) WrtieTo(w io.Writer) error {
160+
// WriteJSON ...
161+
func (p *Payload) WriteJSON(w io.Writer) error {
161162
return json.NewEncoder(w).Encode(p)
162163
}

tts/ttscn/tts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ type result struct {
126126

127127
// Speak 返回音频本地路径
128128
func (tts *TTS) Speak(_ int64, text func() string) (fileName string, err error) {
129-
q := binary.NewWriterF(func(w *binary.Writer) {
129+
q, cl := binary.OpenWriterF(func(w *binary.Writer) {
130130
w.WriteString("language=")
131131
w.WriteString(url.QueryEscape(tts.language))
132132
w.WriteString("&voice=")
@@ -148,7 +148,7 @@ func (tts *TTS) Speak(_ int64, text func() string) (fileName string, err error)
148148
w.WriteString("&styledegree=")
149149
w.WriteString(strconv.FormatFloat(tts.styledegree, 'f', 2, 64))
150150
})
151-
println(string(q))
151+
defer cl()
152152
data, err := web.RequestDataWithHeaders(
153153
web.NewTLS12Client(), ttsapi, "POST", func(r *http.Request) error {
154154
r.Header.Add("accept", "*/*")

0 commit comments

Comments
 (0)