分离搜图与随机图(随机图需要cgo而搜图不用)

This commit is contained in:
fumiama 2021-06-04 13:45:50 +08:00
parent 6e3f395f07
commit 33dc16956d
10 changed files with 82 additions and 70 deletions

View File

@ -1,4 +1,4 @@
package utils package pixiv
import ( import (
"crypto/md5" "crypto/md5"

View File

@ -1,4 +1,4 @@
package utils package pixiv
import ( import (
"crypto/tls" "crypto/tls"
@ -92,3 +92,46 @@ func (this *Illust) IllustInfo(id int64) (err error) {
this.UserName = json.Get("userName").Str this.UserName = json.Get("userName").Str
return nil return nil
} }
// BigPic 返回一张XML大图CQ码
func (i *Illust) BigPic(file string) string {
var hash = PicHash(file)
return fmt.Sprintf(`[CQ:xml,data=<?xml version='1.0'
encoding='UTF-8' standalone='yes' ?><msg serviceID="5"
templateID="12345" action="" brief="不够涩!"
sourceMsgId="0" url="" flag="0" adverSign="0" multiMsgFlag="0">
<item layout="0" advertiser_id="0" aid="0"><image uuid="%s.jpg" md5="%s"
GroupFiledid="2235033681" filesize="81322" local_path="%s.jpg"
minWidth="200" minHeight="200" maxWidth="500" maxHeight="1000" />
</item><source name="%s⭐(id:%d author:%s)" icon=""
action="" appid="-1" /></msg>]`,
hash,
hash,
hash,
i.Title,
i.Pid,
i.UserName,
)
}
// NormalPic 返回一张普通图CQ码
func (i *Illust) NormalPic(file string) string {
return fmt.Sprintf(`[CQ:image,file=file:///%s]`, file)
}
// DetailPic 返回一张带详细信息的图片CQ码
func (i *Illust) DetailPic(file string) string {
return fmt.Sprintf(`[SetuTime] %s
标题%s
插画ID%d
画师%s
画师ID%d
直链https://pixivel.moe/detail?id=%d`,
i.NormalPic(file),
i.Title,
i.Pid,
i.UserName,
i.UserId,
i.Pid,
)
}

View File

@ -1,4 +1,4 @@
package utils package pixiv
import ( import (
"os" "os"

View File

@ -4,4 +4,4 @@ go env -w GO111MODULE=auto
go mod tidy go mod tidy
export CCBIN=~/openwrt_with_lean_packages/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/mips-openwrt-linux-musl-gcc export CCBIN=~/openwrt_with_lean_packages/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/mips-openwrt-linux-musl-gcc
export CXXBIN=~/openwrt_with_lean_packages/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/mips-openwrt-linux-musl-g++ export CXXBIN=~/openwrt_with_lean_packages/staging_dir/toolchain-mips_24kc_gcc-8.4.0_musl/bin/mips-openwrt-linux-musl-g++
GOOS=linux GOARCH=mips GOMIPS=softfloat CGO_ENABLED=1 CC=${CCBIN} CXX=${CXXBIN} go build -ldflags "-s -w" -o zerobot GOOS=linux GOARCH=mips GOMIPS=softfloat CGO_ENABLED=0 CC=${CCBIN} CXX=${CXXBIN} go build -ldflags "-s -w" -o zerobot

View File

@ -24,6 +24,7 @@ import (
// 娱乐类 // 娱乐类
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/music" // 点歌 _ "github.com/Yiwen-Chan/ZeroBot-Plugin/music" // 点歌
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/picsearcher" // 搜图
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/randimg" //简易随机图片 _ "github.com/Yiwen-Chan/ZeroBot-Plugin/randimg" //简易随机图片
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime" // 涩图 _ "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime" // 涩图
_ "github.com/Yiwen-Chan/ZeroBot-Plugin/shindan" // 测定 _ "github.com/Yiwen-Chan/ZeroBot-Plugin/shindan" // 测定

View File

@ -1,4 +1,4 @@
package setutime package picsearcher
import ( import (
"fmt" "fmt"
@ -9,17 +9,24 @@ import (
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
utils "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils" "github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
utils "github.com/Yiwen-Chan/ZeroBot-Plugin/picsearcher/utils"
)
var (
BOTPATH = pixiv.PathExecute() // 当前bot运行目录
DATAPATH = BOTPATH + "data/SetuTime/" // 数据目录
CACHEPATH = DATAPATH + "cache/" // 缓冲图片路径
) )
func init() { // 插件主体 func init() { // 插件主体
// 根据PID搜图 // 根据PID搜图
zero.OnRegex(`^搜图(\d+)$`).SetBlock(true).SetPriority(30). zero.OnRegex(`^搜图(\d+)$`).SetBlock(true).SetPriority(30).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
id := utils.Str2Int(ctx.State["regex_matched"].([]string)[1]) id := pixiv.Str2Int(ctx.State["regex_matched"].([]string)[1])
ctx.Send("少女祈祷中......") ctx.Send("少女祈祷中......")
// 获取P站插图信息 // 获取P站插图信息
illust := &utils.Illust{} illust := &pixiv.Illust{}
if err := illust.IllustInfo(id); err != nil { if err := illust.IllustInfo(id); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err)) ctx.Send(fmt.Sprintf("ERROR: %v", err))
return return

View File

@ -6,6 +6,7 @@ import (
"net/url" "net/url"
"strings" "strings"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
xpath "github.com/antchfx/htmlquery" xpath "github.com/antchfx/htmlquery"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
) )
@ -71,12 +72,12 @@ func Ascii2dSearch(pic string) (message.Message, error) {
if link == "" || index == -1 { if link == "" || index == -1 {
return nil, fmt.Errorf("Ascii2d not found") return nil, fmt.Errorf("Ascii2d not found")
} }
var id = Str2Int(link[index+1:]) var id = pixiv.Str2Int(link[index+1:])
if id == 0 { if id == 0 {
return nil, fmt.Errorf("convert to pid error") return nil, fmt.Errorf("convert to pid error")
} }
// 根据PID查询插图信息 // 根据PID查询插图信息
var illust = &Illust{} var illust = &pixiv.Illust{}
if err := illust.IllustInfo(id); err != nil { if err := illust.IllustInfo(id); err != nil {
return nil, err return nil, err
} }

View File

@ -9,13 +9,14 @@ import (
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
"github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils" "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils"
) )
var limit = rate.NewManager(time.Minute*1, 5) var limit = rate.NewManager(time.Minute*1, 5)
var ( var (
BOTPATH = utils.PathExecute() // 当前bot运行目录 BOTPATH = pixiv.PathExecute() // 当前bot运行目录
DATAPATH = BOTPATH + "data/SetuTime/" // 数据目录 DATAPATH = BOTPATH + "data/SetuTime/" // 数据目录
DBPATH = DATAPATH + "SetuTime.db" // 数据库路径 DBPATH = DATAPATH + "SetuTime.db" // 数据库路径
@ -33,11 +34,11 @@ func init() {
PoolsCache.Group = CACHEGROUP // 图片缓冲群 PoolsCache.Group = CACHEGROUP // 图片缓冲群
PoolsCache.Path = CACHEPATH // 缓冲图片路径 PoolsCache.Path = CACHEPATH // 缓冲图片路径
utils.CreatePath(DBPATH) pixiv.CreatePath(DBPATH)
utils.CreatePath(CACHEPATH) pixiv.CreatePath(CACHEPATH)
for i := range PoolList { for i := range PoolList {
if err := DB.Create(PoolList[i], &utils.Illust{}); err != nil { if err := DB.Create(PoolList[i], &pixiv.Illust{}); err != nil {
panic(err) panic(err)
} }
} }
@ -53,9 +54,9 @@ func init() { // 插件主体
var type_ = ctx.State["regex_matched"].([]string)[1] var type_ = ctx.State["regex_matched"].([]string)[1]
// 补充池子 // 补充池子
go func() { go func() {
times := utils.Min(PoolsCache.Max-PoolsCache.Size(type_), 2) times := pixiv.Min(PoolsCache.Max-PoolsCache.Size(type_), 2)
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
illust := &utils.Illust{} illust := &pixiv.Illust{}
// 查询出一张图片 // 查询出一张图片
if err := DB.Select(type_, illust, "ORDER BY RANDOM() limit 1"); err != nil { if err := DB.Select(type_, illust, "ORDER BY RANDOM() limit 1"); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err)) ctx.Send(fmt.Sprintf("ERROR: %v", err))
@ -96,8 +97,8 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
var ( var (
type_ = ctx.State["regex_matched"].([]string)[1] type_ = ctx.State["regex_matched"].([]string)[1]
id = utils.Str2Int(ctx.State["regex_matched"].([]string)[2]) id = pixiv.Str2Int(ctx.State["regex_matched"].([]string)[2])
illust = &utils.Illust{} illust = &pixiv.Illust{}
) )
ctx.Send("少女祈祷中......") ctx.Send("少女祈祷中......")
// 查询P站插图信息 // 查询P站插图信息
@ -129,7 +130,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
var ( var (
type_ = ctx.State["regex_matched"].([]string)[1] type_ = ctx.State["regex_matched"].([]string)[1]
id = utils.Str2Int(ctx.State["regex_matched"].([]string)[2]) id = pixiv.Str2Int(ctx.State["regex_matched"].([]string)[2])
) )
// 查询数据库 // 查询数据库
if err := DB.Delete(type_, fmt.Sprintf("WHERE pid=%d", id)); err != nil { if err := DB.Delete(type_, fmt.Sprintf("WHERE pid=%d", id)); err != nil {

View File

@ -3,6 +3,8 @@ package utils
import ( import (
"fmt" "fmt"
"sync" "sync"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
) )
// PoolsCache 图片缓冲池 // PoolsCache 图片缓冲池
@ -11,7 +13,7 @@ type PoolsCache struct {
Max int Max int
Path string Path string
Group int64 Group int64
Pool map[string][]*Illust Pool map[string][]*pixiv.Illust
} }
// NewPoolsCache 返回一个缓冲池对象 // NewPoolsCache 返回一个缓冲池对象
@ -20,7 +22,7 @@ func NewPoolsCache() *PoolsCache {
Max: 10, Max: 10,
Path: "./data/SetuTime/cache/", Path: "./data/SetuTime/cache/",
Group: 1048452984, Group: 1048452984,
Pool: map[string][]*Illust{}, Pool: map[string][]*pixiv.Illust{},
} }
} }
@ -35,7 +37,7 @@ func (p *PoolsCache) IsFull(type_ string) bool {
} }
// Push 向缓冲池插入一张图片,返回错误 // Push 向缓冲池插入一张图片,返回错误
func (p *PoolsCache) Push(type_ string, illust *Illust) (err error) { func (p *PoolsCache) Push(type_ string, illust *pixiv.Illust) (err error) {
p.Lock.Lock() p.Lock.Lock()
defer p.Lock.Unlock() defer p.Lock.Unlock()
p.Pool[type_] = append(p.Pool[type_], illust) p.Pool[type_] = append(p.Pool[type_], illust)
@ -43,7 +45,7 @@ func (p *PoolsCache) Push(type_ string, illust *Illust) (err error) {
} }
// Push 在缓冲池拿出一张图片,返回错误 // Push 在缓冲池拿出一张图片,返回错误
func (p *PoolsCache) Pop(type_ string) (illust *Illust) { func (p *PoolsCache) Pop(type_ string) (illust *pixiv.Illust) {
p.Lock.Lock() p.Lock.Lock()
defer p.Lock.Unlock() defer p.Lock.Unlock()
if p.Size(type_) == 0 { if p.Size(type_) == 0 {
@ -69,46 +71,3 @@ func (p *PoolsCache) GetOnePic(type_ string, form string) string {
return illust.NormalPic(file) return illust.NormalPic(file)
} }
} }
// BigPic 返回一张XML大图CQ码
func (i *Illust) BigPic(file string) string {
var hash = PicHash(file)
return fmt.Sprintf(`[CQ:xml,data=<?xml version='1.0'
encoding='UTF-8' standalone='yes' ?><msg serviceID="5"
templateID="12345" action="" brief="不够涩!"
sourceMsgId="0" url="" flag="0" adverSign="0" multiMsgFlag="0">
<item layout="0" advertiser_id="0" aid="0"><image uuid="%s.jpg" md5="%s"
GroupFiledid="2235033681" filesize="81322" local_path="%s.jpg"
minWidth="200" minHeight="200" maxWidth="500" maxHeight="1000" />
</item><source name="%s⭐(id:%d author:%s)" icon=""
action="" appid="-1" /></msg>]`,
hash,
hash,
hash,
i.Title,
i.Pid,
i.UserName,
)
}
// NormalPic 返回一张普通图CQ码
func (i *Illust) NormalPic(file string) string {
return fmt.Sprintf(`[CQ:image,file=file:///%s]`, file)
}
// DetailPic 返回一张带详细信息的图片CQ码
func (i *Illust) DetailPic(file string) string {
return fmt.Sprintf(`[SetuTime] %s
标题%s
插画ID%d
画师%s
画师ID%d
直链https://pixivel.moe/detail?id=%d`,
i.NormalPic(file),
i.Title,
i.Pid,
i.UserName,
i.UserId,
i.Pid,
)
}