mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
* Update calendar.go
更换API方式
* Update calendar.go
* 更换API
修复BUG
* 🎨 改进代码样式
Co-authored-by: 源文雨 <41315874+fumiama@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
bbb4afde53
commit
5081aab497
@ -2,28 +2,12 @@
|
|||||||
package moyucalendar
|
package moyucalendar
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
control "github.com/FloatTech/zbputils/control"
|
control "github.com/FloatTech/zbputils/control"
|
||||||
|
"github.com/FloatTech/zbputils/web"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"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() {
|
func init() {
|
||||||
control.Register("moyucalendar", &control.Options{
|
control.Register("moyucalendar", &control.Options{
|
||||||
DisableOnDefault: true,
|
DisableOnDefault: true,
|
||||||
@ -34,159 +18,11 @@ func init() {
|
|||||||
" - 摸鱼人日历",
|
" - 摸鱼人日历",
|
||||||
}).OnFullMatch("摸鱼人日历").SetBlock(true).
|
}).OnFullMatch("摸鱼人日历").SetBlock(true).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
title := fmt.Sprintf("摸鱼人日历 %d月%d日", time.Now().Month(), time.Now().Day())
|
data, err := web.GetData("https://api.vvhan.com/api/moyu")
|
||||||
sg, cookies, err := sougou(title, "摸鱼人日历", ua)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR:", err))
|
ctx.SendChain(message.Text("ERROR:", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wx, err := redirect(sg, cookies, ua)
|
ctx.SendChain(message.ImageBytes(data))
|
||||||
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))
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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 calendar(link, ua string) (string, error) {
|
|
||||||
req, err := http.NewRequest("GET", link, nil)
|
|
||||||
req.Header.Set("User-Agent", ua)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
doc, err := xpath.Parse(resp.Body)
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
var image string
|
|
||||||
for i := range images {
|
|
||||||
if xpath.SelectAttr(images[i], "data-w") != "540" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
image = xpath.SelectAttr(images[i], "data-src")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if image == "" {
|
|
||||||
return "", errors.New("image not found")
|
|
||||||
}
|
|
||||||
return image, nil
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user