From a8213e127b2b74ad50fcc3101bca74276ebb2ae4 Mon Sep 17 00:00:00 2001 From: Lumine <66518048+1umine@users.noreply.github.com> Date: Wed, 23 Aug 2023 13:05:31 +0800 Subject: [PATCH] fix: get group honor info (#340) * fix: get group honor info * update: use newDecoder instead manual parse --- client/http_api.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/client/http_api.go b/client/http_api.go index e0da46ec..e43ddcce 100644 --- a/client/http_api.go +++ b/client/http_api.go @@ -9,6 +9,7 @@ import ( "net/http" "net/textproto" "net/url" + "regexp" "strconv" "github.com/pkg/errors" @@ -59,15 +60,20 @@ const ( Emotion HonorType = 6 // 快乐源泉 ) +// 匹配 window.__INITIAL_STATE__ = 后的内容 +var honorRe = regexp.MustCompile(`window\.__INITIAL_STATE__\s*?=\s*?(\{.*\})`) + func (c *QQClient) GetGroupHonorInfo(groupCode int64, honorType HonorType) (*GroupHonorInfo, error) { b, err := utils.HttpGetBytes(fmt.Sprintf("https://qun.qq.com/interactive/honorlist?gc=%d&type=%d", groupCode, honorType), c.getCookiesWithDomain("qun.qq.com")) if err != nil { return nil, err } - b = b[bytes.Index(b, []byte(`window.__INITIAL_STATE__=`))+25:] - b = b[:bytes.Index(b, []byte(""))] + matched := honorRe.FindSubmatch(b) + if len(matched) == 0 { + return nil, errors.New("无匹配结果") + } ret := GroupHonorInfo{} - err = json.Unmarshal(b, &ret) + err = json.NewDecoder(bytes.NewReader(matched[1])).Decode(&ret) if err != nil { return nil, err }