沒有調用第三方接口,純Java代碼完成
使用IDEA編輯器直接打開Douyin文件夾,文件夾已打包上傳到網盤
下載地址:https://www.lanzous.com/i4id9mb
Tools.java
package com.lhr;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class Tools{
private String cookies="odin_tt=9a16fa42e650a96379a5901a3d146c7c244dc0c35971927f6e13c208fc4bcf9cc952542516f78dc9098ac4d179f3b127cddfdff2942d259dda9ca33de8ae7677; install_id=43619087057; ttreq=1$4c4b4cc4b31e6f2f4203b62a1df12b43e224434c; qh[360]=1";
public Tools(){
}
/**
*
* 這里獲取作品ID
* */
public String getId (String url){
String result=sendGet(url);
result=getSubString(result,"/share/video/","/?");
return result;
}
/**
* 解析真實地址返回的數據其實是json格式的,Java語言本身不支持json解析,需要借助第三方jar
*
* 這里就直接使用getsubstring
*
* */
public String getUrl (String url){
String result=sendGet(url);
result=getSubString(result,"play_addr_lowbr","width");
result=getSubString(result,"url_list\":[\"","\",\"");
return result;
}
/**
* 取出中間文本
*
* */
private String getSubString(String str,String left,String right){
String strRight="";
int indexLeft = str.indexOf(left);
if(indexLeft==-1){
return "";//沒有找到直接返回空以免出現異常
}else{
strRight=str.substring(indexLeft);
}
int length=str.length()-strRight.length();
int indexRight = strRight.indexOf(right);
if (indexRight==-1){
return "";
}
String result=str.substring(length+left.length(),length+indexRight);
return result;
}
private String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
//打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
//設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Accept-Encoding", "utf-8");
connection.setRequestProperty("Host", "api-hl.amemv.com");
connection.setRequestProperty("user-agent","okhttp/3.10.0.1");
connection.setRequestProperty("cookie",this.cookies);
//建立實際的連接
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
}catch(Exception e) {
//發送異常
return "發送失敗,請檢查URL地址是否正確";
}finally{
try{
if(in != null){
in.close();
}
}catch(Exception e2) {
//關閉異常
System.out.println("關閉異常");
}
}
return result;
}
}