talentVideoInfo.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package youngee_talent_service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/lin-jim-leon/kuaishou/util"
  6. "io/ioutil"
  7. "net/http"
  8. )
  9. const (
  10. //快手
  11. VideoCountUrl = "https://open.kuaishou.com/openapi/photo/count?app_id=%s&access_token=%s"
  12. VideoListUrl = "https://open.kuaishou.com/openapi/photo/count?app_id=%s&access_token=%s?count=200"
  13. //抖音
  14. DyVideoList = "https://open.douyin.com/api/douyin/v1/video/video_list?count=20&open_id=%s&cursor=%d"
  15. )
  16. type Videoresponse struct {
  17. Result int `json:"result"`
  18. PublicCount int `json:"public_count"`
  19. FriendCount int `json:"friend_count"`
  20. PrivateCount int `json:"private_count"`
  21. AllCount int `json:"all_count"`
  22. }
  23. type VideoListResponse struct {
  24. Result int `json:"result"`
  25. VideoList []VideoInfo `json:"video_list"`
  26. }
  27. type VideoInfo struct {
  28. PhotoID int `json:"photo_id"`
  29. Caption int `json:"caption"`
  30. Cover int `json:"cover"`
  31. PlayURL int `json:"play_url"`
  32. CreateTime int `json:"create_time"`
  33. LikeCount int `json:"like_count"`
  34. CommentCount int `json:"comment_count"`
  35. ViewCount int `json:"view_count"`
  36. Pending int `json:"pending"`
  37. }
  38. // 获取快手总作品数
  39. func GetVideoCount(appid string, accesstoken string) (info Videoresponse, err error) {
  40. uri := fmt.Sprintf(VideoCountUrl, appid, accesstoken)
  41. var response []byte
  42. response, err = util.HTTPGet(uri)
  43. if err != nil {
  44. return Videoresponse{}, err
  45. }
  46. var result Videoresponse
  47. err = json.Unmarshal(response, &result)
  48. if err != nil {
  49. return Videoresponse{}, err
  50. }
  51. return result, nil
  52. }
  53. // 获取快手视频列表(暂时最多统计200个视频)
  54. func GetVideoList(appid string, accesstoken string) (info VideoListResponse, err error) {
  55. uri := fmt.Sprintf(VideoListUrl, appid, accesstoken)
  56. var response []byte
  57. response, err = util.HTTPGet(uri)
  58. if err != nil {
  59. return VideoListResponse{}, err
  60. }
  61. var result VideoListResponse
  62. err = json.Unmarshal(response, &result)
  63. if err != nil {
  64. return VideoListResponse{}, err
  65. }
  66. return result, nil
  67. }
  68. // 获取快手总点赞数(暂时最多统计200个视频)
  69. func GetLikeCount(appid string, accesstoken string) (int, error) {
  70. videoListResponse, err := GetVideoList(appid, accesstoken)
  71. if err != nil {
  72. return 0, err
  73. }
  74. totalLikes := 0
  75. for _, video := range videoListResponse.VideoList {
  76. totalLikes += video.LikeCount
  77. }
  78. return totalLikes, nil
  79. }
  80. // 获取抖音视频列表
  81. // 获取快手视频列表(暂时最多统计200个视频)
  82. type DyVideoStatistics struct {
  83. ForwardCount int `json:"forward_count"`
  84. CommentCount int `json:"comment_count"`
  85. DiggCount int `json:"digg_count"`
  86. DownloadCount int `json:"download_count"`
  87. PlayCount int `json:"play_count"`
  88. ShareCount int `json:"share_count"`
  89. }
  90. type DyVideo struct {
  91. Title string `json:"title"`
  92. IsTop bool `json:"is_top"`
  93. CreateTime int64 `json:"create_time"`
  94. IsReviewed bool `json:"is_reviewed"`
  95. VideoStatus int `json:"video_status"`
  96. ShareURL string `json:"share_url"`
  97. ItemID string `json:"item_id"`
  98. MediaType int `json:"media_type"`
  99. Cover string `json:"cover"`
  100. Statistics DyVideoStatistics `json:"statistics"`
  101. VideoId string `json:"video_id"`
  102. }
  103. type DyExtraData struct {
  104. ErrorCode int `json:"error_code"`
  105. Description string `json:"description"`
  106. SubErrorCode int `json:"sub_error_code"`
  107. SubDescription string `json:"sub_description"`
  108. LogID string `json:"logid"`
  109. Now int64 `json:"now"`
  110. }
  111. type DyData struct {
  112. List []DyVideo `json:"list"` // 视频列表
  113. Cursor int `json:"cursor"`
  114. Description string `json:"description"`
  115. ErrorCode int `json:"error_code"`
  116. HasMore bool `json:"has_more"`
  117. }
  118. type DyVideoResponse struct {
  119. Data DyData `json:"data"`
  120. ExtraData DyExtraData `json:"extra"`
  121. }
  122. type DyVideoResult struct {
  123. LikeCount int `json:"like_count"`
  124. VideoCount int `json:"video_count"`
  125. }
  126. func GetDyVideoInfo(openid string, accesstoken string) (info DyVideoResult, err error) {
  127. var totalLikeCount int
  128. var videoCount int
  129. cursor := 0
  130. hasMore := true
  131. for hasMore {
  132. // 调用 GetDyVideoList 获取视频列表
  133. videoListRes, err := GetDyVideoList(openid, cursor, accesstoken)
  134. if err != nil {
  135. fmt.Println("GetDyVideoInfo error", err)
  136. return DyVideoResult{}, err
  137. }
  138. // 统计 video_status=5 的视频总点赞数和视频数
  139. for _, video := range videoListRes.Data.List {
  140. if video.VideoStatus == 1 {
  141. fmt.Println("*******")
  142. fmt.Println(totalLikeCount)
  143. totalLikeCount += video.Statistics.DiggCount
  144. videoCount++
  145. }
  146. }
  147. // 更新 cursor 和 hasMore
  148. cursor = videoListRes.Data.Cursor
  149. hasMore = videoListRes.Data.HasMore
  150. }
  151. fmt.Println("totalLikeCount", totalLikeCount)
  152. fmt.Println("videoCount", videoCount)
  153. return DyVideoResult{
  154. LikeCount: totalLikeCount,
  155. VideoCount: videoCount,
  156. }, nil
  157. }
  158. // GetDyVideoInfo 发送GET请求并返回抖音视频列表信息
  159. func GetDyVideoList(openid string, cursor int, accesstoken string) (info DyVideoResponse, err error) {
  160. // 请求URL
  161. url := fmt.Sprintf("https://open.douyin.com/api/douyin/v1/video/video_list?count=20&cursor=%d&open_id=%s", cursor, openid)
  162. // 创建请求
  163. req, err := http.NewRequest("GET", url, nil)
  164. if err != nil {
  165. return info, err
  166. }
  167. // 设置请求头
  168. req.Header.Set("access-token", accesstoken)
  169. req.Header.Set("Content-Type", "application/json")
  170. // 发送请求
  171. client := &http.Client{}
  172. resp, err := client.Do(req)
  173. if err != nil {
  174. return info, err
  175. }
  176. defer resp.Body.Close()
  177. // 检查HTTP状态码是否为200
  178. if resp.StatusCode != http.StatusOK {
  179. return info, fmt.Errorf("request failed with status code: %d", resp.StatusCode)
  180. }
  181. // 读取响应
  182. body, err := ioutil.ReadAll(resp.Body)
  183. if err != nil {
  184. return info, err
  185. }
  186. // 输出原始响应数据进行调试
  187. fmt.Println("Raw Response:", string(body))
  188. // 解析响应
  189. var result DyVideoResponse
  190. err = json.Unmarshal(body, &result)
  191. if err != nil {
  192. return result, err
  193. }
  194. // 检查API响应中的错误码
  195. if result.ExtraData.ErrorCode != 0 {
  196. return result, fmt.Errorf("API error: %d, description: %s", result.ExtraData.ErrorCode, result.ExtraData.Description)
  197. }
  198. return result, nil
  199. }
  200. //