admin
2021-07-30 19533a17aa55fafc70d0a385928e785cb50e1ebc
src/main/java/com/yeshi/buwan/tencent/TencentVideoUtil.java
@@ -1,186 +1,136 @@
package com.yeshi.buwan.tencent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoResource;
import com.yeshi.buwan.domain.entity.PlayUrl;
import com.yeshi.buwan.domain.video.InternetSearchVideo;
import com.yeshi.buwan.service.imp.VideoResourceService;
import com.yeshi.buwan.service.imp.VideoTypeService;
import com.yeshi.buwan.service.inter.juhe.InternetSearchVideoService;
import com.yeshi.buwan.service.inter.juhe.TencentVideoService;
import com.yeshi.buwan.tencent.entity.TencentCoverInfo;
import net.sf.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.yeshi.utils.HttpUtil;
import com.yeshi.buwan.tencent.entity.TencentCoverVideo;
import com.yeshi.buwan.tencent.entity.TencentSearchVideoMap;
import com.yeshi.buwan.util.factory.VideoInfoFactory;
import com.yeshi.buwan.util.video.VideoConstant;
import com.yeshi.buwan.youku.YouKuUtil;
import org.springframework.stereotype.Component;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Component
public class TencentVideoUtil {
    static ScriptEngine jsEngine = null;
//    static {
//        if (jdGoodsJs == null)
//            jdGoodsJs = getJDGoodsJS();
//        ScriptEngineManager manager = new ScriptEngineManager();
//        jsEngine = manager.getEngineByName("javascript");
//        try {
//            jsEngine.eval(jdGoodsJs);
//        } catch (ScriptException e) {
//            e.printStackTrace();
//        }
//
//        if (jsEngine instanceof Invocable) {
//            Invocable in = (Invocable) jsEngine;
//            Object goodsId = in.invokeFunction("getGoodsId", link);
//            if (goodsId != null)
//                return goodsId.toString().trim();
//        }
//
//    }
    @Resource
    private InternetSearchVideoService internetSearchVideoService;
    @Resource
    private TencentVideoService tencentVideoService;
    private static List<String> objToArray(JSONObject root, String key) {
    @Resource
    private VideoResourceService videoResourceService;
        if (root.optJSONObject(key) == null) {
            return null;
        }
    @Resource
    private VideoTypeService videoTypeService;
        Gson gson = new Gson();
    public final static int RESOURCE_ID = 17;
        List<String> directors = new ArrayList<>();
        if (root.optJSONObject(key).isArray()) {
            Type type = new TypeToken<List<String>>() {
            }.getType();
            directors = gson.fromJson(root.optJSONArray(key).toString(), type);
        } else {
            JSONObject director = root.optJSONObject(key);
            for (Iterator<String> its = director.keys(); its.hasNext(); ) {
                String p = its.next();
                directors.add(director.optString(p));
            }
        }
        root.remove(key);
        return directors;
    }
    public static TencentCoverInfo getCoverInfo(String url) throws Exception {
        String script = null;
        Document doc = Jsoup.connect(url).timeout(10000).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36").get();
        Elements els = doc.getElementsByTag("script");
        for (int i = 0; i < els.size(); i++) {
            if (els.get(i).html().indexOf("var COVER_INFO") > -1) {
                script = els.get(i).html();
    public static VideoDetailInfo convertToDetail(TencentCoverVideo tencentCoverVideo, TencentCoverInfo coverInfo) {
        VideoDetailInfo detail = new VideoDetailInfo();
        switch (coverInfo.getType_name()) {
            case "电影":
                detail.setTag(coverInfo.getTitle());
                break;
            }
            case "电视剧":
            case "动漫":
                detail.setTag(tencentCoverVideo.getStage() + "");
                break;
            case "综艺":
                detail.setTag(tencentCoverVideo.getDate());
                break;
        }
        detail.setName(coverInfo.getTitle());
        detail.setExtraId(tencentCoverVideo.getVideoId());
        detail.setType("url");
        return detail;
    }
        script = script.replace("window.", "_window.");
        script = " var _window={};" + "var document={getElementsByTagName:function(tag){return [''];}};" + script;
        script += "\n function getCoverInfo(){return COVER_INFO }; function getVideoInfo(){return VIDEO_INFO}";
        ScriptEngineManager manager = new ScriptEngineManager();
        jsEngine = manager.getEngineByName("javascript");
        try {
            jsEngine.eval(script);
        } catch (ScriptException e) {
            e.printStackTrace();
    public VideoInfo getVideoInfo(InternetSearchVideo internetSearchVideo, Integer pageSize) throws Exception {
        TencentSearchVideoMap map = tencentVideoService.selectMapByVideoId(internetSearchVideo.getId());
        if (map == null) {
            throw new Exception("视频源不存在");
        }
        Gson gson = new Gson();
        if (jsEngine instanceof Invocable) {
            Invocable in = (Invocable) jsEngine;
            Object coverInfo = in.invokeFunction("getCoverInfo");
            JSONObject root = JSONObject.fromObject(gson.toJson(coverInfo));
            System.out.println(root.toString());
            List<TencentCoverInfo.VipIdsBean> vipIdsBeans = new ArrayList<>();
            if (root.optJSONObject("vip_ids").isArray()) {
                Type type = new TypeToken<List<TencentCoverInfo.VipIdsBean>>() {
                }.getType();
                vipIdsBeans = gson.fromJson(root.optJSONArray("vip_ids").toString(), type);
            } else {
                JSONObject vipIds = root.optJSONObject("vip_ids");
                for (Iterator<String> its = vipIds.keys(); its.hasNext(); ) {
                    String p = its.next();
                    TencentCoverInfo.VipIdsBean idsBean = gson.fromJson(vipIds.optJSONObject(p).toString(), TencentCoverInfo.VipIdsBean.class);
                    idsBean.setP(Integer.parseInt(p));
                    vipIdsBeans.add(idsBean);
                }
            }
            root.remove("vip_ids");
            try {
                List<String> subTypes = objToArray(root, "subtype");
                List<String> directors = objToArray(root, "director");
                List<String> leadingActor = objToArray(root, "leading_actor");
                List<String> subGenre = objToArray(root, "sub_genre");
                TencentCoverInfo coverInfoBean = gson.fromJson(root.toString(), TencentCoverInfo.class);
                coverInfoBean.setVip_ids(vipIdsBeans);
                coverInfoBean.setSubtype(subTypes);
                coverInfoBean.setDirector(directors);
                coverInfoBean.setLeading_actor(leadingActor);
                coverInfoBean.setSub_genre(subGenre);
                return coverInfoBean;
            } catch (Exception e) {
                e.printStackTrace();
            }
//            System.out.println(coverInfoBean);
        TencentCoverInfo showDetail = tencentVideoService.getSimpleCoverDetail(map.getCoverId());
        showDetail.setVideoList(tencentVideoService.getVideoList(showDetail.getId(), 1, pageSize));
        VideoInfo info = VideoInfoFactory.create(internetSearchVideo);
        info.setIntroduction(showDetail.getDescription());
        info.setVideoDetailList(convertToDetail(showDetail.getVideoList(), showDetail));
        info.setPlayPicture(info.getHpicture());
        //获取资源列表
        List<String> rids = Arrays.asList(internetSearchVideo.getResourceIds().split(","));
        List<VideoResource> resourceList = videoResourceService.getResource(rids);
        for (VideoResource vr : resourceList) {
            if (vr.getId().equalsIgnoreCase(RESOURCE_ID + ""))
                vr.setChecked(true);
        }
        return null;
        info.setResourceList(resourceList);
        info.setShowType(YouKuUtil.getShowType(internetSearchVideo));
        info.setScore(new BigDecimal(showDetail.getScore().getScore()).setScale(1).toString());
        //获取分类详情
        info.setVideoType(videoTypeService.getVideoType(info.getVideoType().getId()));
        info.setCommentCount(0);
        info.setVideocount((int) (Math.random() * 1000));
        return info;
    }
    private static void parseList(String url) {
        try {
            Document document = Jsoup.connect(url).timeout(5000).get();
            Elements els = document.getElementsByAttributeValue("class", "list_item");
            for (int i = 0; i < els.size(); i++) {
                String href = els.get(i).getElementsByTag("a").get(0).attr("href");
                String img = els.get(i).getElementsByTag("img").get(0).attr("src");
                img = img.startsWith("http") ? img : "http:" + img;
                try {
                    TencentCoverInfo coverInfo = getCoverInfo(href);
                    System.out.println(coverInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
    public static List<VideoDetailInfo> convertToDetail(List<TencentCoverVideo> tencentCoverVideoList, TencentCoverInfo coverInfo) {
        List<VideoDetailInfo> detailList = new ArrayList<>();
        for (TencentCoverVideo video : tencentCoverVideoList) {
            detailList.add(convertToDetail(video, coverInfo));
        }
        return detailList;
    }
    public static void main(String[] args) {
//        try {
//            TencentCoverInfo info = getCoverInfo("https://v.qq.com/x/cover/mzc00200fw94jce.html");
//
//            info = getCoverInfo("https://v.qq.com/x/cover/mzc00200s0ii272/r0036xibgw2.html");
//            info = getCoverInfo("https://v.qq.com/x/cover/ylgl3m6wo0sypou/w0036x9c5c7.html");
//            info = getCoverInfo("https://v.qq.com/x/cover/ylgl3m6wo0sypou/w0036x9c5c7.html");
//            //
//            info = getCoverInfo("https://v.qq.com/x/cover/mzc00200tlv15ub.html");
//            System.out.println(info);
//        } catch (Exception e) {
//            e.printStackTrace();
    public static int getShowType(InternetSearchVideo video) {
        if (video == null)
            return 2;
//        }
        int page = 1;
        parseList(String.format("https://v.qq.com/x/bu/pagesheet/list?_all=1&append=1&channel=cartoon&ipay=2&listpage=%s&offset=%s&pagesize=30&sort=18", page, (page - 1) * 30));
        switch (video.getRootType()) {
            case VideoConstant
                    .VIDEO_CATEGORY_DIANYING:
                return 1;
            case VideoConstant
                    .VIDEO_CATEGORY_DIANSHIJU:
            case VideoConstant
                    .VIDEO_CATEGORY_DONGMAN:
                return 2;
            case VideoConstant
                    .VIDEO_CATEGORY_ZONGYI:
                return 1;
        }
        return 2;
    }
    public PlayUrl getPlayUrl(String detailSystemId, String id, String type, int resourceid, String videoid) {
        PlayUrl playUrl = new PlayUrl();
        VideoResource videoResource = videoResourceService.getResource(resourceid + "");
        playUrl.setResource(videoResource);
        TencentCoverVideo tencentCoverVideo = tencentVideoService.getVideoDetail(id);
        playUrl.setUrl(tencentCoverVideo.getLink());
        playUrl.setPlayType(1);
        playUrl.setParams("");
        return playUrl;
    }
}