-
Notifications
You must be signed in to change notification settings - Fork 28
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
Showing
9 changed files
with
219 additions
and
11 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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
ascii2d 搜图 | ||
## bilibili | ||
b站相关API | ||
## emozi | ||
颜文字抽象转写 | ||
## huggingface | ||
huggingface API | ||
## neteasemusic | ||
|
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
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,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) | ||
} | ||
} |
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,40 @@ | ||
package emozi | ||
|
||
const api = "https://emozi.seku.su/api/" | ||
|
||
type User struct { | ||
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"` | ||
} |
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,79 @@ | ||
package emozi | ||
|
||
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) { | ||
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) { | ||
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 | ||
} |
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,59 @@ | ||
package emozi | ||
|
||
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) { | ||
usr.name = name | ||
usr.pswd = pswd | ||
return | ||
} | ||
|
||
func Anonymous() (usr User) { | ||
return | ||
} | ||
|
||
func (usr *User) Login() error { | ||
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 | ||
} |
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
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
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