talent_platform_link_statistic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. )
  12. type XiaohongshuNoteResponse struct {
  13. Code int `json:"code"`
  14. Data struct {
  15. ResponseBody struct {
  16. Data struct {
  17. Items []struct {
  18. NoteCard struct {
  19. InteractInfo struct {
  20. LikedCount string `json:"liked_count"` // 点赞数
  21. CommentCount string `json:"comment_count"` // 评论数
  22. CollectedCount string `json:"collected_count"` // 收藏数
  23. ShareCount string `json:"share_count"` // 分享数
  24. } `json:"interact_info"`
  25. } `json:"note_card"`
  26. } `json:"items"`
  27. } `json:"data"`
  28. } `json:"response_body"`
  29. } `json:"data"`
  30. }
  31. func parseNoteStats(jsonData []byte) (likes, comments, collects, shares string, err error) {
  32. var resp XiaohongshuNoteResponse
  33. if err := json.Unmarshal(jsonData, &resp); err != nil {
  34. return "", "", "", "", fmt.Errorf("JSON解析失败: %v", err)
  35. }
  36. // 检查数据是否存在
  37. if len(resp.Data.ResponseBody.Data.Items) == 0 {
  38. return "", "", "", "", errors.New("未找到笔记数据")
  39. }
  40. stats := resp.Data.ResponseBody.Data.Items[0].NoteCard.InteractInfo
  41. return stats.LikedCount, stats.CommentCount, stats.CollectedCount, stats.ShareCount, nil
  42. }
  43. func GetRedBookLinkDetail(ctx context.Context, shareText string) (int, int, int, int, error) {
  44. url := "http://120.46.92.62:6888/api/xhs/note_detail"
  45. method := "POST"
  46. // 构造请求体结构
  47. requestBody := struct {
  48. ShareText string `json:"share_text"`
  49. Proxy string `json:"proxy"`
  50. }{
  51. ShareText: shareText,
  52. Proxy: "",
  53. }
  54. jsonData, err := json.Marshal(requestBody)
  55. if err != nil {
  56. fmt.Println("JSON编码失败:", err)
  57. return 0, 0, 0, 0, err
  58. }
  59. payload := strings.NewReader(string(jsonData))
  60. client := &http.Client{}
  61. req, err := http.NewRequest(method, url, payload)
  62. if err != nil {
  63. // fmt.Println(err)
  64. return 0, 0, 0, 0, err
  65. }
  66. 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}")
  67. req.Header.Add("Content-Type", "application/json")
  68. res, err := client.Do(req)
  69. if err != nil {
  70. // fmt.Println(err)
  71. return 0, 0, 0, 0, err
  72. }
  73. defer res.Body.Close()
  74. body, err := ioutil.ReadAll(res.Body)
  75. if err != nil {
  76. // fmt.Println(err)
  77. return 0, 0, 0, 0, err
  78. }
  79. fmt.Println(string(body))
  80. // 提取互动数据
  81. likes, comments, collects, shares, err := parseNoteStats(body)
  82. if err != nil {
  83. //fmt.Println("解析互动数据失败:", err)
  84. return 0, 0, 0, 0, err
  85. }
  86. // fmt.Println("点赞数:", likes)
  87. // fmt.Println("评论数:", comments)
  88. // fmt.Println("收藏数:", collects)
  89. // fmt.Println("分享数(可作为浏览量参考):", shares)
  90. like, likeErr := strconv.Atoi(likes)
  91. if likeErr != nil {
  92. }
  93. comment, commentErr := strconv.Atoi(comments)
  94. if commentErr != nil {
  95. }
  96. collect, collectErr := strconv.Atoi(collects)
  97. if collectErr != nil {
  98. }
  99. share, shareErr := strconv.Atoi(shares)
  100. if shareErr != nil {
  101. }
  102. return like, comment, collect, share, nil
  103. }