当前位置: 首页 > article >正文

springboot 整合酷狗获取MV视频最高画质(使用自己账户)

在此声明,本内容仅供个人学习、研究或娱乐之用,严禁任何形式的商业用途。若您发现本内容被用于商业目的,请立即删除,及时与小编联系,我们将删除原代码。

请根据上一篇文章使用该代码:SpringBoot 整合酷狗获取下载音乐(需要自己账户)-CSDN博客

1.KuGouUtil 工具类

    /**
     * mv搜索验签 (源码)
     */
    public static String mvMd5SignSearch(String timestamp,KuGouHand hand) {
        String[] signatureList = {
                "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
                "appid="+hand.getAppid(),
                "clienttime="+timestamp,
                "clientver=20000",
                "dfid="+hand.getDfid(),
                "mid="+hand.getMid(),
                "mvid="+hand.getMvid(),
                "srcappid="+hand.getSrcappid(),
                "userid="+hand.getUserid(),
                "uuid="+hand.getUuid(),
                "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        };
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : signatureList) {
            stringBuilder.append(s);
        }
        String string = stringBuilder.toString();
        return getMD5(string);
    }

    /**
     * 获取Mv信息 (源码)
     */
    public static List<KuGoMv> getKuGouMvList(Headers headers,long timestamp, KuGouHand hand) throws IOException {
        List<KuGoMv> kuGoMvList = new ArrayList<>();
        String sign = mvMd5SignSearch(String.valueOf(timestamp),hand);
        LinkedHashMap<String, String> param = new LinkedHashMap<>();
        param.put("srcappid", hand.getSrcappid());
        param.put("clientver", "20000");
        param.put("clienttime", String.valueOf(timestamp));
        param.put("mid", hand.getMid());
        param.put("uuid", hand.getUuid());
        param.put("dfid",hand.getDfid());
        param.put("appid", hand.getAppid());
        param.put("mvid", hand.getMvid());
        param.put("userid", hand.getUserid());
        param.put("signature", sign);

        OkHttpClient client = new OkHttpClient();
        String url = "https://gateway.kugou.com/mixedvideo/v1/relatedVideo/list" + GetParams(param);

        Request request = new Request.Builder()
                .url(url)
                .headers(headers)
                .build();
        Response response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {}.getType();
            Map<String, Object> map = gson.fromJson(responseBody, type);
            if (String.valueOf(map.get("errcode")) =="0.0" || String.valueOf(map.get("errcode"))=="0"){
                kuGoMvList = JSON.parseArray(JSON.toJSONString(map.get("data")), KuGoMv.class);
                for (KuGoMv item:kuGoMvList){
                 item.setPlayDuration(getSeconds(item.getDuration()/1000));
                }
                return kuGoMvList;
            }
        }
        return kuGoMvList;
    }


    /**
     * 获取MV 播放地址的MD5验签 (源码)
     */
    public static String setMvMd5Sign(long timestamp,KuGouHand hand) {
        String[] signatureList = {
                "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
                "appid="+hand.getAppid(),
                "clienttime="+timestamp,
                "clientver=1000",
                "dfid="+hand.getDfid(),
                "id="+hand.getMvid(),
                "mid="+hand.getMid(),
                "srcappid="+hand.getSrcappid(),
                "uuid="+hand.getUuid(),
                "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        };
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : signatureList) {
            stringBuilder.append(s);
        }
        String string = stringBuilder.toString();
        return getMD5(string);
    }


    /**
     * 获取酷狗音乐播放地址MV (源码)
     */
    public static KuGoMv getKuGouMvMp4(Headers headers,long timestamp,KuGouHand hand,KuGoMv kuGoMv) throws IOException {
        String sign = setMvMd5Sign(timestamp,hand);
        LinkedHashMap<String, String> param = new LinkedHashMap<>();
        param.put("srcappid", hand.getSrcappid());
        param.put("clientver", "1000");
        param.put("clienttime", String.valueOf(timestamp));
        param.put("mid", hand.getMid());
        param.put("uuid", hand.getUuid());
        param.put("dfid",hand.getDfid());
        param.put("appid", hand.getAppid());
        param.put("id", hand.getMvid());
        param.put("signature", sign);

        OkHttpClient client = new OkHttpClient();
        String url = "https://wwwapi.kugou.com/play/mv" + GetParams(param);
        Request request = new Request.Builder()
                .url(url)
                .headers(headers)
                .build();
        Response response = client.newCall(request).execute();
        String responseBody = response.body().string();
        if (response.isSuccessful()) {
            Gson gson = new Gson();
            Type type = new TypeToken<Map<String, Object>>() {}.getType();
            Map<String, Object> map = gson.fromJson(responseBody, type);
            String code = map.get("status").toString().replace(".0","");
            if (code.equals("1")){
                Map<String, Object> map2 = gson.fromJson(JSON.toJSONString(map.get("data")), type);
                Type type2 = new TypeToken<Map<String, Map<String,Object>>>() {}.getType();
                HashMap<String,Map<String,Object>> privileges = JSON.parseObject(JSON.toJSONString(map2.get("privilege")), type2);
                HashMap<String,Map<String,Object>> plays = JSON.parseObject(JSON.toJSONString(map2.get("play")), type2);
                if (privileges.size()>=1 && plays.size()>=1){
                    Integer level = 0;
                    String hashName = "";
                    for (Map.Entry<String, Map<String, Object>> entry : privileges.entrySet()) {
                        Map<String, Object> value = entry.getValue();
                        Integer innerValue = Integer.parseInt(String.valueOf(value.get("level")).replace(".0",""));
                        if (innerValue >=level){
                            level=innerValue;
                            hashName = entry.getKey();
                            String[] mvList = String.valueOf(value.get("name")).split("-");
                            kuGoMv.setMvName(mvList[1]);
                            kuGoMv.setSingerName(String.valueOf(value.get("singer_name")));
                            kuGoMv.setLevel(innerValue);
                        }
                    }
                    for (Map.Entry<String, Map<String, Object>> entry : plays.entrySet()) {
                        Map<String, Object> value = entry.getValue();
                        if (entry.getKey().equals(hashName)){
                            kuGoMv.setDownUrl(String.valueOf(value.get("downurl")));
                            kuGoMv.setPlayUrl(value.get("backupdownurl"));
                        }
                    }
                    return kuGoMv;
                }

            }
        }
        return kuGoMv;
    }

 2.ToolUtil

   /**
     * duration 时长转分秒
     */
    public static String getSeconds(Integer num){
        try {
            long totalSeconds = Duration.ofSeconds(num).getSeconds();
            long minutes = totalSeconds / 60;
            long seconds = totalSeconds % 60;
            return String.format("%d:%02d", minutes, seconds);
        }catch (Exception e){
            return String.valueOf(num);
        }
    }


    /**
     * 参数装换GET
     */
    public static String GetParams(Map<String, String> params) {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (result.length() > 0) {
                result.append("&");
            }
            result.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return "?"+result.toString();
    }


  /**
     * 获取所有请求头信息
     * type true 代表开启条件设置
     */
    public static Headers getHeaders(HttpServletRequest request,String[] strings,Boolean type ) {
        Headers.Builder headersBuilder = new Headers.Builder();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            if (type){
                if (strings.toString().contains(headerName)) {
                    Enumeration<String> v = request.getHeaders(headerName);
                    while (v.hasMoreElements()) {
                        String value = v.nextElement();
                        headersBuilder.add(headerName, value);
                    }
                }
            }else {
                Enumeration<String> v = request.getHeaders(headerName);
                while (v.hasMoreElements()) {
                    String value = v.nextElement();
                    headersBuilder.add(headerName, value);
                }
            }
        }
        return headersBuilder.build();
    }


http://www.kler.cn/a/316372.html

相关文章:

  • MySQL的SQL书写顺序和执行顺序
  • python 同时控制多部手机
  • C++《继承》
  • C++模板特化实战:在使用开源库boost::geometry::index::rtree时,用特化来让其支持自己的数据类型
  • uni-app移动端与PC端兼容预览PDF文件
  • 软件测试面试八股文(超详细整理)
  • 2024人工智能结课作业-DFS/BFS/Astar解决数码问题
  • Linux主流Web服务器:你选择哪一款?
  • 华为---以太网静态路由配置使用下一跳通信正常,而使用出接口无法通信
  • React项目实战(React后台管理系统、TypeScript+React18)
  • 锤炼核心技能以应对编程革命
  • threejs加载高度图渲染点云,不支持tiff
  • 【Linux:共享内存】
  • C# 从字符串中分离文件路径、文件名及扩展名
  • C# winforms DataGridView设置数据源自动显示表格
  • Go 第二期
  • 界面控件Telerik UI for WinForms 2024 Q3概览 - 支持合并单元格等
  • (已解决)vscode如何选择python解释器
  • 【学习笔记】SSL/TLS安全机制之HSTS
  • apt-get install 安装的tomcat配置
  • biopython提取.cif文件的变换矩阵
  • spring全家桶使用教程
  • Kotlin 操作符 in 的基本使用(十)
  • docker存储
  • 2.《DevOps》系列K8S部署CICD流水线之部署NFS网络存储与K8S创建StorageClass
  • 2016年国赛高教杯数学建模A题系泊系统的设计解题全过程文档及程序