|
@@ -0,0 +1,120 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+type XiaohongshuNoteResponse struct {
|
|
|
+ Code int `json:"code"`
|
|
|
+ Data struct {
|
|
|
+ ResponseBody struct {
|
|
|
+ Data struct {
|
|
|
+ Items []struct {
|
|
|
+ NoteCard struct {
|
|
|
+ InteractInfo struct {
|
|
|
+ LikedCount string `json:"liked_count"` // 点赞数
|
|
|
+ CommentCount string `json:"comment_count"` // 评论数
|
|
|
+ CollectedCount string `json:"collected_count"` // 收藏数
|
|
|
+ ShareCount string `json:"share_count"` // 分享数
|
|
|
+ } `json:"interact_info"`
|
|
|
+ } `json:"note_card"`
|
|
|
+ } `json:"items"`
|
|
|
+ } `json:"data"`
|
|
|
+ } `json:"response_body"`
|
|
|
+ } `json:"data"`
|
|
|
+}
|
|
|
+
|
|
|
+func parseNoteStats(jsonData []byte) (likes, comments, collects, shares string, err error) {
|
|
|
+ var resp XiaohongshuNoteResponse
|
|
|
+ if err := json.Unmarshal(jsonData, &resp); err != nil {
|
|
|
+ return "", "", "", "", fmt.Errorf("JSON解析失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查数据是否存在
|
|
|
+ if len(resp.Data.ResponseBody.Data.Items) == 0 {
|
|
|
+ return "", "", "", "", errors.New("未找到笔记数据")
|
|
|
+ }
|
|
|
+
|
|
|
+ stats := resp.Data.ResponseBody.Data.Items[0].NoteCard.InteractInfo
|
|
|
+ return stats.LikedCount, stats.CommentCount, stats.CollectedCount, stats.ShareCount, nil
|
|
|
+}
|
|
|
+
|
|
|
+func GetRedBookLinkDetail(ctx context.Context, shareText string) (int, int, int, int, error) {
|
|
|
+
|
|
|
+ url := "http://120.46.92.62:6888/api/xhs/note_detail"
|
|
|
+ method := "POST"
|
|
|
+
|
|
|
+ // 构造请求体结构
|
|
|
+ requestBody := struct {
|
|
|
+ ShareText string `json:"share_text"`
|
|
|
+ Proxy string `json:"proxy"`
|
|
|
+ }{
|
|
|
+ ShareText: shareText,
|
|
|
+ Proxy: "",
|
|
|
+ }
|
|
|
+
|
|
|
+ jsonData, err := json.Marshal(requestBody)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("JSON编码失败:", err)
|
|
|
+ return 0, 0, 0, 0, err
|
|
|
+ }
|
|
|
+
|
|
|
+ payload := strings.NewReader(string(jsonData))
|
|
|
+
|
|
|
+ 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("Cookie", "abRequestId=87d8ec6f-314f-5914-af1d-21296bf34c5f; a1=197a0b8a06djmry91j6taqhe0kcutwojdcu15nypd50000386246; webId=ae01b40eef7780a778b6624a3b295f4f; gid=yjW08S8Y2yffyjW08DY08SIvKfV6FvjyVK907UfAxd8TSy2879ElV2888qYKJ4K82qfKy82D; webBuild=4.68.0; acw_tc=0a4a2d6e17510033969926232e7b39d55058a43fb37e1ddc19745e6642f746; websectiga=7750c37de43b7be9de8ed9ff8ea0e576519e8cd2157322eb972ecb429a7735d4; sec_poison_id=b4fae566-5d78-4269-85c6-28fc28ec20d3; xsecappid=xhs-pc-web; web_session=040069b22467fb3084e74f796b3a4b11424ee0; loadts=1751003476156; unread={%22ub%22:%22685b56850000000020019eef%22%2C%22ue%22:%22685df405000000002201fd00%22%2C%22uc%22:14}")
|
|
|
+ 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 := parseNoteStats(body)
|
|
|
+ if err != nil {
|
|
|
+ //fmt.Println("解析互动数据失败:", 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
|
|
|
+}
|