This commit is contained in:
fumiama 2022-02-26 22:09:55 +08:00
commit 91f522b666

View File

@ -12,15 +12,20 @@ import (
"strings" "strings"
"time" "time"
"github.com/FloatTech/zbputils/binary"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/control/order" 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", order.AcquirePrio(), &control.Options{ control.Register("moyucalendar", order.AcquirePrio(), &control.Options{
DisableOnDefault: true, DisableOnDefault: true,
@ -29,20 +34,41 @@ func init() {
"- /禁用 moyucalendar", "- /禁用 moyucalendar",
}).OnFullMatch("摸鱼人日历").SetBlock(true). }).OnFullMatch("摸鱼人日历").SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
image, err := crew() title := fmt.Sprintf("摸鱼人日历 %d月%d日", time.Now().Month(), time.Now().Day())
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
}
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)) ctx.SendChain(message.Image(image))
}) })
// 定时任务每天8点执行一次 // 定时任务每天8点30分执行一次
_, err := process.CronTab.AddFunc("30 8 * * *", func() { _, err := process.CronTab.AddFunc("30 8 * * *", func() {
m, ok := control.Lookup("moyucalendar") m, ok := control.Lookup("moyucalendar")
if !ok { if !ok {
return return
} }
image, err := crew() 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 { if err != nil {
return return
} }
@ -62,23 +88,73 @@ func init() {
} }
} }
var newest = regexp.MustCompile(`href="(/link.+?)" id="sogou_vr_11002601_title_0" uigs="article_title_0"`) func sougou(title, publisher, ua string) (string, []*http.Cookie, error) {
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{}
u, _ := url.Parse("https://weixin.sogou.com/weixin") u, _ := url.Parse("https://weixin.sogou.com/weixin")
u.RawQuery = url.Values{ u.RawQuery = url.Values{
"type": []string{"2"}, "type": []string{"2"},
"s_from": []string{"input"}, "s_from": []string{"input"},
"query": []string{fmt.Sprintf("摸鱼人日历 %d月%d日", time.Now().Month(), time.Now().Day())}, "query": []string{title},
}.Encode() }.Encode()
req, err := http.NewRequest("GET", u.String(), nil) 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 { if err != nil {
return "", err 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") 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) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", err return "", err
@ -87,38 +163,8 @@ func crew() (string, error) {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return "", errors.New("status not ok") return "", errors.New("status not ok")
} }
b, err := io.ReadAll(resp.Body) br := bufio.NewReader(resp.Body)
if err != nil { var u = make([]string, 0)
return "", err
}
match := newest.FindStringSubmatch(binary.BytesToString(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 { for {
b, _, err := br.ReadLine() b, _, err := br.ReadLine()
if err == io.EOF { if err == io.EOF {
@ -127,39 +173,54 @@ func crew() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
matcha := weixin.FindStringSubmatch(binary.BytesToString(b)) matcha := weixin.FindStringSubmatch(string(b))
if len(matcha) < 2 { if len(matcha) < 2 {
continue continue
} }
weixinurl = append(weixinurl, strings.ReplaceAll(matcha[1], "@", "")) u = append(u, strings.ReplaceAll(matcha[1], "@", ""))
} }
if len(weixinurl) == 0 { if len(u) == 0 {
return "", errors.New("weixin url not found") return "", errors.New("weixin url not found")
} }
reqw, err := http.NewRequest("GET", strings.Join(weixinurl, ""), nil) return strings.Join(u, ""), 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") }
func calendar(link, ua string) (string, error) {
req, err := http.NewRequest("GET", link, nil)
req.Header.Set("User-Agent", ua)
if err != nil { if err != nil {
return "", err return "", err
} }
respw, err := client.Do(reqw) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", err return "", err
} }
defer respw.Body.Close() defer resp.Body.Close()
if respw.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return "", errors.New("status not ok") return "", errors.New("status not ok")
} }
bw, _ := io.ReadAll(respw.Body) doc, err := xpath.Parse(resp.Body)
today, err := regexp.Compile(time.Now().Format("2006-01-02"))
if err != nil { if err != nil {
return "", err return "", err
} }
if !today.Match(bw) { html := xpath.OutputHTML(doc, false)
return "", errors.New("today not found") if !strings.Contains(html, time.Now().Format("2006-01-02")) {
return "", errors.New("calendar not today")
} }
matchw := calendar.FindStringSubmatch(binary.BytesToString(bw)) images := xpath.Find(doc, `//*[@id="js_content"]/p/img`)
if len(matchw) < 2 { if images == nil {
return "", errors.New("calendar not found") return "", errors.New("calendar not found")
} }
return matchw[1], nil 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
} }