|
@@ -11,7 +11,8 @@ import (
|
|
"strings"
|
|
"strings"
|
|
)
|
|
)
|
|
|
|
|
|
-type XiaohongshuNoteResponse struct {
|
|
|
|
|
|
+// RedBookNoteResponse 小红书接收结构体
|
|
|
|
+type RedBookNoteResponse struct {
|
|
Code int `json:"code"`
|
|
Code int `json:"code"`
|
|
Data struct {
|
|
Data struct {
|
|
ResponseBody struct {
|
|
ResponseBody struct {
|
|
@@ -31,8 +32,21 @@ type XiaohongshuNoteResponse struct {
|
|
} `json:"data"`
|
|
} `json:"data"`
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// WeiboPostResponse 微博接收结构体
|
|
|
|
+type WeiboPostResponse struct {
|
|
|
|
+ Code int `json:"code"`
|
|
|
|
+ Msg string `json:"msg"`
|
|
|
|
+ Data struct {
|
|
|
|
+ AttitudesCount int `json:"attitudes_count"` // 点赞数
|
|
|
|
+ CommentsCount int `json:"comments_count"` // 评论数
|
|
|
|
+ RepostsCount int `json:"reposts_count"` // 转发数
|
|
|
|
+ ReadsCount int `json:"reads_count"` // 浏览数
|
|
|
|
+ } `json:"data"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// parseNoteStats 小红书接收方法
|
|
func parseNoteStats(jsonData []byte) (likes, comments, collects, shares string, err error) {
|
|
func parseNoteStats(jsonData []byte) (likes, comments, collects, shares string, err error) {
|
|
- var resp XiaohongshuNoteResponse
|
|
|
|
|
|
+ var resp RedBookNoteResponse
|
|
if err := json.Unmarshal(jsonData, &resp); err != nil {
|
|
if err := json.Unmarshal(jsonData, &resp); err != nil {
|
|
return "", "", "", "", fmt.Errorf("JSON解析失败: %v", err)
|
|
return "", "", "", "", fmt.Errorf("JSON解析失败: %v", err)
|
|
}
|
|
}
|
|
@@ -46,6 +60,28 @@ func parseNoteStats(jsonData []byte) (likes, comments, collects, shares string,
|
|
return stats.LikedCount, stats.CommentCount, stats.CollectedCount, stats.ShareCount, nil
|
|
return stats.LikedCount, stats.CommentCount, stats.CollectedCount, stats.ShareCount, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// parseWeiboStats 微博接收方法
|
|
|
|
+func parseWeiboStats(jsonData []byte) (likes, comments, reposts, reads string, err error) {
|
|
|
|
+ var resp WeiboPostResponse
|
|
|
|
+
|
|
|
|
+ // 解析JSON
|
|
|
|
+ if err := json.Unmarshal(jsonData, &resp); err != nil {
|
|
|
|
+ return "", "", "", "", fmt.Errorf("JSON解析失败: %v", err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 检查状态码(微博通常用 code=200 表示成功)
|
|
|
|
+ if resp.Code != 200 {
|
|
|
|
+ return "", "", "", "", fmt.Errorf("API返回错误: %s", resp.Msg)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 返回数值(转为字符串,保持与原函数一致)
|
|
|
|
+ return strconv.Itoa(resp.Data.AttitudesCount),
|
|
|
|
+ strconv.Itoa(resp.Data.CommentsCount),
|
|
|
|
+ strconv.Itoa(resp.Data.RepostsCount),
|
|
|
|
+ strconv.Itoa(resp.Data.ReadsCount),
|
|
|
|
+ nil
|
|
|
|
+}
|
|
|
|
+
|
|
func GetRedBookLinkDetail(ctx context.Context, shareText string) (int, int, int, int, error) {
|
|
func GetRedBookLinkDetail(ctx context.Context, shareText string) (int, int, int, int, error) {
|
|
|
|
|
|
url := "http://120.46.92.62:6888/api/xhs/note_detail"
|
|
url := "http://120.46.92.62:6888/api/xhs/note_detail"
|
|
@@ -99,10 +135,59 @@ func GetRedBookLinkDetail(ctx context.Context, shareText string) (int, int, int,
|
|
return 0, 0, 0, 0, err
|
|
return 0, 0, 0, 0, err
|
|
}
|
|
}
|
|
|
|
|
|
- // fmt.Println("点赞数:", likes)
|
|
|
|
- // fmt.Println("评论数:", comments)
|
|
|
|
- // fmt.Println("收藏数:", collects)
|
|
|
|
- // fmt.Println("分享数(可作为浏览量参考):", shares)
|
|
|
|
|
|
+ like, likeErr := strconv.Atoi(likes)
|
|
|
|
+ if likeErr != nil {
|
|
|
|
+ }
|
|
|
|
+ comment, commentErr := strconv.Atoi(comments)
|
|
|
|
+ if commentErr != nil {
|
|
|
|
+ }
|
|
|
|
+ collect, collectErr := strconv.Atoi(collects)
|
|
|
|
+ if collectErr != nil {
|
|
|
|
+ }
|
|
|
|
+ share, shareErr := strconv.Atoi(shares)
|
|
|
|
+ if shareErr != nil {
|
|
|
|
+ }
|
|
|
|
+ return like, comment, collect, share, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetWeiBoLinkDetail(ctx context.Context, shareText string) (int, int, int, int, error) {
|
|
|
|
+ url := "http://api.moreapi.cn/api/weibo/post_detail"
|
|
|
|
+ method := "POST"
|
|
|
|
+
|
|
|
|
+ payload := strings.NewReader(`{
|
|
|
|
+ "id":"",
|
|
|
|
+ "share_text":"",
|
|
|
|
+ "proxy":""
|
|
|
|
+}`)
|
|
|
|
+
|
|
|
|
+ client := &http.Client{}
|
|
|
|
+ req, err := http.NewRequest(method, url, payload)
|
|
|
|
+
|
|
|
|
+ if err != nil {
|
|
|
|
+ //fmt.Println(err)
|
|
|
|
+ return 0, 0, 0, 0, err
|
|
|
|
+ }
|
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
|
+
|
|
|
|
+ res, err := client.Do(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //fmt.Println(err)
|
|
|
|
+ return 0, 0, 0, 0, err
|
|
|
|
+ }
|
|
|
|
+ defer res.Body.Close()
|
|
|
|
+
|
|
|
|
+ body, err := ioutil.ReadAll(res.Body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //fmt.Println(err)
|
|
|
|
+ return 0, 0, 0, 0, err
|
|
|
|
+ }
|
|
|
|
+ // fmt.Println(string(body))
|
|
|
|
+
|
|
|
|
+ likes, comments, collects, shares, err := parseWeiboStats(body)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //fmt.Println("解析互动数据失败:", err)
|
|
|
|
+ return 0, 0, 0, 0, err
|
|
|
|
+ }
|
|
|
|
|
|
like, likeErr := strconv.Atoi(likes)
|
|
like, likeErr := strconv.Atoi(likes)
|
|
if likeErr != nil {
|
|
if likeErr != nil {
|