模块化rand_image

This commit is contained in:
fumiama 2021-07-02 17:19:41 +08:00
parent 956c741c38
commit 60dbd0d579
8 changed files with 17 additions and 224 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
data/SetuTime/cache
data/SetuTime/search
data/manager
.idea/
.idea/
.DS_Store

View File

@ -79,7 +79,7 @@
- 搜图 `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_saucenao"`
- [x] 以图搜图|搜索图片|以图识图[图片]
- [x] 搜图[P站图片ID]
- 简易随机图片(调用url) `github.com/FloatTech/ZeroBot-Plugin/plugin_rand_image`
- 随机图片与点评 `github.com/FloatTech/ZeroBot-Plugin/plugin_rand_image`
- [x] 随机图片
- [x] 直接随机无r18检测后果自负
- [x] 设置随机图片网址[url]

5
go.mod
View File

@ -4,14 +4,15 @@ go 1.16
require (
github.com/FloatTech/AnimeAPI v0.0.0-20210619112022-a8fdd4fefc07
github.com/FloatTech/ZeroBot-ACGImage v1.1.2
github.com/FloatTech/ZeroBot-Plugin-Timer v1.2.2
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/robfig/cron v1.2.0
github.com/shirou/gopsutil v3.21.5+incompatible
github.com/shirou/gopsutil v3.21.6+incompatible
github.com/sirupsen/logrus v1.8.1
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/tidwall/gjson v1.8.0
github.com/tidwall/gjson v1.8.1
github.com/tklauser/go-sysconf v0.3.6 // indirect
github.com/wdvxdr1123/ZeroBot v1.2.1
modernc.org/sqlite v1.11.1

9
go.sum
View File

@ -1,5 +1,7 @@
github.com/FloatTech/AnimeAPI v0.0.0-20210619112022-a8fdd4fefc07 h1:7fBPLLoZysaa9PI9ApgzyVMrMLMWBam273YgKwy7Ddg=
github.com/FloatTech/AnimeAPI v0.0.0-20210619112022-a8fdd4fefc07/go.mod h1:GYrQD70HvBzry1XiZ1tHFNOsOqRkR+ao4XGYZqV4I9c=
github.com/FloatTech/ZeroBot-ACGImage v1.1.2 h1:Q+FwOb1vCCYG2B7hPU0uBMzWwB85eVlC0pWRJZ+rBCQ=
github.com/FloatTech/ZeroBot-ACGImage v1.1.2/go.mod h1:CtEYnFTjgzH4v1jaowyyjBmjSKK3p8WPXExfGnVmx1E=
github.com/FloatTech/ZeroBot-Plugin-Timer v1.2.2 h1:I7RSCsJVT6elehdaSiWB4n96y06fQxMsvAG494S5Od0=
github.com/FloatTech/ZeroBot-Plugin-Timer v1.2.2/go.mod h1:femu/wVuqVSN/rg209qksMx8/2OKUF8l1PZCNg4RwVA=
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
@ -53,8 +55,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/shirou/gopsutil v3.21.5+incompatible h1:OloQyEerMi7JUrXiNzy8wQ5XN+baemxSl12QgIzt0jc=
github.com/shirou/gopsutil v3.21.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v3.21.6+incompatible h1:mmZtAlWSd8U2HeRTjswbnDLPxqsEoK01NK+GZ1P+nEM=
github.com/shirou/gopsutil v3.21.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
@ -67,8 +69,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/gjson v1.8.1 h1:8j5EE9Hrh3l9Od1OIEDAb7IpezNA20UdRngNAj5N0WU=
github.com/tidwall/gjson v1.8.1/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
// 注:以下插件均可通过前面加 // 注释,注释后停用并不加载插件
// 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用
@ -19,8 +20,6 @@ import (
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_shindan" // 测定
//_ "github.com/FloatTech/ZeroBot-ACGImage" //简易随机图片
// b站相关
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili" // 查询b站用户信息
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_diana" // 嘉心糖发病
@ -28,7 +27,7 @@ import (
// 二次元图片
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_image_finder" // 关键字搜图
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_lolicon" // lolicon 随机图片
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_rand_image" // 随机图片
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_rand_image" // 随机图片与点评
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_saucenao" // 以图搜图
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime" // 来份涩图
@ -61,7 +60,7 @@ func main() {
zero.Run(zero.Config{
NickName: []string{"椛椛", "ATRI", "atri", "亚托莉", "アトリ"},
CommandPrefix: "/",
SuperUsers: []string{"213864964"}, // 必须修改,否则无权限
SuperUsers: os.Args[1:], // 必须修改,否则无权限
Driver: []zero.Driver{
&driver.WSClient{
Url: "ws://127.0.0.1:6700",

View File

@ -1,111 +0,0 @@
package plugin_rand_image
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]我好啦!"))
}
}
}

View File

@ -1,105 +1,5 @@
package plugin_rand_image
import (
"fmt"
"strings"
"time"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
_ "github.com/FloatTech/ZeroBot-ACGImage" //简易随机图片
)
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("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("评价图片").SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) {
// 匹配图片
rule := func() zero.Rule {
return func(ctx *zero.Ctx) bool {
var urls = []string{}
for _, elem := range ctx.Event.Message {
if elem.Type == "image" {
urls = append(urls, elem.Data["url"])
}
}
if len(urls) > 0 {
ctx.State["image_url"] = urls
return true
}
return false
}
}
// 索取图片
ctx.SendChain(message.Text("请发送一张图片"))
next := zero.NewFutureEvent("message", 999, false, zero.CheckUser(ctx.Event.UserID), rule())
recv, cancel := next.Repeat()
select {
case <-time.After(time.Second * 120):
return
case e := <-recv:
cancel()
newCtx := &zero.Ctx{Event: e, State: zero.State{}}
if rule()(newCtx) {
ctx.State["image_url"] = newCtx.State["image_url"]
}
}
if ctx.Event.GroupID > 0 {
ctx.Send("少女祈祷中......")
for _, pic := range ctx.State["image_url"].([]string) {
fmt.Println(pic)
Classify(ctx, pic, true)
}
}
return
})
}

View File

@ -38,7 +38,7 @@ func NewPools() *Pool {
Pool: map[string][]*pixiv.Illust{},
Form: 0,
}
err := os.MkdirAll(cache.Path, 0644)
err := os.MkdirAll(cache.Path, 0755)
if err != nil {
panic(err)
}