From 8c40293fed0d5bd35422039a83d956d6cd9fa974 Mon Sep 17 00:00:00 2001 From: Kanri Date: Wed, 23 Feb 2022 16:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=20=E6=91=B8=E9=B1=BC=E4=BA=BA=E6=97=A5=E5=8E=86=20(#1?= =?UTF-8?q?33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ 新增插件 摸鱼人日历 * ✨ 新增插件 摸鱼人日历 * ✏️ 优化插件 摸鱼人日历 --- README.md | 3 + main.go | 1 + plugin_moyu_calendar/calendar.go | 147 +++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 plugin_moyu_calendar/calendar.go diff --git a/README.md b/README.md index b660522e..6a868396 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,9 @@ zerobot [-h] [-t token] [-u url] [-n nickname] [-p prefix] [-d|w] [-g 监听地 - **摸鱼** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu"` - [x] /启用 moyu - [x] /禁用 moyu +- **摸鱼人日历** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu_calendar"` + - [x] /启用 moyucalendar + - [x] /禁用 moyucalendar - **涩图** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime"` - [x] 来份[涩图/二次元/风景/车万] - [x] 添加[涩图/二次元/风景/车万][P站图片ID] diff --git a/main.go b/main.go index 2b38fba0..23e38d14 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,7 @@ import ( _ "github.com/FloatTech/ZeroBot-Plugin/plugin_lolicon" // lolicon 随机图片 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft" // MCSManager _ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu" // 摸鱼 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu_calendar" // 摸鱼人日历 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nativesetu" // 本地涩图 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nativewife" // 本地老婆 diff --git a/plugin_moyu_calendar/calendar.go b/plugin_moyu_calendar/calendar.go new file mode 100644 index 00000000..f56ee503 --- /dev/null +++ b/plugin_moyu_calendar/calendar.go @@ -0,0 +1,147 @@ +// Package moyucalendar 摸鱼人日历 +package moyucalendar + +import ( + "bufio" + "errors" + "io" + "io/ioutil" + "net/http" + "regexp" + "strings" + "time" + + control "github.com/FloatTech/zbputils/control" + "github.com/fumiama/cron" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + + "github.com/FloatTech/zbputils/control/order" +) + +func init() { + control.Register("moyucalendar", order.AcquirePrio(), &control.Options{ + DisableOnDefault: true, + Help: "摸鱼人日历\n" + + "- /启用 moyucalendar\n" + + "- /禁用 moyucalendar", + }) + + // 定时任务每天8点执行一次 + c := cron.New() + _, err := c.AddFunc("* 8 * * *", func() { + m, ok := control.Lookup("moyucalendar") + if !ok { + return + } + image, err := crew() + if err != nil { + return + } + zero.RangeBot(func(id int64, ctx *zero.Ctx) bool { + for _, g := range ctx.GetGroupList().Array() { + grp := g.Get("group_id").Int() + if m.IsEnabledIn(grp) { + ctx.SendGroupMessage(grp, []message.MessageSegment{message.Image(image)}) + } + } + return true + }) + }) + if err == nil { + c.Start() + } +} + +var newest = regexp.MustCompile(`uigs="account_article_0" href="(/link.+?)">`) +var weixin = regexp.MustCompile(`url \+= '(.+)';`) +var calendar = regexp.MustCompile(`data-src="(.{0,300})" data-type="png" data-w="540"`) + +func crew() (string, error) { + client := &http.Client{} + req, err := http.NewRequest("GET", "https://weixin.sogou.com/weixin?type=1&s_from=input&query=%E6%91%B8%E9%B1%BC%E4%BA%BA%E6%97%A5%E5%8E%86", nil) + if err != nil { + return "", err + } + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return "", errors.New("status not ok") + } + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + match := newest.FindStringSubmatch(string(b)) + if len(match) < 2 { + return "", errors.New("newest not found") + } + var link = "https://weixin.sogou.com" + match[1] + reqa, err := http.NewRequest("GET", link, nil) + if err != nil { + return "", err + } + reqa.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") + var cookies = make([]string, 0, 4) + for _, cookie := range resp.Cookies() { + if cookie.Name != "ABTEST" && cookie.Name != "SNUID" && cookie.Name != "IPLOC" && cookie.Name != "SUID" { + continue + } + cookies = append(cookies, cookie.Name+"="+cookie.Value) + } + reqa.Header.Set("Cookie", strings.Join(cookies, "; ")) + respa, err := client.Do(reqa) + if err != nil { + return "", err + } + defer respa.Body.Close() + if respa.StatusCode != http.StatusOK { + return "", errors.New("status not ok") + } + br := bufio.NewReader(respa.Body) + var weixinurl = make([]string, 0) + for { + b, _, err := br.ReadLine() + if err == io.EOF { + break + } + if err != nil { + return "", err + } + matcha := weixin.FindStringSubmatch(string(b)) + if len(matcha) < 2 { + continue + } + weixinurl = append(weixinurl, strings.ReplaceAll(matcha[1], "@", "")) + } + if len(weixinurl) == 0 { + return "", errors.New("weixin url not found") + } + reqw, err := http.NewRequest("GET", strings.Join(weixinurl, ""), nil) + reqa.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") + if err != nil { + return "", err + } + respw, err := client.Do(reqw) + if err != nil { + return "", err + } + defer respw.Body.Close() + if respw.StatusCode != http.StatusOK { + return "", errors.New("status not ok") + } + bw, _ := ioutil.ReadAll(respw.Body) + today := regexp.MustCompile(time.Now().Format("2006-01-02")) + if !today.Match(bw) { + return "", errors.New("calendar not found") + } + matchw := calendar.FindStringSubmatch(string(bw)) + if len(matchw) < 2 { + return "", errors.New("calendar not found") + } + return matchw[1], nil +}