forked from Mrs4s/MiraiGo
/
http_api.go
312 lines (282 loc) · 9.13 KB
/
http_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package client
import (
"bytes"
"encoding/json"
"fmt"
"html"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"strconv"
"github.com/pkg/errors"
"github.com/Sunakier/MiraiGo/binary"
"github.com/Sunakier/MiraiGo/client/pb/richmedia"
"github.com/Sunakier/MiraiGo/internal/proto"
"github.com/Sunakier/MiraiGo/utils"
)
/* -------- GroupHonorInfo -------- */
type (
HonorType int
GroupHonorInfo struct {
GroupCode string `json:"gc"`
Uin string `json:"uin"`
Type HonorType `json:"type"`
TalkativeList []HonorMemberInfo `json:"talkativeList"`
CurrentTalkative CurrentTalkative `json:"currentTalkative"`
ActorList []HonorMemberInfo `json:"actorList"`
LegendList []HonorMemberInfo `json:"legendList"`
StrongNewbieList []HonorMemberInfo `json:"strongnewbieList"`
EmotionList []HonorMemberInfo `json:"emotionList"`
}
HonorMemberInfo struct {
Uin int64 `json:"uin"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Desc string `json:"desc"`
}
CurrentTalkative struct {
Uin int64 `json:"uin"`
DayCount int32 `json:"day_count"`
Avatar string `json:"avatar"`
Name string `json:"nick"`
}
)
const (
Talkative HonorType = 1 // 龙王
Performer HonorType = 2 // 群聊之火
Legend HonorType = 3 // 群聊炙焰
StrongNewbie HonorType = 5 // 冒尖小春笋
Emotion HonorType = 6 // 快乐源泉
)
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("</script>"))]
ret := GroupHonorInfo{}
err = json.Unmarshal(b, &ret)
if err != nil {
return nil, err
}
return &ret, nil
}
/* -------- TextToSpeech -------- */
func (c *QQClient) GetTts(text string) ([]byte, error) {
apiUrl := "https://textts.qq.com/cgi-bin/tts"
data := fmt.Sprintf(`{"appid": "201908021016","sendUin": %v,"text": %q}`, c.Uin, text)
rsp, err := utils.HttpPostBytesWithCookie(apiUrl, []byte(data), c.getCookies())
if err != nil {
return nil, errors.Wrap(err, "failed to post to tts server")
}
ttsReader := binary.NewReader(rsp)
ttsWriter := binary.SelectWriter()
for {
// 数据格式 69e(字符串) 十六进制 数据长度 0 为结尾
// 0D 0A (分隔符) payload 0D 0A
var dataLen []byte
for b := ttsReader.ReadByte(); b != byte(0x0d); b = ttsReader.ReadByte() {
dataLen = append(dataLen, b)
}
ttsReader.ReadByte()
var length int
_, _ = fmt.Sscan("0x"+string(dataLen), &length)
if length == 0 {
break
}
ttsRsp := &richmedia.TtsRspBody{}
err := proto.Unmarshal(ttsReader.ReadBytes(length), ttsRsp)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal protobuf message")
}
if ttsRsp.RetCode != 0 {
return nil, errors.New("can't convert text to voice")
}
for _, voiceItem := range ttsRsp.VoiceData {
ttsWriter.Write(voiceItem.Voice)
}
ttsReader.ReadBytes(2)
}
ret := ttsWriter.Bytes()
ret[0] = '\x02'
return ret, nil
}
/* -------- GroupNotice -------- */
type groupNoticeRsp struct {
Feeds []*GroupNoticeFeed `json:"feeds"`
Inst []*GroupNoticeFeed `json:"inst"`
}
type GroupNoticeFeed struct {
NoticeId string `json:"fid"`
SenderId uint32 `json:"u"`
PublishTime uint64 `json:"pubt"`
Message struct {
Text string `json:"text"`
Images []noticeImage `json:"pics"`
} `json:"msg"`
}
type GroupNoticeMessage struct {
NoticeId string `json:"notice_id"`
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"`
ID string `json:"id"`
}
type noticeImage struct {
Height string `json:"h"`
Width string `json:"w"`
ID string `json:"id"`
}
type noticeSendResp struct {
NoticeId string `json:"new_fid"`
}
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)+len(s.Inst))
parse := func(v *GroupNoticeFeed) {
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{
NoticeId: v.NoticeId,
SenderId: v.SenderId,
PublishTime: v.PublishTime,
Message: struct {
Text string `json:"text"`
Images []GroupNoticeImage `json:"images"`
}{
Text: v.Message.Text,
Images: ims,
},
})
}
for _, v := range s.Feeds {
parse(v)
}
for _, v := range s.Inst {
parse(v)
}
return o
}
func (c *QQClient) uploadGroupNoticePic(img []byte) (*noticeImage, error) {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
_ = w.WriteField("bkn", strconv.Itoa(c.getCSRFToken()))
_ = w.WriteField("source", "troopNotice")
_ = w.WriteField("m", "0")
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="pic_up"; filename="temp_uploadFile.png"`)
h.Set("Content-Type", "image/png")
fw, _ := w.CreatePart(h)
_, _ = fw.Write(img)
_ = w.Close()
req, err := http.NewRequest(http.MethodPost, "https://web.qun.qq.com/cgi-bin/announce/upload_img", buf)
if err != nil {
return nil, errors.Wrap(err, "new request error")
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("Cookie", c.getCookies())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "post error")
}
defer resp.Body.Close()
var res noticePicUpResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal json")
}
if res.ErrorCode != 0 {
return nil, errors.New(res.ErrorMessage)
}
ret := ¬iceImage{}
err = json.Unmarshal([]byte(html.UnescapeString(res.ID)), &ret)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal json")
}
return ret, nil
}
// AddGroupNoticeSimple 发群公告
func (c *QQClient) AddGroupNoticeSimple(groupCode int64, text string) (noticeId string, err error) {
body := fmt.Sprintf(`qid=%v&bkn=%v&text=%v&pinned=0&type=1&settings={"is_show_edit_card":0,"tip_window_type":1,"confirm_required":1}`, groupCode, c.getCSRFToken(), url.QueryEscape(text))
resp, err := utils.HttpPostBytesWithCookie("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice?bkn="+fmt.Sprint(c.getCSRFToken()), []byte(body), c.getCookiesWithDomain("qun.qq.com"))
if err != nil {
return "", errors.Wrap(err, "request error")
}
var res noticeSendResp
err = json.Unmarshal(resp, &res)
if err != nil {
return "", errors.Wrap(err, "json unmarshal error")
}
return res.NoticeId, nil
}
// AddGroupNoticeWithPic 发群公告带图片
func (c *QQClient) AddGroupNoticeWithPic(groupCode int64, text string, pic []byte) (noticeId string, err error) {
img, err := c.uploadGroupNoticePic(pic)
if err != nil {
return "", err
}
body := fmt.Sprintf(`qid=%v&bkn=%v&text=%v&pinned=0&type=1&settings={"is_show_edit_card":0,"tip_window_type":1,"confirm_required":1}&pic=%v&imgWidth=%v&imgHeight=%v`, groupCode, c.getCSRFToken(), url.QueryEscape(text), img.ID, img.Width, img.Height)
resp, err := utils.HttpPostBytesWithCookie("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice?bkn="+fmt.Sprint(c.getCSRFToken()), []byte(body), c.getCookiesWithDomain("qun.qq.com"))
if err != nil {
return "", errors.Wrap(err, "request error")
}
var res noticeSendResp
err = json.Unmarshal(resp, &res)
if err != nil {
return "", errors.Wrap(err, "json unmarshal error")
}
return res.NoticeId, nil
}
func (c *QQClient) DelGroupNotice(groupCode int64, fid string) error {
body := fmt.Sprintf(`fid=%s&qid=%v&bkn=%v&ft=23&op=1`, fid, groupCode, c.getCSRFToken())
_, err := utils.HttpPostBytesWithCookie("https://web.qun.qq.com/cgi-bin/announce/del_feed", []byte(body), c.getCookiesWithDomain("qun.qq.com"))
if err != nil {
return errors.Wrap(err, "request error")
}
return nil
}