Skip to content

Commit

Permalink
feat: add emozi
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 13, 2024
1 parent abb9f0f commit b0c31f3
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
ascii2d 搜图
## bilibili
b站相关API
## emozi
颜文字抽象转写
## huggingface
huggingface API
## neteasemusic
Expand Down
7 changes: 5 additions & 2 deletions aireply/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"strings"

"github.com/FloatTech/floatbox/binary"
)

// ChatGPT GPT回复类
Expand Down Expand Up @@ -77,12 +79,13 @@ func chat(msg string, apiKey string, url string) string {
FrequencyPenalty: 0,
PresencePenalty: 0,
}
requestData := bytes.NewBuffer(make([]byte, 0, 1024*1024))
requestData := binary.SelectWriter()
defer binary.PutWriter(requestData)
err := json.NewEncoder(requestData).Encode(&requestBody)
if err != nil {
return err.Error()
}
req, err := http.NewRequest("POST", url+"completions", requestData)
req, err := http.NewRequest("POST", url+"completions", (*bytes.Buffer)(requestData))
if err != nil {
return err.Error()
}
Expand Down
24 changes: 24 additions & 0 deletions emozi/all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package emozi

import "testing"

func TestAll(t *testing.T) {
usr := Anonymous()
in := "你好,世界!"
out, _, err := usr.Marshal(false, in)
if err != nil {
t.Fatal(err)
}
exp := "🥛‎👔⁡🐴‌👤🌹🐱🐴👩,💦🌞😨🌍➕👴😨👨‍🌾!" //nolint: go-staticcheck
if out != exp {
t.Fatal("expected", exp, "but got", out)
}
out, err = usr.Unmarshal(false, out)
if err != nil {
t.Fatal(err)
}
exp = "[你|儗]好,世[界|畍]!"
if out != exp {
t.Fatal("expected", exp, "but got", out)
}
}
40 changes: 40 additions & 0 deletions emozi/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package emozi

Check failure on line 1 in emozi/api.go

View workflow job for this annotation

GitHub Actions / lint

package-comments: should have a package comment (revive)

const api = "https://emozi.seku.su/api/"

type User struct {

Check failure on line 5 in emozi/api.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type User should have comment or be unexported (revive)
name string
pswd string
auth string
}

type encodebody struct {
Random bool `json:"random"`
Text string `json:"text"`
Choice []int `json:"choice"`
}

type encoderesult struct {
Code int `json:"code"`
Message string `json:"message"`
Result struct {
Text string `json:"text"`
Choice []int `json:"choice,omitempty"`
} `json:"result"`
}

type decodebody struct {
Force bool `json:"force"`
Text string `json:"text"`
}

type decoderesult struct {
Code int `json:"code"`
Message string `json:"message"`
Result string `json:"result"`
}

type loginbody struct {
Username string `json:"username"`
Password string `json:"password"`
}
79 changes: 79 additions & 0 deletions emozi/coder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package emozi

Check failure on line 1 in emozi/coder.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
"bytes"
"encoding/json"
"errors"
"net/http"

"github.com/FloatTech/floatbox/binary"
)

func (usr *User) Marshal(randomSameMeaning bool, text string, choices ...int) (string, []int, error) {

Check failure on line 12 in emozi/coder.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method User.Marshal should have comment or be unexported (revive)
w := binary.SelectWriter()
defer binary.PutWriter(w)
err := json.NewEncoder(w).Encode(&encodebody{
Random: randomSameMeaning,
Text: text,
Choice: choices,
})
if err != nil {
return "", nil, err
}
req, err := http.NewRequest("POST", api+"encode", (*bytes.Buffer)(w))
if err != nil {
return "", nil, err
}
req.Header.Set("Content-Type", "application/json")
if usr.auth != "" {
req.Header.Set("Authorization", usr.auth)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
r := encoderesult{}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", nil, err
}
if r.Code != 0 {
return "", nil, errors.New(r.Message)
}
return r.Result.Text, r.Result.Choice, nil
}

func (usr *User) Unmarshal(force bool, text string) (string, error) {

Check failure on line 47 in emozi/coder.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method User.Unmarshal should have comment or be unexported (revive)
w := binary.SelectWriter()
defer binary.PutWriter(w)
err := json.NewEncoder(w).Encode(&decodebody{
Force: force,
Text: text,
})
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", api+"decode", (*bytes.Buffer)(w))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
if usr.auth != "" {
req.Header.Set("Authorization", usr.auth)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
r := decoderesult{}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return "", err
}
if r.Code != 0 {
return "", errors.New(r.Message)
}
return r.Result, nil
}
59 changes: 59 additions & 0 deletions emozi/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package emozi

Check failure on line 1 in emozi/login.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"net/url"

"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/web"
"github.com/tidwall/gjson"
)

func NewUser(name, pswd string) (usr User, err error) {

Check failure on line 16 in emozi/login.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function NewUser should have comment or be unexported (revive)
usr.name = name
usr.pswd = pswd
return
}

func Anonymous() (usr User) {

Check failure on line 22 in emozi/login.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function Anonymous should have comment or be unexported (revive)
return
}

func (usr *User) Login() error {

Check failure on line 26 in emozi/login.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method User.Login should have comment or be unexported (revive)
data, err := web.GetData(api + "getLoginSalt?username=" + url.QueryEscape(usr.name))
if err != nil {
return err
}
r := gjson.ParseBytes(data)
if r.Get("code").Int() != 0 {
return errors.New(r.Get("message").Str)
}
salt := r.Get("result.salt").Str
h := md5.New()
h.Write([]byte(usr.pswd))
h.Write([]byte(salt))
passchlg := hex.EncodeToString(h.Sum(make([]byte, 0, md5.Size)))
w := binary.SelectWriter()
defer binary.PutWriter(w)
err = json.NewEncoder(w).Encode(&loginbody{
Username: usr.name,
Password: passchlg,
})
if err != nil {
return err
}
data, err = web.PostData(api+"login", "application/json", (*bytes.Buffer)(w))
if err != nil {
return err
}
r = gjson.ParseBytes(data)
if r.Get("code").Int() != 0 {
return errors.New(r.Get("message").Str)
}
usr.auth = r.Get("result.token").Str
return nil
}
4 changes: 2 additions & 2 deletions huggingface/huggingface.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type StatusResponse struct {
}

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

// Status 状态请求
func Status(statusURL string, statusReq StatusRequest) (data []byte, err error) {
func Status(statusURL string, statusReq *StatusRequest) (data []byte, err error) {
b, err := json.Marshal(statusReq)
if err != nil {
return
Expand Down
11 changes: 6 additions & 5 deletions novelai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ func (nv *NovalAI) Draw(tags string) (seed int, tagsproceeded string, img []byte
}
seed = config.Parameters.Seed
tagsproceeded = tags
buf := bytes.NewBuffer(nil)
err = config.WrtieTo(buf)
buf := binary.SelectWriter()
defer binary.PutWriter(buf)
err = config.WriteJSON(buf)
if err != nil {
return
}
req, err := http.NewRequest("POST", genapi, buf)
req, err := http.NewRequest("POST", genapi, (*bytes.Buffer)(buf))
if err != nil {
return
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func (p *Payload) String() string {
return binary.BytesToString(b)
}

// WrtieTo ...
func (p *Payload) WrtieTo(w io.Writer) error {
// WriteJSON ...
func (p *Payload) WriteJSON(w io.Writer) error {
return json.NewEncoder(w).Encode(p)
}
4 changes: 2 additions & 2 deletions tts/ttscn/tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type result struct {

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

0 comments on commit b0c31f3

Please sign in to comment.