From 98699da802242b98af3dcf24c3c2b79034d6b2d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=B2=90=E9=A6=A8?= <70338354+LingMuXing@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:04:55 +0800 Subject: [PATCH 1/4] Update calendar.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更换API方式 --- plugin/moyu_calendar/calendar.go | 230 ++++++++----------------------- 1 file changed, 54 insertions(+), 176 deletions(-) diff --git a/plugin/moyu_calendar/calendar.go b/plugin/moyu_calendar/calendar.go index 883581581a..f25d085423 100644 --- a/plugin/moyu_calendar/calendar.go +++ b/plugin/moyu_calendar/calendar.go @@ -2,81 +2,43 @@ package moyucalendar import ( - "bufio" - "errors" + "encoding/base64" "fmt" - "io" + "io/ioutil" "net/http" - "net/url" - "regexp" - "strings" "time" - control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/control/order" "github.com/FloatTech/zbputils/process" + "github.com/wdvxdr1123/ZeroBot/utils/helper" + zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" - - xpath "github.com/antchfx/htmlquery" ) -var ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" -var weixin = regexp.MustCompile(`url \+= '(.+)';`) - -var client = &http.Client{} - -func init() { - control.Register("moyucalendar", order.AcquirePrio(), &control.Options{ - DisableOnDefault: true, - Help: "摸鱼人日历\n" + - "- /启用 moyucalendar\n" + - "- /禁用 moyucalendar", - }).OnFullMatch("摸鱼人日历").SetBlock(true). +func init() { // 插件主体 + engine := control.Register("moyucalendar", order.AcquirePrio(), &control.Options{ + DisableOnDefault: false, + Help: "moyu_calendar\n" + + "- 日历]", + }) + engine.OnFullMatch("日历").SetBlock(true). Handle(func(ctx *zero.Ctx) { - title := fmt.Sprintf("摸鱼人日历 %d月%d日", time.Now().Month(), time.Now().Day()) - sg, cookies, err := sougou(title, "摸鱼人日历", ua) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - wx, err := redirect(sg, cookies, ua) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - image, err := calendar(wx, ua) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - ctx.SendChain(message.Image(image)) + a := rili() + ctx.SendChain(message.Image("base64://" + helper.BytesToString(a))) }) - - // 定时任务每天8点30分执行一次 _, err := process.CronTab.AddFunc("30 8 * * *", func() { m, ok := control.Lookup("moyucalendar") if !ok { return } - title := fmt.Sprintf("摸鱼人日历 %d月%d日", time.Now().Month(), time.Now().Day()) - sg, cookies, err := sougou(title, "摸鱼人日历", ua) - if err != nil { - return - } - wx, err := redirect(sg, cookies, ua) - if err != nil { - return - } - image, err := calendar(wx, ua) - if err != nil { - return - } + a := rili() 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.Message{message.Image(image)}) + ctx.SendGroupMessage(grp, message.Message{message.Image("base64://" + helper.BytesToString(a))}) process.SleepAbout1sTo2s() } } @@ -88,139 +50,55 @@ func init() { } } -func sougou(title, publisher, ua string) (string, []*http.Cookie, error) { - u, _ := url.Parse("https://weixin.sogou.com/weixin") - u.RawQuery = url.Values{ - "type": []string{"2"}, - "s_from": []string{"input"}, - "query": []string{title}, - }.Encode() - req, err := http.NewRequest("GET", u.String(), nil) - if err != nil { - return "", nil, err - } - req.Header.Set("User-Agent", ua) - resp, err := client.Do(req) - if err != nil { - return "", nil, err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return "", nil, errors.New("status not ok") - } - // 解析XPATH - doc, err := xpath.Parse(resp.Body) - if err != nil { - return "", nil, err - } - // 取出每个返回的结果 - list := xpath.Find(doc, `//*[@class="news-list"]/li/div[@class="txt-box"]`) - if len(list) == 0 { - return "", nil, errors.New("sougou result not found") - } - var match string - for i := range list { - account := xpath.FindOne(list[i], `//div[@class="s-p"]/a[@class="account"]`) - if account == nil { - continue - } - if xpath.InnerText(account) != publisher { - continue - } - target := xpath.FindOne(list[i], `//h3/a[@target="_blank"]`) - if target == nil { - continue - } - match = xpath.SelectAttr(target, "href") - break - } - if match == "" { - return "", nil, errors.New("sougou result not found") - } - return "https://weixin.sogou.com" + match, resp.Cookies(), nil +func rili() []byte { + url := "https://api.vvhan.com/api/moyu" + res, _ := http.Get(url) + defer res.Body.Close() + body, _ := ioutil.ReadAll(res.Body) + c := string(body) + rese := Base64Encoding(c) + a := []byte(rese) + return a } -func redirect(link string, cookies []*http.Cookie, ua string) (string, error) { - req, err := http.NewRequest("GET", link, nil) - if err != nil { - return "", err - } - req.Header.Set("User-Agent", ua) - var c = make([]string, 0, 4) - for _, cookie := range cookies { - if cookie.Name != "ABTEST" && cookie.Name != "SNUID" && - cookie.Name != "IPLOC" && cookie.Name != "SUID" { - continue - } - c = append(c, cookie.Name+"="+cookie.Value) - } - req.Header.Set("Cookie", strings.Join(c, "; ")) - 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") - } - br := bufio.NewReader(resp.Body) - var u = 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 - } - u = append(u, strings.ReplaceAll(matcha[1], "@", "")) - } - if len(u) == 0 { - return "", errors.New("weixin url not found") - } - return strings.Join(u, ""), nil +func Base64Encoding(str string) string { //Base64编码 + src := []byte(str) + ress := base64.StdEncoding.EncodeToString(src) //将编码变成字符串 + return ress } -func calendar(link, ua string) (string, error) { - req, err := http.NewRequest("GET", link, nil) - req.Header.Set("User-Agent", ua) +func cpuPercent() float64 { + percent, err := cpu.Percent(time.Second, false) if err != nil { - return "", err + return -1 } - resp, err := client.Do(req) + return math.Round(percent[0]) +} + +func memPercent() float64 { + memInfo, err := mem.VirtualMemory() if err != nil { - return "", err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return "", errors.New("status not ok") + return -1 } - doc, err := xpath.Parse(resp.Body) + return math.Round(memInfo.UsedPercent) +} + +func diskPercent() string { + parts, err := disk.Partitions(true) if err != nil { - return "", err - } - html := xpath.OutputHTML(doc, false) - if !strings.Contains(html, time.Now().Format("2006-01-02")) { - return "", errors.New("calendar not today") - } - images := xpath.Find(doc, `//*[@id="js_content"]/p/img`) - if images == nil { - return "", errors.New("calendar not found") + return err.Error() } - var image string - for i := range images { - if xpath.SelectAttr(images[i], "data-w") != "540" { + msg := "" + for _, p := range parts { + diskInfo, err := disk.Usage(p.Mountpoint) + if err != nil { + msg += "\n - " + err.Error() continue } - image = xpath.SelectAttr(images[i], "data-src") - break - } - if image == "" { - return "", errors.New("image not found") + pc := uint(math.Round(diskInfo.UsedPercent)) + if pc > 0 { + msg += fmt.Sprintf("\n - %s(%dM) %d%%", p.Mountpoint, diskInfo.Total/1024/1024, pc) + } } - return image, nil + return msg } From 772da52c200298d48d23159542f71b17b893a19c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=B2=90=E9=A6=A8?= <70338354+LingMuXing@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:05:39 +0800 Subject: [PATCH 2/4] Update calendar.go --- plugin/moyu_calendar/calendar.go | 36 -------------------------------- 1 file changed, 36 deletions(-) diff --git a/plugin/moyu_calendar/calendar.go b/plugin/moyu_calendar/calendar.go index f25d085423..863164da5e 100644 --- a/plugin/moyu_calendar/calendar.go +++ b/plugin/moyu_calendar/calendar.go @@ -66,39 +66,3 @@ func Base64Encoding(str string) string { //Base64编码 ress := base64.StdEncoding.EncodeToString(src) //将编码变成字符串 return ress } - -func cpuPercent() float64 { - percent, err := cpu.Percent(time.Second, false) - if err != nil { - return -1 - } - return math.Round(percent[0]) -} - -func memPercent() float64 { - memInfo, err := mem.VirtualMemory() - if err != nil { - return -1 - } - return math.Round(memInfo.UsedPercent) -} - -func diskPercent() string { - parts, err := disk.Partitions(true) - if err != nil { - return err.Error() - } - msg := "" - for _, p := range parts { - diskInfo, err := disk.Usage(p.Mountpoint) - if err != nil { - msg += "\n - " + err.Error() - continue - } - pc := uint(math.Round(diskInfo.UsedPercent)) - if pc > 0 { - msg += fmt.Sprintf("\n - %s(%dM) %d%%", p.Mountpoint, diskInfo.Total/1024/1024, pc) - } - } - return msg -} From 52c4ef62dce90bc80ca484d612352b7f1c7f07eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=B2=90=E9=A6=A8?= <70338354+LingMuXing@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:13:37 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=9B=B4=E6=8D=A2API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复BUG --- plugin/moyu_calendar/calendar.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin/moyu_calendar/calendar.go b/plugin/moyu_calendar/calendar.go index 863164da5e..51886320a8 100644 --- a/plugin/moyu_calendar/calendar.go +++ b/plugin/moyu_calendar/calendar.go @@ -3,12 +3,10 @@ package moyucalendar import ( "encoding/base64" - "fmt" "io/ioutil" "net/http" - "time" - - + + control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control/order" "github.com/FloatTech/zbputils/process" "github.com/wdvxdr1123/ZeroBot/utils/helper" From 98c66ce95193e6993973388460cfd70e5f371d50 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 7 May 2022 14:34:37 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=8E=A8=20=E6=94=B9=E8=BF=9B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/moyu_calendar/calendar.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/moyu_calendar/calendar.go b/plugin/moyu_calendar/calendar.go index 287150aecd..879992563f 100644 --- a/plugin/moyu_calendar/calendar.go +++ b/plugin/moyu_calendar/calendar.go @@ -3,7 +3,7 @@ package moyucalendar import ( control "github.com/FloatTech/zbputils/control" - "github.com/FloatTech/zbputils/web" + "github.com/FloatTech/zbputils/web" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" ) @@ -18,8 +18,8 @@ func init() { " - 摸鱼人日历", }).OnFullMatch("摸鱼人日历").SetBlock(true). Handle(func(ctx *zero.Ctx) { - data, err := web.GetData("https://api.vvhan.com/api/moyu") - if err != nil { + data, err := web.GetData("https://api.vvhan.com/api/moyu") + if err != nil { ctx.SendChain(message.Text("ERROR:", err)) return }