mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
feat:增加煎蛋网无聊图 (#115)
* feat:添加煎蛋网无聊图 * fix:修lint * fix:修改model和名称 * fix:删除不要的驱动 * fix:修改日志错误的地方 * fix:修改权限 * fix:去掉数据库 Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
This commit is contained in:
74
plugin_jandan/cron.go
Normal file
74
plugin_jandan/cron.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package jandan
|
||||
|
||||
import (
|
||||
"github.com/FloatTech/zbputils/process"
|
||||
"github.com/antchfx/htmlquery"
|
||||
"github.com/fumiama/cron"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
chanPicture = make(chan string, 100000)
|
||||
pageTotal int
|
||||
)
|
||||
|
||||
func init() {
|
||||
go func() {
|
||||
process.SleepAbout1sTo2s()
|
||||
scorePicture()
|
||||
log.Println("[jandan/cron] 开启jandan数据库日常更新")
|
||||
jandanDaily()
|
||||
}()
|
||||
travelWebpage()
|
||||
}
|
||||
|
||||
func jandanDaily() {
|
||||
c := cron.New()
|
||||
_, err := c.AddFunc("10 4 * * *", func() { travelWebpage() })
|
||||
if err != nil {
|
||||
log.Errorln("定时任务有错误:", err)
|
||||
} else {
|
||||
log.Println("开启jandan数据库定时任务")
|
||||
c.Start()
|
||||
}
|
||||
}
|
||||
|
||||
func travelWebpage() {
|
||||
pictureList = pictureList[0:0]
|
||||
webpageURL := jandanPictureURL
|
||||
doc, err := htmlquery.LoadURL(webpageURL)
|
||||
if err != nil {
|
||||
log.Errorln("[jandan]:", err)
|
||||
}
|
||||
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 {
|
||||
log.Errorln("[jandan]:", err)
|
||||
}
|
||||
for i := 0; i < pageTotal; i++ {
|
||||
doc, err = htmlquery.LoadURL(webpageURL)
|
||||
if err != nil {
|
||||
log.Errorln("[jandan]:", err)
|
||||
}
|
||||
picList, err := htmlquery.QueryAll(doc, "//*[@class='view_img_link']")
|
||||
if err != nil {
|
||||
log.Errorln("[jandan]:", err)
|
||||
}
|
||||
if len(picList) != 0 {
|
||||
for _, v := range picList {
|
||||
chanPicture <- "https:" + v.Attr[0].Val
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func scorePicture() {
|
||||
for pictureURL := range chanPicture {
|
||||
pictureList = append(pictureList, pictureURL)
|
||||
}
|
||||
}
|
||||
39
plugin_jandan/jandan.go
Normal file
39
plugin_jandan/jandan.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Package jandan 煎蛋网无聊图
|
||||
package jandan
|
||||
|
||||
import (
|
||||
"github.com/FloatTech/ZeroBot-Plugin/order"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
)
|
||||
|
||||
const (
|
||||
jandanPictureURL = "http://jandan.net/pic"
|
||||
)
|
||||
|
||||
var (
|
||||
pictureList []string
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine := control.Register("jandan", order.PrioJandan, &control.Options{
|
||||
DisableOnDefault: false,
|
||||
Help: "煎蛋网无聊图\n- 来份屌图\n- 更新屌图\n",
|
||||
})
|
||||
|
||||
engine.OnFullMatch("来份屌图").SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
ctx.SendChain(message.Image(pictureList[rand.Intn(len(pictureList))]))
|
||||
})
|
||||
|
||||
engine.OnFullMatch("更新屌图", zero.SuperUserPermission).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
ctx.Send("少女更新中......")
|
||||
travelWebpage()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user