fix: customize tts download

This commit is contained in:
源文雨
2023-08-28 00:05:09 +08:00
parent f043fbf0c0
commit 9dade7f3e8
4 changed files with 41 additions and 4 deletions

View File

@@ -2,11 +2,17 @@
package aireply
import (
"crypto/md5"
"encoding/hex"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/FloatTech/AnimeAPI/tts/genshin"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
@@ -75,6 +81,12 @@ func init() { // 插件主体
})
endpre := regexp.MustCompile(`\pP$`)
ttscachedir := ent.DataFolder() + "cache/"
_ = os.RemoveAll(ttscachedir)
err := os.MkdirAll(ttscachedir, 0755)
if err != nil {
panic(err)
}
ent.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText()
@@ -98,6 +110,17 @@ func init() { // 插件主体
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply))
return
}
if strings.HasPrefix(rec, "http") {
b := md5.Sum(binary.StringToBytes(rec))
fn := hex.EncodeToString(b[:])
fp := ttscachedir + fn
if file.IsNotExist(fp) {
if file.DownloadTo(rec, fp) != nil {
return
}
}
rec = "file:///" + file.BOTPATH + "/" + fp
}
// 发送语音
if id := ctx.SendChain(message.Record(rec)); id.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply))

View File

@@ -2,6 +2,8 @@
package moegoe
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
@@ -9,6 +11,8 @@ import (
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/AnimeAPI/tts/genshin"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
@@ -53,6 +57,16 @@ func init() {
}
text := ctx.State["regex_matched"].([]string)[2]
name := ctx.State["regex_matched"].([]string)[1]
ctx.SendChain(message.Record(fmt.Sprintf(genshin.CNAPI, name, url.QueryEscape(text), url.QueryEscape(.k))))
rec := fmt.Sprintf(genshin.CNAPI, name, url.QueryEscape(text), url.QueryEscape(.k))
b := md5.Sum(binary.StringToBytes(rec))
fn := hex.EncodeToString(b[:])
fp := "data/tts/" + fn
if file.IsNotExist(fp) {
if file.DownloadTo(rec, fp) != nil {
return
}
}
rec = "file:///" + file.BOTPATH + "/" + fp
ctx.SendChain(message.Record(rec))
})
}