package com.cst.douyi;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.util.ArrayList;
import java.util.List;

/**
 * 短视频平台 热门视频获取（合规公开接口版）
 * 支持：抖音开放平台/快手公开榜单/小红书公开热门等
 *
 * 申请平台开放平台权限
 * 抖音开放平台：https://open.douyin.com/
 * 快手开放平台：https://open.kuaishou.com/
 * 小红书开放平台：https://open.xiaohongshu.com/
 *开发控制台： https://developer.open-douyin.com/console?type=1
 *
 */
public class VideoHotUtil01 {
    private static final OkHttpClient client = new OkHttpClient();
    private static final ObjectMapper mapper = new ObjectMapper();

    // ==================== 抖音公开热门视频（示例，替换为你自己的开放平台接口）====================
    // 注意：必须使用抖音开放平台申请的接口，不能用抓包的私有接口！
    private static final String DOUYIN_HOT_API =
            "https://open.douyin.com/api/douyin/v1/video/list?access_token=YOUR_TOKEN";

    /**
     * 获取抖音热门视频（需要开放平台权限）
     */
    public static List<HotVideo> getDouyinHotVideos() {
        List<HotVideo> list = new ArrayList<>();
        try {
            Request request = new Request.Builder()
                    .url(DOUYIN_HOT_API)
                    .addHeader("User-Agent", "Mozilla/5.0")
                    .get()
                    .build();

            try (Response response = client.newCall(request).execute()) {
                if (!response.isSuccessful()) return list;

                JsonNode root = mapper.readTree(response.body().string());
                JsonNode videos = root.path("data").path("list");

                // 解析视频
                for (JsonNode node : videos) {
                    HotVideo video = new HotVideo();
                    video.setTitle(node.path("title").asText());
                    video.setPlayUrl(node.path("play_addr").path("url_list").get(0).asText());
                    video.setCoverUrl(node.path("cover").path("url_list").get(0).asText());
                    video.setLikeCount(node.path("like_count").asLong());
                    video.setCreateTime(node.path("create_time").asLong());
                    list.add(video);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    // 视频实体类
    public static class HotVideo {
        private String title;        // 标题
        private String playUrl;      // 播放地址
        private String coverUrl;     // 封面
        private long likeCount;      // 点赞数
        private long createTime;     // 发布时间

        // getter + setter
        public String getTitle() {return title;}
        public void setTitle(String title) {this.title = title;}
        public String getPlayUrl() {return playUrl;}
        public void setPlayUrl(String playUrl) {this.playUrl = playUrl;}
        public String getCoverUrl() {return coverUrl;}
        public void setCoverUrl(String coverUrl) {this.coverUrl = coverUrl;}
        public long getLikeCount() {return likeCount;}
        public void setLikeCount(long likeCount) {this.likeCount = likeCount;}
        public long getCreateTime() {return createTime;}
        public void setCreateTime(long createTime) {this.createTime = createTime;}
    }

    // 测试
    public static void main(String[] args) {
        List<HotVideo> videos = getDouyinHotVideos();
        for (HotVideo v : videos) {
            System.out.println("标题：" + v.getTitle());
            System.out.println("点赞：" + v.getLikeCount());
            System.out.println("播放地址：" + v.getPlayUrl());
            System.out.println("----------------------------------");
        }
    }
}
