Skip to content

Commit

Permalink
feat: support get group announcement (Mrs4s#258)
Browse files Browse the repository at this point in the history
* feat: support get group announcement

* refactor: avoid importing new dependent package

* refactor: prettify
  • Loading branch information
qianjunakasumi committed Mar 18, 2022
1 parent f5950d7 commit 8fa49fe
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
86 changes: 86 additions & 0 deletions client/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ func (c *QQClient) GetTts(text string) ([]byte, error) {

/* -------- GroupNotice -------- */

type groupNoticeRsp struct {
Feeds []*struct {
SenderId uint32 `json:"u"`
PublishTime uint64 `json:"pubt"`
Message struct {
Text string `json:"text"`
Images []noticeImage `json:"pics"`
} `json:"msg"`
} `json:"feeds"`
}

type GroupNoticeMessage struct {
SenderId uint32 `json:"sender_id"`
PublishTime uint64 `json:"publish_time"`
Message struct {
Text string `json:"text"`
Images []GroupNoticeImage `json:"images"`
} `json:"message"`
}

type GroupNoticeImage struct {
Height string `json:"height"`
Width string `json:"width"`
ID string `json:"id"`
}

type noticePicUpResponse struct {
ErrorCode int `json:"ec"`
ErrorMessage string `json:"em"`
Expand All @@ -177,6 +203,66 @@ type noticeImage struct {
ID string `json:"id"`
}

func (c *QQClient) GetGroupNotice(groupCode int64) (l []*GroupNoticeMessage, err error) {

v := url.Values{}
v.Set("bkn", strconv.Itoa(c.getCSRFToken()))
v.Set("qid", strconv.FormatInt(groupCode, 10))
v.Set("ft", "23")
v.Set("ni", "1")
v.Set("n", "1")
v.Set("i", "1")
v.Set("log_read", "1")
v.Set("platform", "1")
v.Set("s", "-1")
v.Set("n", "20")

req, _ := http.NewRequest(http.MethodGet, "https://web.qun.qq.com/cgi-bin/announce/get_t_list?"+v.Encode(), nil)
req.Header.Set("Cookie", c.getCookies())
rsp, err := utils.Client.Do(req)
if err != nil {
return
}
defer rsp.Body.Close()

r := groupNoticeRsp{}
err = json.NewDecoder(rsp.Body).Decode(&r)
if err != nil {
return
}

return c.parseGroupNoticeJson(&r), nil
}

func (c *QQClient) parseGroupNoticeJson(s *groupNoticeRsp) []*GroupNoticeMessage {
o := make([]*GroupNoticeMessage, 0, len(s.Feeds))
for _, v := range s.Feeds {

ims := make([]GroupNoticeImage, 0, len(v.Message.Images))
for i := 0; i < len(v.Message.Images); i++ {
ims = append(ims, GroupNoticeImage{
Height: v.Message.Images[i].Height,
Width: v.Message.Images[i].Width,
ID: v.Message.Images[i].ID,
})
}

o = append(o, &GroupNoticeMessage{
SenderId: v.SenderId,
PublishTime: v.PublishTime,
Message: struct {
Text string `json:"text"`
Images []GroupNoticeImage `json:"images"`
}{
Text: v.Message.Text,
Images: ims,
},
})
}

return o
}

func (c *QQClient) uploadGroupNoticePic(img []byte) (*noticeImage, error) {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
Expand Down
8 changes: 4 additions & 4 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

var client = &http.Client{
var Client = &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
MaxConnsPerHost: 0,
Expand All @@ -34,7 +34,7 @@ func HttpPostBytes(url string, data []byte) ([]byte, error) {
}
req.Header["User-Agent"] = []string{"QQ/8.2.0.1296 CFNetwork/1126"}
req.Header["Net-Type"] = []string{"Wifi"}
resp, err := client.Do(req)
resp, err := Client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func HttpPostBytesWithCookie(url string, data []byte, cookie string, contentType
if cookie != "" {
req.Header["Cookie"] = []string{cookie}
}
resp, err := client.Do(req)
resp, err := Client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func HTTPGetReadCloser(url string, cookie string) (io.ReadCloser, error) {
if cookie != "" {
req.Header["Cookie"] = []string{cookie}
}
resp, err := client.Do(req)
resp, err := Client.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8fa49fe

Please sign in to comment.