-
Notifications
You must be signed in to change notification settings - Fork 9
/
mlogSend.go
132 lines (122 loc) · 3.42 KB
/
mlogSend.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
package api
import (
"encoding/json"
"fmt"
"github.com/XiaoMengXinX/Music163Api-Go/types"
"github.com/XiaoMengXinX/Music163Api-Go/utils"
)
// SendMlogAPI 发送 Mlog API
const SendMlogAPI = "/api/mlog/publish/v1"
type mlogSong struct {
EndTime int `json:"endTime"`
Name string `json:"name"`
SongId string `json:"songId"`
StartTime int `json:"startTime"`
}
type picMlogJsonContent struct {
Image []MlogPic `json:"image"`
NeedAudio bool `json:"needAudio"`
Song mlogSong `json:"song"`
Text string `json:"text"`
}
type picMlogJson struct {
Content picMlogJsonContent `json:"content"`
From int `json:"from"`
Type int `json:"type"`
}
// MlogPic Mlog 的图片对象
type MlogPic struct {
Height int `json:"height"`
More bool `json:"more"`
NosKey string `json:"nosKey"`
PicKey string `json:"picKey"`
Width int `json:"width"`
}
// SendMlogReqJson 发送 Mlog 请求 json
type SendMlogReqJson struct {
Type string `json:"type"`
Mlog string `json:"mlog"`
Header string `json:"header"`
ER string `json:"e_r"`
}
// NewMlogPicObj 创建 Mlog 图片对象
func NewMlogPicObj(picFile []byte, picData types.NosTokenData) (MlogPic, error) {
width, height, err := utils.ImageSize(picFile)
if err != nil {
return MlogPic{}, err
}
mlogPicData := MlogPic{
Width: width,
Height: height,
More: false,
NosKey: fmt.Sprintf("%s/%s", picData.Data.Bucket, picData.Data.ObjectKey),
PicKey: fmt.Sprintf("%d", picData.Data.ResourceId),
}
return mlogPicData, err
}
// CreatePicMlogReqJson 创建请求 body json
func CreatePicMlogReqJson(text string, songInfo types.SongDetailData, picData []MlogPic) string {
mlogSong := mlogSong{
EndTime: 0,
Name: songInfo.Name,
SongId: fmt.Sprintf("%d", songInfo.Id),
StartTime: 30000,
}
mlogContent := picMlogJsonContent{
Image: picData,
NeedAudio: false,
Song: mlogSong,
Text: text,
}
mlogData := picMlogJson{
Content: mlogContent,
From: 0,
Type: 1,
}
mlogDataJson, _ := json.Marshal(mlogData)
mlogJson := SendMlogReqJson{
Type: "1",
Mlog: string(mlogDataJson),
Header: "{}",
ER: "true",
}
resultJson, _ := json.Marshal(mlogJson)
return string(resultJson)
}
// SendPicMlog 发送图片 Mlog
func SendPicMlog(data utils.RequestData, text string, songID int, picPath []string) (result types.SendMlogData, err error) {
var options utils.EapiOption
options.Path = SendMlogAPI
options.Url = "https://music.163.com/eapi/mlog/publish/v1"
var picData []MlogPic
if len(picPath) == 0 || text == "" || songID == 0 {
return types.SendMlogData{}, fmt.Errorf("参数错误 ")
}
songInfo, err := GetSongDetail(data, []int{songID})
if err != nil || len(songInfo.Songs) == 0 {
return result, fmt.Errorf("获取音乐详细信息失败 %v", err)
}
for i := 0; i < len(picPath); i++ {
nosToken, file, err := GetMlogNosToken(data, picPath[i])
if err != nil {
return result, err
}
_, err = UploadFile(data, file, nosToken)
if err != nil {
return result, err
}
picObj, err := NewMlogPicObj(file, nosToken)
if err != nil {
return result, err
}
picData = append(picData, picObj)
}
options.Json = CreatePicMlogReqJson(text, songInfo.Songs[0], picData)
resBody, _, err := utils.EapiRequest(options, data)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(resBody), &result)
result.RawJson = resBody
return result, err
}