Java得到小破站的视频
函数作用:得到重定向的链接
public static String getrealurl(String str,Property d)
{
HttpURLConnection httpURLConnection;
HttpsURLConnection conn;
try {
URL url = new URL(str);
trustAllHosts();
if (url.getProtocol().toLowerCase().equals("https")) {
conn=(HttpsURLConnection)url.openConnection();
conn.setHostnameVerifier(DO_NOT_VERIFY);
httpURLConnection=conn;
}
else
httpURLConnection=(HttpURLConnection)url.openConnection();
d.setProperty(httpURLConnection);
String urls=httpURLConnection.getHeaderField("location");
httpURLConnection.disconnect();
return urls;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
函数作用:得到网页内容以及下载文件
public static String get_https(
String urls,//网址
Property pro,//请求头
boolean flag,//是否截断
String path//保存位置
)
{
HttpURLConnection https;
HttpsURLConnection httpssl;
try {
URL url = new URL(urls);
trustAllHosts();
if (url.getProtocol().toLowerCase().equals("https")) {
httpssl=(HttpsURLConnection) url.openConnection();
//ALLOW_ALL_HOSTNAME_VERIFIER
httpssl.setHostnameVerifier(DO_NOT_VERIFY);
https=httpssl;
}
else
https=(HttpURLConnection) url.openConnection();
https.setRequestMethod("GET");
pro.setProperty(https);
https.setConnectTimeout(2000);
int ttts=https.getResponseCode();
if(ttts==302)
{
return get_https(https.getHeaderField("location"), pro,flag, path);
}
else if(ttts!=404&&ttts!=502&&ttts!=403)
{
InputStream IS=https.getInputStream();
byte[] bt=new byte[1024];
int len=0;
FileOutputStream ttt = null;
if(path!=null)
{
ttt=new FileOutputStream(path);
}
ByteArrayOutputStream BS=new ByteArrayOutputStream();
int all=0;
while((len=IS.read(bt))!=-1)
{
if(flag)
{
if(all>6144)
{
BS.write(bt, 0, len);
}
all+=len;
if(all>8192)
break;
}
else
{
BS.write(bt, 0, len);
}
if(path!=null)
ttt.write(bt,0,len);
}
IS.close();
if(path!=null)
{
ttt.close();
return null;
}
String h=new String(BS.toByteArray(),"UTF-8");
BS.close();
BS=null;
return h;
}
else
{
System.out.println("error:"+urls);
throw new Exception("连接失败!"+ttts);
}
} catch (Exception e) {
System.out.println(path);
e.printStackTrace();
}
return null;
}
附加
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
public static void trustAllHosts() {
final String TAG = "trustAllHosts";
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//System.out.println(TAG+"checkClientTrusted");
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//System.out.println(TAG+"checkServerTrusted");
}
}};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
//e.printStackTrace();
}
}
接口作用:用于设置https的请求头和方法
public interface Property {
public abstract void setProperty(HttpsURLConnection https);
public abstract void setProperty(HttpURLConnection https);
}
类的作用:用于储存数据
package youget;
public class BiliPT {
public String title,Cid,Bvid;
public BiliPT(String title, String Cid,String Bvid) {
this.title=title;
this.Cid=Cid;
this.Bvid=Bvid;
}
}
函数作用:找到关键内容
public static List<String> findall(String code,String starts,String ends)
{
List<String> temp=new ArrayList<String>();
int start=0;
int end=0;
while(true)
{
start=code.indexOf(starts,end);
if(start==-1)
break;
start+=starts.length();
end=code.indexOf(ends,start);
temp.add(code.substring(start,end));
end+=ends.length();
}
return temp;
}
函数作用:通过网址得到Bvid
public static String Bilibili_Get_Bvid_By_Url(String bilibiliurl)
{
int start=bilibiliurl.indexOf("https://www.bilibili.com/video/");
if(start==-1)
return null;
start+=31;
int end=bilibiliurl.indexOf("?",start);
String Bvid=null;
if(end==-1)
Bvid=bilibiliurl.substring(start);
else
Bvid=bilibiliurl.substring(start, end);
return Bvid;
}
函数作用:通过获得的Bvid得到Cid
public static void Bilibili_Get_Vedio_Cid_By_Bvid(String Bvid,List<BiliPT> out,
Property dy)
{
int start=0,end=0;
String cid=null,title=null,bvid=null;
String url="https://api.bilibili.com/x/web-interface/view?bvid="+Bvid;
String h=WebGet.get_https(url, dy, false, null);
if(h==null)
return;
start=h.indexOf("title\":\"",end);
if(start==-1)
return;
start+=8;
end=h.indexOf("\"",start);
title=h.substring(start, end);
start=h.indexOf("cid\":",end)+5;
end=h.indexOf(",",start);
cid=h.substring(start, end);
start=h.indexOf("\"episodes\":");
if(start==-1)
{
out.add(new BiliPT(title, cid, Bvid));
return;
}
h=h.substring(start);
end=0;
for(int i=0;true;i++)
{
start=h.indexOf("cid\":",end);
if(start==-1)
break;
start+=5;
end=h.indexOf(",",start);
cid=h.substring(start, end);
start=h.indexOf("title\":\"",end)+8;
end=h.indexOf("\"",start);
title=h.substring(start, end);
start=h.indexOf("bvid\":\"",end)+7;
end=h.indexOf("\"",start);
bvid=h.substring(start, end);
out.add(new BiliPT(title,cid,bvid));
cid=null;
title=null;
bvid=null;
}
}
public static void Bilibili_Get_Vedio_Cid_By_Bvid(String Bvid,List<BiliPT> out)
{
//https://www.bilibili.com/video/BV1VY4y1e7xP
allProperty.douyin dy=new allProperty().new douyin();
Bilibili_Get_Vedio_Cid_By_Bvid(Bvid,out,dy);
}
函数作用:通过Bvid和Cid的到视频下载链接
public static String Bilibili_Get_Vedio_Url_By_Bvid_and_Cid(String Bvid,String Cid)
{
allProperty.douyin dy=new allProperty().new douyin();
return Bilibili_Get_Vedio_Url_By_Bvid_and_Cid(Bvid,Cid,dy);
}
public static String Bilibili_Get_Vedio_Url_By_Bvid_and_Cid(String Bvid,String Cid,Property pro)
{
String url="https://api.bilibili.com/x/player/playurl?otype=json&fnver=0&fnval=2&player=1&qn=64&bvid="+Bvid+"&cid="+Cid;
String h=WebGet.get_https(url, pro, false,null);
if(h==null)
return null;
int start=h.indexOf("url\":\"");
if(start==-1)
return null;
start+=6;
int end=h.indexOf("\"",start);
h=h.substring(start,end);
h=h.replace("\\u0026", "&");
return h;
}
附加:B站视频请求头设置(不然会被403)
public class bilibili implements Property
{
String Referer;
public bilibili()
{
Referer="https://www.bilibili.com/";
}
public void setReferer(String Bvid)
{
Referer="https://www.bilibili.com/video/"+Bvid;
}
@Override
public void setProperty(HttpsURLConnection https) {
// TODO 自动生成的方法存根
setProperty((HttpURLConnection)https);
}
@Override
public void setProperty(HttpURLConnection https) {
// TODO 自动生成的方法存根
//https.setInstanceFollowRedirects(false);
//https.setRequestProperty("Sec-Fetch-Mode", "no-cors");
https.setRequestProperty("Accept-Encoding","identity");
https.setRequestProperty("Accept","*/*");
https.setRequestProperty("Connection","close");
https.setRequestProperty("Accept-Language","en-US,en;q=0.5");
https.setRequestProperty("Referer", this.Referer);
https.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
}
}
类的作用:用于储存番剧信息
package youget;
public class bangumiPT {
public String cid,aid,epid,title;
public bangumiPT(String title,String cid,String aid,String epid)
{
this.title=title;
this.aid=aid;
this.cid=cid;
this.epid=epid;
}
}
函数作用:通过Url得到番剧列表,得到Aid,Cid,Epid
public static void Bilibili_Get_Bangumi_By_Url(String bilibiliurl,List<bangumiPT> out)
{
allProperty.putong pt=new allProperty().new putong();
String h=WebGet.get_https(bilibiliurl, pt, false, null);
int start=h.indexOf("episodes");
int end=h.indexOf("episodes",start+100);
h=h.substring(start,end);
end=0;
for(int i=0;true;i++)
{
start=h.indexOf("aid\":",end);
if(start==-1)
break;
start+=5;
end=h.indexOf(",",start);
String aid=h.substring(start,end);
start=h.indexOf("cid\":",end)+5;
end=h.indexOf(",",start);
String cid=h.substring(start,end);
start=h.indexOf("\\u002Fep",end)+8;
end=h.indexOf("\"",start);
String epid=h.substring(start,end);
start=h.indexOf("title\":\"",end)+8;
end=h.indexOf("\",\"",start);
String title=h.substring(start,end);
out.add(new bangumiPT(title, cid, aid, epid));
}
}
函数作用:通过Aid,Epid,Cid得到视频下载链接
public static String Bilibili_Get_Bangumi_Url_By_Aid_and_Epid_and_Cid(String aid,
String epid,String cid,
Property pro)
{
String url="https://api.bilibili.com/pgc/player/web/playurl?avid="+aid+"&cid="
+cid+"&qn=112&type=&otype=json&ep_id="+epid+"&fnver=0&fnval=8";
String h=WebGet.get_https(url, pro,false,null);
if(h==null)
return null;
int start=h.indexOf("\"url\":\"");
if(start==-1)
return null;
start+=7;
int end=h.indexOf("\"",start);
h=h.substring(start,end);
h=h.replace("\\u0026", "&");
return h;
}
public static String Bilibili_Get_Bangumi_Url_By_Aid_and_Epid_and_Cid(String aid,
String epid,String cid)
{
allProperty.putong pt=new allProperty().new putong();
return Bilibili_Get_Bangumi_Url_By_Aid_and_Epid_and_Cid(aid,epid,cid,pt);
}