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();
}