mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-13 02:30:26 +00:00
🚧🎨⚡️ 重大重构中
This commit is contained in:
111
setutime/classify.go
Normal file
111
setutime/classify.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var (
|
||||
CACHE_IMG_FILE = "/tmp/setugt"
|
||||
CACHE_URI = "file:///" + CACHE_IMG_FILE
|
||||
VOTE_API_URL = "http://saki.fumiama.top/vote?uuid=零号&img=%s&class=%d"
|
||||
CLASSIFY_HEAD = "http://saki.fumiama.top:62002/dice?url="
|
||||
MsgofGrp = make(map[int64]int64)
|
||||
dhashofmsg = make(map[int64]string)
|
||||
)
|
||||
|
||||
func Classify(ctx *zero.Ctx, targeturl string, noimg bool) {
|
||||
get_url := CLASSIFY_HEAD + url.QueryEscape(targeturl)
|
||||
if noimg {
|
||||
get_url += "&noimg=true"
|
||||
}
|
||||
resp, err := http.Get(get_url)
|
||||
if err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
} else {
|
||||
if noimg {
|
||||
data, err1 := ioutil.ReadAll(resp.Body)
|
||||
if err1 == nil {
|
||||
dhash := gjson.GetBytes(data, "img").String()
|
||||
class := int(gjson.GetBytes(data, "class").Int())
|
||||
replyClass(ctx, dhash, class, noimg)
|
||||
} else {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err1))
|
||||
}
|
||||
} else {
|
||||
class, err1 := strconv.Atoi(resp.Header.Get("Class"))
|
||||
dhash := resp.Header.Get("DHash")
|
||||
if err1 != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err1))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// 写入文件
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
f, _ := os.OpenFile(CACHE_IMG_FILE, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
|
||||
defer f.Close()
|
||||
f.Write(data)
|
||||
replyClass(ctx, dhash, class, noimg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Vote(ctx *zero.Ctx, class int) {
|
||||
msg, ok := MsgofGrp[ctx.Event.GroupID]
|
||||
if ok {
|
||||
ctx.DeleteMessage(msg)
|
||||
delete(MsgofGrp, ctx.Event.GroupID)
|
||||
dhash, ok2 := dhashofmsg[msg]
|
||||
if ok2 {
|
||||
http.Get(fmt.Sprintf(VOTE_API_URL, dhash, class))
|
||||
delete(dhashofmsg, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func replyClass(ctx *zero.Ctx, dhash string, class int, noimg bool) {
|
||||
if class > 4 {
|
||||
switch class {
|
||||
case 5:
|
||||
ctx.Send("[5]影响不好啦!")
|
||||
case 6:
|
||||
ctx.Send("[6]太涩啦,🐛了!")
|
||||
}
|
||||
if dhash != "" && !noimg {
|
||||
b14, err3 := url.QueryUnescape(dhash)
|
||||
if err3 == nil {
|
||||
ctx.Send("给你点提示哦:" + b14)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var last_message_id int64
|
||||
if !noimg {
|
||||
last_message_id = ctx.SendChain(message.Image(CACHE_URI).Add("no_cache", "0"))
|
||||
last_group_id := ctx.Event.GroupID
|
||||
MsgofGrp[last_group_id] = last_message_id
|
||||
dhashofmsg[last_message_id] = dhash
|
||||
} else {
|
||||
last_message_id = ctx.Event.MessageID
|
||||
}
|
||||
switch class {
|
||||
case 0:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[0]一堆像素点"))
|
||||
case 1:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[1]普通"))
|
||||
case 2:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[2]有点意思"))
|
||||
case 3:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[3]不错"))
|
||||
case 4:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[4]我好啦!"))
|
||||
}
|
||||
}
|
||||
}
|
||||
167
setutime/pic_searcher.go
Normal file
167
setutime/pic_searcher.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/FloatTech/AnimeAPI/ascii2d"
|
||||
"github.com/FloatTech/AnimeAPI/pixiv"
|
||||
"github.com/FloatTech/AnimeAPI/saucenao"
|
||||
)
|
||||
|
||||
var (
|
||||
CACHEPATH = os.TempDir() // 缓冲图片路径
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
// 根据PID搜图
|
||||
zero.OnRegex(`^搜图(\d+)$`).SetBlock(true).SetPriority(30).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
id, _ := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64)
|
||||
ctx.Send("少女祈祷中......")
|
||||
// 获取P站插图信息
|
||||
illust, err := pixiv.Works(id)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
// 下载P站插图
|
||||
if _, err := download(illust, "data/SetuTime/search/"); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
// 发送搜索结果
|
||||
ctx.SendChain(
|
||||
message.Image(file(illust)),
|
||||
message.Text(
|
||||
"\n",
|
||||
"标题:", illust.Title, "\n",
|
||||
"插画ID:", illust.Pid, "\n",
|
||||
"画师:", illust.UserName, "\n",
|
||||
"画师ID:", illust.UserId, "\n",
|
||||
"直链:", "https://pixivel.moe/detail?id=", illust.Pid,
|
||||
),
|
||||
)
|
||||
})
|
||||
// 以图搜图
|
||||
zero.OnMessage(FullMatchText("以图搜图", "搜索图片", "以图识图"), MustHasPicture()).SetBlock(true).SetPriority(999).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
// 开始搜索图片
|
||||
ctx.Send("少女祈祷中......")
|
||||
for _, pic := range ctx.State["image_url"].([]string) {
|
||||
fmt.Println(pic)
|
||||
if result, err := saucenao.SauceNAO(pic); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
} else {
|
||||
// 返回SauceNAO的结果
|
||||
ctx.SendChain(
|
||||
message.Text("我有把握是这个!"),
|
||||
message.Image(result.Thumbnail),
|
||||
message.Text(
|
||||
"\n",
|
||||
"相似度:", result.Similarity, "\n",
|
||||
"标题:", result.Title, "\n",
|
||||
"插画ID:", result.PixivID, "\n",
|
||||
"画师:", result.MemberName, "\n",
|
||||
"画师ID:", result.MemberID, "\n",
|
||||
"直链:", "https://pixivel.moe/detail?id=", result.PixivID,
|
||||
),
|
||||
)
|
||||
continue
|
||||
}
|
||||
if result, err := ascii2d.Ascii2d(pic); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
} else {
|
||||
// 返回Ascii2d的结果
|
||||
ctx.SendChain(
|
||||
message.Text(
|
||||
"大概是这个?", "\n",
|
||||
"标题:", result.Title, "\n",
|
||||
"插画ID:", result.Pid, "\n",
|
||||
"画师:", result.UserName, "\n",
|
||||
"画师ID:", result.UserId, "\n",
|
||||
"直链:", "https://pixivel.moe/detail?id=", result.Pid,
|
||||
),
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// FullMatchText 如果信息中文本完全匹配则返回 true
|
||||
func FullMatchText(src ...string) zero.Rule {
|
||||
return func(ctx *zero.Ctx) bool {
|
||||
msg := ctx.Event.Message
|
||||
for _, elem := range msg {
|
||||
if elem.Type == "text" {
|
||||
text := elem.Data["text"]
|
||||
text = strings.ReplaceAll(text, " ", "")
|
||||
text = strings.ReplaceAll(text, "\r", "")
|
||||
text = strings.ReplaceAll(text, "\n", "")
|
||||
for _, s := range src {
|
||||
if text == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// HasPicture 消息含有图片返回 true
|
||||
func HasPicture() zero.Rule {
|
||||
return func(ctx *zero.Ctx) bool {
|
||||
msg := ctx.Event.Message
|
||||
url := []string{}
|
||||
// 如果是回复信息则将信息替换成被回复的那条
|
||||
if msg[0].Type == "reply" {
|
||||
id, _ := strconv.Atoi(msg[0].Data["id"])
|
||||
msg = ctx.GetMessage(int64(id)).Elements
|
||||
}
|
||||
// 遍历信息中所有图片
|
||||
for _, elem := range msg {
|
||||
if elem.Type == "image" {
|
||||
url = append(url, elem.Data["url"])
|
||||
}
|
||||
}
|
||||
// 如果有图片就返回true
|
||||
if len(url) > 0 {
|
||||
ctx.State["image_url"] = url
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// MustHasPicture 消息不存在图片阻塞60秒至有图片,超时返回 false
|
||||
func MustHasPicture() zero.Rule {
|
||||
return func(ctx *zero.Ctx) bool {
|
||||
if HasPicture()(ctx) {
|
||||
return true
|
||||
}
|
||||
// 没有图片就索取
|
||||
ctx.Send("请发送一张图片")
|
||||
next := zero.NewFutureEvent("message", 999, false, zero.CheckUser(ctx.Event.UserID), HasPicture())
|
||||
recv, cancel := next.Repeat()
|
||||
select {
|
||||
case e := <-recv:
|
||||
cancel()
|
||||
newCtx := &zero.Ctx{Event: e, State: zero.State{}}
|
||||
if HasPicture()(newCtx) {
|
||||
ctx.State["image_url"] = newCtx.State["image_url"]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case <-time.After(time.Second * 60):
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
73
setutime/rand_image.go
Normal file
73
setutime/rand_image.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
var (
|
||||
RANDOM_API_URL = "https://api.pixivweb.com/anime18r.php?return=img"
|
||||
BLOCK_REQUEST = false
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
zero.OnRegex(`^设置随机图片网址(.*)$`, zero.SuperUserPermission).SetBlock(true).SetPriority(20).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
url := ctx.State["regex_matched"].([]string)[1]
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
ctx.Send("URL非法!")
|
||||
} else {
|
||||
RANDOM_API_URL = url
|
||||
}
|
||||
return
|
||||
})
|
||||
// 有保护的随机图片
|
||||
zero.OnFullMatch("随机图片").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if ctx.Event.GroupID > 0 {
|
||||
Classify(ctx, RANDOM_API_URL, false)
|
||||
}
|
||||
return
|
||||
})
|
||||
// 直接随机图片,危险,仅管理员可用
|
||||
zero.OnFullMatch("直接随机", zero.AdminPermission).SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if ctx.Event.GroupID > 0 {
|
||||
if BLOCK_REQUEST {
|
||||
ctx.Send("请稍后再试哦")
|
||||
} else {
|
||||
BLOCK_REQUEST = true
|
||||
last_message_id := ctx.SendChain(message.Image(RANDOM_API_URL).Add("no_cache", "0"))
|
||||
last_group_id := ctx.Event.GroupID
|
||||
MsgofGrp[last_group_id] = last_message_id
|
||||
BLOCK_REQUEST = false
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
// 撤回最后的随机图片
|
||||
zero.OnFullMatch("不许好").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
Vote(ctx, 5)
|
||||
})
|
||||
// 撤回最后的随机图片
|
||||
zero.OnFullMatch("太涩了").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
Vote(ctx, 6)
|
||||
})
|
||||
// 上传一张图进行评价
|
||||
zero.OnFullMatch("评价图片", MustHasPicture()).SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if ctx.Event.GroupID > 0 {
|
||||
ctx.Send("少女祈祷中......")
|
||||
for _, pic := range ctx.State["image_url"].([]string) {
|
||||
fmt.Println(pic)
|
||||
Classify(ctx, pic, true)
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
@@ -1,140 +1,147 @@
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/extension/rate"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
|
||||
apiutils "github.com/Yiwen-Chan/ZeroBot-Plugin/api/utils"
|
||||
"github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils"
|
||||
"github.com/FloatTech/AnimeAPI/pixiv"
|
||||
)
|
||||
|
||||
var limit = rate.NewManager(time.Minute*1, 5)
|
||||
// Pools 图片缓冲池
|
||||
type Pool struct {
|
||||
Lock sync.Mutex
|
||||
DB *Sqlite
|
||||
Path string
|
||||
Group int64
|
||||
List []string
|
||||
Max int
|
||||
Pool map[string][]*pixiv.Illust
|
||||
Form int64
|
||||
}
|
||||
|
||||
var (
|
||||
BOTPATH = apiutils.PathExecute() // 当前bot运行目录
|
||||
DATAPATH = BOTPATH + "data/SetuTime/" // 数据目录
|
||||
DBPATH = DATAPATH + "SetuTime.db" // 数据库路径
|
||||
|
||||
CACHEPATH = DATAPATH + "cache/" // 缓冲图片路径
|
||||
CACHEGROUP int64 = 0 // 缓冲图片群,为0即可
|
||||
PoolList = []string{"涩图", "二次元", "风景", "车万"} // 可自定义
|
||||
|
||||
DB = utils.Sqlite{DBPath: DBPATH} // 新建涩图数据库对象
|
||||
PoolsCache = utils.NewPoolsCache() // 新建一个缓冲池对象
|
||||
|
||||
FORM = "PIC" // 默认 PIC 格式
|
||||
)
|
||||
|
||||
func init() {
|
||||
PoolsCache.Group = CACHEGROUP // 图片缓冲群
|
||||
PoolsCache.Path = CACHEPATH // 缓冲图片路径
|
||||
|
||||
apiutils.CreatePath(DBPATH)
|
||||
apiutils.CreatePath(CACHEPATH)
|
||||
|
||||
for i := range PoolList {
|
||||
if err := DB.Create(PoolList[i], &pixiv.Illust{}); err != nil {
|
||||
// NewPoolsCache 返回一个缓冲池对象
|
||||
func NewPools() *Pool {
|
||||
cache := &Pool{
|
||||
DB: &Sqlite{DBPath: "data/SetuTime/SetuTime.db"},
|
||||
Path: "data/SetuTime/cache/",
|
||||
Group: 0,
|
||||
List: []string{"涩图", "二次元", "风景", "车万"}, // 可以自己加类别,得自己加图片进数据库
|
||||
Max: 10,
|
||||
Pool: map[string][]*pixiv.Illust{},
|
||||
Form: 0,
|
||||
}
|
||||
err := os.MkdirAll(cache.Path, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for i := range cache.List {
|
||||
if err := cache.DB.Create(cache.List[i], &pixiv.Illust{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return cache
|
||||
}
|
||||
|
||||
var (
|
||||
POOL = NewPools()
|
||||
limit = rate.NewManager(time.Minute*1, 5)
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
zero.OnRegex(`^来份(.*)$`, FirstValueInList(PoolList)).SetBlock(true).SetPriority(20).
|
||||
zero.OnRegex(`^来份(.*)$`, FirstValueInList(POOL.List)).SetBlock(true).SetPriority(20).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if !limit.Load(ctx.Event.UserID).Acquire() {
|
||||
ctx.Send("请稍后重试0x0...")
|
||||
ctx.SendChain(message.Text("少女祈祷中......"))
|
||||
return
|
||||
}
|
||||
var type_ = ctx.State["regex_matched"].([]string)[1]
|
||||
// 补充池子
|
||||
go func() {
|
||||
times := apiutils.Min(PoolsCache.Max-PoolsCache.Size(type_), 2)
|
||||
times := Min(POOL.Max-POOL.Size(type_), 2)
|
||||
for i := 0; i < times; i++ {
|
||||
illust := &pixiv.Illust{}
|
||||
// 查询出一张图片
|
||||
if err := DB.Select(type_, illust, "ORDER BY RANDOM() limit 1"); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
if err := POOL.DB.Select(type_, illust, "ORDER BY RANDOM() limit 1"); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
continue
|
||||
}
|
||||
ctx.SendGroupMessage(PoolsCache.Group, "正在下载"+illust.ImageUrls)
|
||||
file, err := illust.PixivPicDown(PoolsCache.Path)
|
||||
// 下载图片
|
||||
file, err := download(illust, POOL.Path)
|
||||
if err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
continue
|
||||
}
|
||||
ctx.SendGroupMessage(PoolsCache.Group, illust.NormalPic(file))
|
||||
ctx.SendGroupMessage(POOL.Group, []message.MessageSegment{message.Image(file)})
|
||||
// 向缓冲池添加一张图片
|
||||
if err := PoolsCache.Push(type_, illust); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
continue
|
||||
}
|
||||
POOL.Push(type_, illust)
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
}
|
||||
}()
|
||||
// 如果没有缓存,阻塞5秒
|
||||
if PoolsCache.Size(type_) == 0 {
|
||||
ctx.Send("[SetuTime] 正在填充弹药......")
|
||||
if POOL.Size(type_) == 0 {
|
||||
ctx.SendChain(message.Text("INFO: 正在填充弹药......"))
|
||||
<-time.After(time.Second * 5)
|
||||
if PoolsCache.Size(type_) == 0 {
|
||||
ctx.Send("[SetuTime] 等待填充,请稍后再试......")
|
||||
if POOL.Size(type_) == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
|
||||
return
|
||||
}
|
||||
}
|
||||
// 从缓冲池里抽一张
|
||||
if id := ctx.Send(PoolsCache.GetOnePic(type_, FORM)); id == 0 {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", errors.New("可能被风控了")))
|
||||
if id := ctx.SendChain(message.Image(file(POOL.Pop(type_)))); id == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
zero.OnRegex(`^添加(.*?)(\d+)$`, FirstValueInList(PoolList), zero.SuperUserPermission).SetBlock(true).SetPriority(21).
|
||||
zero.OnRegex(`^添加(.*?)(\d+)$`, FirstValueInList(POOL.List), zero.SuperUserPermission).SetBlock(true).SetPriority(21).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
var (
|
||||
type_ = ctx.State["regex_matched"].([]string)[1]
|
||||
id = apiutils.Str2Int(ctx.State["regex_matched"].([]string)[2])
|
||||
illust = &pixiv.Illust{}
|
||||
type_ = ctx.State["regex_matched"].([]string)[1]
|
||||
id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64)
|
||||
)
|
||||
ctx.Send("少女祈祷中......")
|
||||
ctx.SendChain(message.Text("少女祈祷中......"))
|
||||
// 查询P站插图信息
|
||||
|
||||
if err := illust.IllustInfo(id); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
illust, err := pixiv.Works(id)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
// 下载插画
|
||||
if _, err := illust.PixivPicDown(PoolsCache.Path); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
if _, err := download(illust, POOL.Path); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
file := fmt.Sprintf("%s%d.jpg", PoolsCache.Path, illust.Pid)
|
||||
if id := ctx.Send(illust.DetailPic(file)); id == 0 {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", "可能被风控,发送失败"))
|
||||
// 发送到发送者
|
||||
if id := ctx.SendChain(message.Image(file(illust))); id == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控,发送失败"))
|
||||
return
|
||||
}
|
||||
// 添加插画到对应的数据库table
|
||||
if err := DB.Insert(type_, illust); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
if err := POOL.DB.Insert(type_, illust); err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
ctx.Send("添加成功")
|
||||
return
|
||||
})
|
||||
|
||||
zero.OnRegex(`^删除(.*?)(\d+)$`, FirstValueInList(PoolList), zero.SuperUserPermission).SetBlock(true).SetPriority(22).
|
||||
zero.OnRegex(`^删除(.*?)(\d+)$`, FirstValueInList(POOL.List), zero.SuperUserPermission).SetBlock(true).SetPriority(22).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
var (
|
||||
type_ = ctx.State["regex_matched"].([]string)[1]
|
||||
id = apiutils.Str2Int(ctx.State["regex_matched"].([]string)[2])
|
||||
id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64)
|
||||
)
|
||||
// 查询数据库
|
||||
if err := DB.Delete(type_, fmt.Sprintf("WHERE pid=%d", id)); err != nil {
|
||||
if err := POOL.DB.Delete(type_, fmt.Sprintf("WHERE pid=%d", id)); err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
return
|
||||
}
|
||||
@@ -143,36 +150,22 @@ func init() { // 插件主体
|
||||
})
|
||||
|
||||
// 查询数据库涩图数量
|
||||
zero.OnFullMatchGroup([]string{"setu -s", "setu --status", ">setu status"}).SetBlock(true).SetPriority(23).
|
||||
zero.OnFullMatchGroup([]string{">setu status"}).SetBlock(true).SetPriority(23).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
state := []string{"[SetuTime]"}
|
||||
for i := range PoolList {
|
||||
num, err := DB.Num(PoolList[i])
|
||||
for i := range POOL.List {
|
||||
num, err := POOL.DB.Num(POOL.List[i])
|
||||
if err != nil {
|
||||
num = 0
|
||||
}
|
||||
state = append(state, "\n")
|
||||
state = append(state, PoolList[i])
|
||||
state = append(state, POOL.List[i])
|
||||
state = append(state, ": ")
|
||||
state = append(state, fmt.Sprintf("%d", num))
|
||||
}
|
||||
ctx.Send(strings.Join(state, ""))
|
||||
return
|
||||
})
|
||||
// 开xml模式
|
||||
zero.OnFullMatchGroup([]string{"setu -x", "setu --xml", ">setu xml"}).SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
FORM = "XML"
|
||||
ctx.Send("[SetuTime] XML->ON")
|
||||
return
|
||||
})
|
||||
// 关xml模式
|
||||
zero.OnFullMatchGroup([]string{"setu -p", "setu --pic", ">setu pic"}).SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
FORM = "PIC"
|
||||
ctx.Send("[SetuTime] XML->OFF")
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
// FirstValueInList 判断正则匹配的第一个参数是否在列表中
|
||||
@@ -187,3 +180,80 @@ func FirstValueInList(list []string) zero.Rule {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Min 返回两数最小值
|
||||
func Min(a, b int) int {
|
||||
switch {
|
||||
default:
|
||||
return a
|
||||
case a > b:
|
||||
return b
|
||||
case a < b:
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
// Size 返回缓冲池指定类型的现有大小
|
||||
func (p *Pool) Size(type_ string) int {
|
||||
return len(p.Pool[type_])
|
||||
}
|
||||
|
||||
// IsFull 返回缓冲池指定类型是否已满
|
||||
func (p *Pool) IsFull(type_ string) bool {
|
||||
return len(p.Pool[type_]) >= p.Max
|
||||
}
|
||||
|
||||
// Push 向缓冲池插入一张图片
|
||||
func (p *Pool) Push(type_ string, illust *pixiv.Illust) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
p.Pool[type_] = append(p.Pool[type_], illust)
|
||||
}
|
||||
|
||||
// Push 在缓冲池拿出一张图片
|
||||
func (p *Pool) Pop(type_ string) (illust *pixiv.Illust) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
if p.Size(type_) == 0 {
|
||||
return
|
||||
}
|
||||
illust = p.Pool[type_][0]
|
||||
p.Pool[type_] = p.Pool[type_][1:]
|
||||
return
|
||||
}
|
||||
|
||||
func file(i *pixiv.Illust) string {
|
||||
filename := fmt.Sprint(i.Pid)
|
||||
pwd, _ := os.Getwd()
|
||||
filepath := pwd + `\` + POOL.Path + filename
|
||||
if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) {
|
||||
return `file:///` + filepath + ".jpg"
|
||||
}
|
||||
if _, err := os.Stat(filepath + ".png"); err == nil || os.IsExist(err) {
|
||||
return `file:///` + filepath + ".png"
|
||||
}
|
||||
if _, err := os.Stat(filepath + ".gif"); err == nil || os.IsExist(err) {
|
||||
return `file:///` + filepath + ".gif"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func download(i *pixiv.Illust, filedir string) (string, error) {
|
||||
filename := fmt.Sprint(i.Pid)
|
||||
filepath := filedir + filename
|
||||
if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) {
|
||||
return filepath + ".jpg", nil
|
||||
}
|
||||
if _, err := os.Stat(filepath + ".png"); err == nil || os.IsExist(err) {
|
||||
return filepath + ".png", nil
|
||||
}
|
||||
if _, err := os.Stat(filepath + ".gif"); err == nil || os.IsExist(err) {
|
||||
return filepath + ".gif", nil
|
||||
}
|
||||
// 下载最大分辨率为 1200 的图片
|
||||
link := i.ImageUrls
|
||||
link = strings.ReplaceAll(link, "img-original", "img-master")
|
||||
link = strings.ReplaceAll(link, "_p0", "_p0_master1200")
|
||||
// 下载
|
||||
return pixiv.Download(link, filedir, filename)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
// Sqlite 数据库对象
|
||||
@@ -20,7 +20,7 @@ type Sqlite struct {
|
||||
// 返回错误
|
||||
func (db *Sqlite) Create(table string, objptr interface{}) (err error) {
|
||||
if db.DB == nil {
|
||||
database, err := sql.Open("sqlite3", db.DBPath)
|
||||
database, err := sql.Open("sqlite", db.DBPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
|
||||
)
|
||||
|
||||
// PoolsCache 图片缓冲池
|
||||
type PoolsCache struct {
|
||||
Lock sync.Mutex
|
||||
Max int
|
||||
Path string
|
||||
Group int64
|
||||
Pool map[string][]*pixiv.Illust
|
||||
}
|
||||
|
||||
// NewPoolsCache 返回一个缓冲池对象
|
||||
func NewPoolsCache() *PoolsCache {
|
||||
return &PoolsCache{
|
||||
Max: 10,
|
||||
Path: "./data/SetuTime/cache/",
|
||||
Group: 1048452984,
|
||||
Pool: map[string][]*pixiv.Illust{},
|
||||
}
|
||||
}
|
||||
|
||||
// Size 返回缓冲池指定类型的现有大小
|
||||
func (p *PoolsCache) Size(type_ string) int {
|
||||
return len(p.Pool[type_])
|
||||
}
|
||||
|
||||
// IsFull 返回缓冲池指定类型是否已满
|
||||
func (p *PoolsCache) IsFull(type_ string) bool {
|
||||
return len(p.Pool[type_]) >= p.Max
|
||||
}
|
||||
|
||||
// Push 向缓冲池插入一张图片,返回错误
|
||||
func (p *PoolsCache) Push(type_ string, illust *pixiv.Illust) (err error) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
p.Pool[type_] = append(p.Pool[type_], illust)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Push 在缓冲池拿出一张图片,返回错误
|
||||
func (p *PoolsCache) Pop(type_ string) (illust *pixiv.Illust) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
if p.Size(type_) == 0 {
|
||||
return
|
||||
}
|
||||
illust = p.Pool[type_][0]
|
||||
p.Pool[type_] = p.Pool[type_][1:]
|
||||
return
|
||||
}
|
||||
|
||||
// Push 在缓冲池拿出一张图片,返回指定格式CQ码
|
||||
func (p *PoolsCache) GetOnePic(type_ string, form string) string {
|
||||
var (
|
||||
illust = p.Pop(type_)
|
||||
file = fmt.Sprintf("%s%d.jpg", p.Path, illust.Pid)
|
||||
)
|
||||
switch form {
|
||||
case "XML":
|
||||
return illust.BigPic(file)
|
||||
case "DETAIL":
|
||||
return illust.DetailPic(file)
|
||||
default:
|
||||
return illust.NormalPic(file)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package utils
|
||||
|
||||
var (
|
||||
XML = true // 图片XML开关,默认开启
|
||||
)
|
||||
Reference in New Issue
Block a user