123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package youngee_talent_service
- import (
- "encoding/json"
- "fmt"
- "github.com/lin-jim-leon/kuaishou/util"
- "io/ioutil"
- "net/http"
- )
- const (
- //快手
- VideoCountUrl = "https://open.kuaishou.com/openapi/photo/count?app_id=%s&access_token=%s"
- VideoListUrl = "https://open.kuaishou.com/openapi/photo/count?app_id=%s&access_token=%s?count=200"
- //抖音
- DyVideoList = "https://open.douyin.com/api/douyin/v1/video/video_list?count=20&open_id=%s&cursor=%d"
- )
- type Videoresponse struct {
- Result int `json:"result"`
- PublicCount int `json:"public_count"`
- FriendCount int `json:"friend_count"`
- PrivateCount int `json:"private_count"`
- AllCount int `json:"all_count"`
- }
- type VideoListResponse struct {
- Result int `json:"result"`
- VideoList []VideoInfo `json:"video_list"`
- }
- type VideoInfo struct {
- PhotoID int `json:"photo_id"`
- Caption int `json:"caption"`
- Cover int `json:"cover"`
- PlayURL int `json:"play_url"`
- CreateTime int `json:"create_time"`
- LikeCount int `json:"like_count"`
- CommentCount int `json:"comment_count"`
- ViewCount int `json:"view_count"`
- Pending int `json:"pending"`
- }
- // 获取快手总作品数
- func GetVideoCount(appid string, accesstoken string) (info Videoresponse, err error) {
- uri := fmt.Sprintf(VideoCountUrl, appid, accesstoken)
- var response []byte
- response, err = util.HTTPGet(uri)
- if err != nil {
- return Videoresponse{}, err
- }
- var result Videoresponse
- err = json.Unmarshal(response, &result)
- if err != nil {
- return Videoresponse{}, err
- }
- return result, nil
- }
- // 获取快手视频列表(暂时最多统计200个视频)
- func GetVideoList(appid string, accesstoken string) (info VideoListResponse, err error) {
- uri := fmt.Sprintf(VideoListUrl, appid, accesstoken)
- var response []byte
- response, err = util.HTTPGet(uri)
- if err != nil {
- return VideoListResponse{}, err
- }
- var result VideoListResponse
- err = json.Unmarshal(response, &result)
- if err != nil {
- return VideoListResponse{}, err
- }
- return result, nil
- }
- // 获取快手总点赞数(暂时最多统计200个视频)
- func GetLikeCount(appid string, accesstoken string) (int, error) {
- videoListResponse, err := GetVideoList(appid, accesstoken)
- if err != nil {
- return 0, err
- }
- totalLikes := 0
- for _, video := range videoListResponse.VideoList {
- totalLikes += video.LikeCount
- }
- return totalLikes, nil
- }
- // 获取抖音视频列表
- // 获取快手视频列表(暂时最多统计200个视频)
- type DyVideoStatistics struct {
- ForwardCount int `json:"forward_count"`
- CommentCount int `json:"comment_count"`
- DiggCount int `json:"digg_count"`
- DownloadCount int `json:"download_count"`
- PlayCount int `json:"play_count"`
- ShareCount int `json:"share_count"`
- }
- type DyVideo struct {
- Title string `json:"title"`
- IsTop bool `json:"is_top"`
- CreateTime int64 `json:"create_time"`
- IsReviewed bool `json:"is_reviewed"`
- VideoStatus int `json:"video_status"`
- ShareURL string `json:"share_url"`
- ItemID string `json:"item_id"`
- MediaType int `json:"media_type"`
- Cover string `json:"cover"`
- Statistics DyVideoStatistics `json:"statistics"`
- VideoId string `json:"video_id"`
- }
- type DyExtraData struct {
- ErrorCode int `json:"error_code"`
- Description string `json:"description"`
- SubErrorCode int `json:"sub_error_code"`
- SubDescription string `json:"sub_description"`
- LogID string `json:"logid"`
- Now int64 `json:"now"`
- }
- type DyData struct {
- List []DyVideo `json:"list"` // 视频列表
- Cursor int `json:"cursor"`
- Description string `json:"description"`
- ErrorCode int `json:"error_code"`
- HasMore bool `json:"has_more"`
- }
- type DyVideoResponse struct {
- Data DyData `json:"data"`
- ExtraData DyExtraData `json:"extra"`
- }
- type DyVideoResult struct {
- LikeCount int `json:"like_count"`
- VideoCount int `json:"video_count"`
- }
- func GetDyVideoInfo(openid string, accesstoken string) (info DyVideoResult, err error) {
- var totalLikeCount int
- var videoCount int
- cursor := 0
- hasMore := true
- for hasMore {
- // 调用 GetDyVideoList 获取视频列表
- videoListRes, err := GetDyVideoList(openid, cursor, accesstoken)
- if err != nil {
- fmt.Println("GetDyVideoInfo error", err)
- return DyVideoResult{}, err
- }
- // 统计 video_status=5 的视频总点赞数和视频数
- for _, video := range videoListRes.Data.List {
- if video.VideoStatus == 1 {
- fmt.Println("*******")
- fmt.Println(totalLikeCount)
- totalLikeCount += video.Statistics.DiggCount
- videoCount++
- }
- }
- // 更新 cursor 和 hasMore
- cursor = videoListRes.Data.Cursor
- hasMore = videoListRes.Data.HasMore
- }
- fmt.Println("totalLikeCount", totalLikeCount)
- fmt.Println("videoCount", videoCount)
- return DyVideoResult{
- LikeCount: totalLikeCount,
- VideoCount: videoCount,
- }, nil
- }
- // GetDyVideoInfo 发送GET请求并返回抖音视频列表信息
- func GetDyVideoList(openid string, cursor int, accesstoken string) (info DyVideoResponse, err error) {
- // 请求URL
- url := fmt.Sprintf("https://open.douyin.com/api/douyin/v1/video/video_list?count=20&cursor=%d&open_id=%s", cursor, openid)
- // 创建请求
- req, err := http.NewRequest("GET", url, nil)
- if err != nil {
- return info, err
- }
- // 设置请求头
- req.Header.Set("access-token", accesstoken)
- req.Header.Set("Content-Type", "application/json")
- // 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return info, err
- }
- defer resp.Body.Close()
- // 检查HTTP状态码是否为200
- if resp.StatusCode != http.StatusOK {
- return info, fmt.Errorf("request failed with status code: %d", resp.StatusCode)
- }
- // 读取响应
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return info, err
- }
- // 输出原始响应数据进行调试
- fmt.Println("Raw Response:", string(body))
- // 解析响应
- var result DyVideoResponse
- err = json.Unmarshal(body, &result)
- if err != nil {
- return result, err
- }
- // 检查API响应中的错误码
- if result.ExtraData.ErrorCode != 0 {
- return result, fmt.Errorf("API error: %d, description: %s", result.ExtraData.ErrorCode, result.ExtraData.Description)
- }
- return result, nil
- }
- //
|