KKSU
2024-05-30 84599f1fdac0d9723c7f02a2e66b0e909ad911d7
src/main/java/cc/mrbird/febs/mall/test/DYDelMark.java
@@ -1,162 +1,162 @@
package cc.mrbird.febs.mall.test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DYDelMark {
    public static void main(String[] args) {
        String content = "https://v.douyin.com/iLwxgYHE/";
        JSONObject douYData = extractDouYinData(content);
        if (douYData != null) {
            System.out.println(douYData);
        } else {
            System.out.println(1);
        }
    }
    private static JSONObject extractDouYinData(String content) {
        String url = extractUrl(content);
        if (url != null) {
            if (content.contains("douyin.com")) {
                if (content.contains("live.douyin.com")) {
                    System.out.println("直播");
                    return null;
                } else {
                    String dyUrlOrId = processContent(content);
                    String videoId = isNumeric(dyUrlOrId) ? dyUrlOrId : extractVideoId(dyUrlOrId);
                    if (videoId!=null){
                        JSONArray dyData = serverGetDy(videoId);
                        if (dyData != null && dyData.size() > 0) {
                            JSONObject jsonData = dyData.getJSONObject(0);
                            return buildDouYinData(jsonData);
                        }
                    }
                }
            }
        }
        return null;
    }
    private static JSONObject buildDouYinData(JSONObject jsonData) {
        JSONObject douYData = new JSONObject();
        douYData.put("desc", jsonData.getString("desc"));
        JSONObject author = jsonData.getJSONObject("author");
        douYData.put("nickname", author.getString("nickname"));
        JSONObject videoJson = jsonData.getJSONObject("video");
        JSONObject coverJson = videoJson.getJSONObject("cover");
        String coverUrl = coverJson.getJSONArray("url_list").getString(0);
        douYData.put("coverUrl", coverUrl);
        if (jsonData.getJSONArray("images") != null) {
            JSONArray images = jsonData.getJSONArray("images");
            JSONArray imageArray = new JSONArray();
            for (int i = 0; i < images.size(); i++) {
                JSONObject image = images.getJSONObject(i);
                JSONArray urlList = image.getJSONArray("url_list");
                imageArray.add(urlList.get(0));
            }
            douYData.put("type", 2);
            douYData.put("data", imageArray);
        } else {
            JSONObject playAddr = videoJson.getJSONObject("play_addr");
            String uri = playAddr.getString("uri");
            String videoUrl = "https://www.iesdouyin.com/aweme/v1/play/?video_id=" + uri + "&ratio=1080p&line=0";
            douYData.put("type", 1);
            douYData.put("data", videoUrl);
        }
        return douYData;
    }
    public static String processContent(String content) {
        String douyinUrl = extractMatch(content, "v\\.douyin\\.com/[a-zA-Z0-9]+");
        if (douyinUrl != null) {
            return "https://" + douyinUrl;
        }
        String digit = extractMatch(content, "\\d{19}");
        return digit;
    }
    private static String extractMatch(String content, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        return matcher.find() ? matcher.group(0) : null;
    }
    public static boolean isNumeric(String str) {
        return str.matches("\\d+");
    }
    private static String extractUrl(String text) {
        // 匹配网址的正则表达式
        String regex = "https?://\\S+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        // 查找匹配的链接
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
    public static String extractVideoId(String link) {
        String redirectLink = getRedirectUrl(link);
        Pattern patternVideo = Pattern.compile("/video/(\\d+)");
        Pattern patternNote = Pattern.compile("/note/(\\d+)");
        Matcher matcherVideo = patternVideo.matcher(redirectLink);
        Matcher matcherNote = patternNote.matcher(redirectLink);
        if (matcherVideo.find()) {
            return matcherVideo.group(1);
        } else if (matcherNote.find()) {
            return matcherNote.group(1);
        } else {
            return null;
        }
    }
    public static String getRedirectUrl(String url) {
        OkHttpClient client = new OkHttpClient();
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            return response.request().url().toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    private static JSONArray serverGetDy(String itemIds){
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        Request Request = new Request.Builder()
                //不知道这个a_bogus是啥 可能是个算法 随便填写或空即可
                .url("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?reflow_source=reflow_page&item_ids="+itemIds+"&a_bogus=sdfds")
                .addHeader("content-type", "application/x-www-form-urlencoded")
                .get()
                .build();
        try {
            Response response = client.newCall(Request).execute();
            JSONObject jsonObject= JSONObject.parseObject(response.body().string());
            if (jsonObject.getInteger("status_code").equals(0)){
                return jsonObject.getJSONArray("item_list");
            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
//package cc.mrbird.febs.mall.test;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Response;
//
//import java.io.IOException;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//public class DYDelMark {
//    public static void main(String[] args) {
//        String content = "https://v.douyin.com/iLwxgYHE/";
//        JSONObject douYData = extractDouYinData(content);
//        if (douYData != null) {
//            System.out.println(douYData);
//        } else {
//            System.out.println(1);
//        }
//    }
//    private static JSONObject extractDouYinData(String content) {
//        String url = extractUrl(content);
//
//        if (url != null) {
//            if (content.contains("douyin.com")) {
//                if (content.contains("live.douyin.com")) {
//                    System.out.println("直播");
//                    return null;
//                } else {
//                    String dyUrlOrId = processContent(content);
//                    String videoId = isNumeric(dyUrlOrId) ? dyUrlOrId : extractVideoId(dyUrlOrId);
//                    if (videoId!=null){
//                        JSONArray dyData = serverGetDy(videoId);
//
//                        if (dyData != null && dyData.size() > 0) {
//                            JSONObject jsonData = dyData.getJSONObject(0);
//                            return buildDouYinData(jsonData);
//                        }
//                    }
//                }
//            }
//        }
//        return null;
//    }
//    private static JSONObject buildDouYinData(JSONObject jsonData) {
//        JSONObject douYData = new JSONObject();
//        douYData.put("desc", jsonData.getString("desc"));
//
//        JSONObject author = jsonData.getJSONObject("author");
//        douYData.put("nickname", author.getString("nickname"));
//
//        JSONObject videoJson = jsonData.getJSONObject("video");
//        JSONObject coverJson = videoJson.getJSONObject("cover");
//        String coverUrl = coverJson.getJSONArray("url_list").getString(0);
//        douYData.put("coverUrl", coverUrl);
//
//        if (jsonData.getJSONArray("images") != null) {
//            JSONArray images = jsonData.getJSONArray("images");
//            JSONArray imageArray = new JSONArray();
//
//            for (int i = 0; i < images.size(); i++) {
//                JSONObject image = images.getJSONObject(i);
//                JSONArray urlList = image.getJSONArray("url_list");
//                imageArray.add(urlList.get(0));
//            }
//
//            douYData.put("type", 2);
//            douYData.put("data", imageArray);
//        } else {
//            JSONObject playAddr = videoJson.getJSONObject("play_addr");
//            String uri = playAddr.getString("uri");
//            String videoUrl = "https://www.iesdouyin.com/aweme/v1/play/?video_id=" + uri + "&ratio=1080p&line=0";
//            douYData.put("type", 1);
//            douYData.put("data", videoUrl);
//        }
//
//        return douYData;
//    }
//    public static String processContent(String content) {
//        String douyinUrl = extractMatch(content, "v\\.douyin\\.com/[a-zA-Z0-9]+");
//        if (douyinUrl != null) {
//            return "https://" + douyinUrl;
//        }
//        String digit = extractMatch(content, "\\d{19}");
//        return digit;
//    }
//    private static String extractMatch(String content, String regex) {
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(content);
//        return matcher.find() ? matcher.group(0) : null;
//    }
//    public static boolean isNumeric(String str) {
//        return str.matches("\\d+");
//    }
//    private static String extractUrl(String text) {
//        // 匹配网址的正则表达式
//        String regex = "https?://\\S+";
//        Pattern pattern = Pattern.compile(regex);
//        Matcher matcher = pattern.matcher(text);
//
//        // 查找匹配的链接
//        if (matcher.find()) {
//            return matcher.group();
//        }
//
//        return null;
//    }
//    public static String extractVideoId(String link) {
//        String redirectLink = getRedirectUrl(link);
//        Pattern patternVideo = Pattern.compile("/video/(\\d+)");
//        Pattern patternNote = Pattern.compile("/note/(\\d+)");
//
//        Matcher matcherVideo = patternVideo.matcher(redirectLink);
//        Matcher matcherNote = patternNote.matcher(redirectLink);
//
//        if (matcherVideo.find()) {
//            return matcherVideo.group(1);
//        } else if (matcherNote.find()) {
//            return matcherNote.group(1);
//        } else {
//            return null;
//        }
//    }
//    public static String getRedirectUrl(String url) {
//        OkHttpClient client = new OkHttpClient();
//        try {
//            Request request = new Request.Builder()
//                    .url(url)
//                    .get()
//                    .build();
//            Response response = client.newCall(request).execute();
//            return response.request().url().toString();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return null;
//    }
//    private static JSONArray serverGetDy(String itemIds){
//        OkHttpClient client = new OkHttpClient().newBuilder().build();
//        Request Request = new Request.Builder()
//                //不知道这个a_bogus是啥 可能是个算法 随便填写或空即可
//                .url("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?reflow_source=reflow_page&item_ids="+itemIds+"&a_bogus=sdfds")
//                .addHeader("content-type", "application/x-www-form-urlencoded")
//                .get()
//                .build();
//        try {
//            Response response = client.newCall(Request).execute();
//            JSONObject jsonObject= JSONObject.parseObject(response.body().string());
//            if (jsonObject.getInteger("status_code").equals(0)){
//                return jsonObject.getJSONArray("item_list");
//            }
//            return null;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//}