Java 版抖音解析介面

lizhiqiang666發表於2020-05-18

沒有呼叫第三方介面,純Java程式碼完成

使用IDEA編輯器直接開啟Douyin資料夾,資料夾已打包上傳到網盤

下載地址: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;

    }
    }

碼雲連結:

gitee.com/lhr0321/20175318_JAVA/tr...

www.cnblogs.com/L1079991001/p/1099...

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章