ZeroBot-Plugin/plugin/jandan/jandan.go
莫思潋 87e3297904
定制退群提醒+调整煎蛋网无聊图触发 (#166)
* 优化在两个命令中使用空格分隔的体验
- fortune的设置底图功能
- b14的加密功能

* 优化四个插件中使用空格分隔的体验
- 加密
- 哔哩哔哩推送
- 藏头诗
- 运势

* 优化并修正了上一个commit
- 加上了因为复制粘贴疏忽又没有注意测试遗漏的`?`
- 调整藏头诗和加密的正则触发,使其不必多此一举
- 删去了未被发现的测试代码

* - 删去了遗漏的Trim

* 优化了更多插件中使用空格的体验
- 优化了music bilibili image_finder 中使用空格的体验
- 补上了plugin_bilibili中未实现的vup开头触发
- 为plugin_bilibili_parse输出的消息加上一个换行符,优化排版

* 小调整

- 考虑到屌字既难打又有碍观瞻,改为正则匹配`[屌|弔|吊]图`
- 增加了退群提醒的定制

* - 修复退群提醒本身忘记修改了的问题
2022-03-22 12:31:44 +08:00

109 lines
2.9 KiB
Go

// Package jandan 煎蛋网无聊图
package jandan
import (
"fmt"
"hash/crc64"
"regexp"
"strconv"
"github.com/FloatTech/zbputils/binary"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/file"
"github.com/antchfx/htmlquery"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/control/order"
)
const (
api = "http://jandan.net/pic"
)
func init() {
engine := control.Register("jandan", order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "煎蛋网无聊图\n- 来份[屌|弔|吊]图\n- 更新[屌|弔|吊]图\n",
PublicDataFolder: "Jandan",
})
go func() {
dbpath := engine.DataFolder()
db.DBPath = dbpath + "pics.db"
_, _ = file.GetLazyData(db.DBPath, false, false)
err := db.Create("picture", &picture{})
if err != nil {
panic(err)
}
n, err := db.Count("picture")
if err != nil {
panic(err)
}
logrus.Printf("[jandan]读取%d张图片", n)
}()
engine.OnRegex(`来份[屌|弔|吊]图`).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
u, err := getRandomPicture()
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
ctx.SendChain(message.Image(u))
})
engine.OnRegex(`更新[屌|弔|吊]图`, zero.SuperUserPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
ctx.Send("少女更新中...")
webpageURL := api
doc, err := htmlquery.LoadURL(webpageURL)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
re := regexp.MustCompile(`\d+`)
pageTotal, err := strconv.Atoi(re.FindString(htmlquery.FindOne(doc, "//*[@id='comments']/div[2]/div/span[@class='current-comment-page']/text()").Data))
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
LOOP:
for i := 0; i < pageTotal; i++ {
logrus.Infoln("[jandan]", fmt.Sprintf("处理第%d/%d页...", i, pageTotal))
doc, err = htmlquery.LoadURL(webpageURL)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
picList, err := htmlquery.QueryAll(doc, "//*[@class='view_img_link']")
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if len(picList) != 0 {
for _, v := range picList {
u := "https:" + v.Attr[0].Val
i := crc64.Checksum(binary.StringToBytes(u), crc64.MakeTable(crc64.ISO))
mu.RLock()
ok := db.CanFind("picture", "where id="+strconv.FormatUint(i, 10))
mu.RUnlock()
if !ok {
mu.Lock()
_ = db.Insert("picture", &picture{ID: i, URL: u})
mu.Unlock()
} else {
// 开始重复,说明之后都是重复
break LOOP
}
}
}
if i != pageTotal-1 {
webpageURL = "https:" + htmlquery.FindOne(doc, "//*[@id='comments']/div[@class='comments']/div[@class='cp-pagenavi']/a[@class='previous-comment-page']").Attr[1].Val
}
}
ctx.Send("更新完成!")
})
}