抖音无水印解析API

 翻译一个JAVA版API解析

 

 

package com.demo;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

public class Testce {


    private static String url = "http://v.douyin.com/AaP8m6/";
    private static String [] DEVICEINFOS = {"iid=43619087057&device_id=57318346369","iid=74655440239&device_id=57318346369"};

    public static void main(String[] args) {

        parseVideoByLink(url);

    }


    public static void parseVideoByLink(String url) {

        String awemeId = getAwemeId(url);
        int api_n = 4; //控制每次取得的设备信息数量
        for(String s : getApis(4)){
            String req_url = s + awemeId;
            System.out.println("请求地址: " + req_url);
            String htmlText = reqPost(req_url);

            System.out.println(htmlText);
        }
    }


    /**
     * 生成api
     * @param api_n
     * @return
     */
    private static List getApis(int api_n){
        Map version = versions(); //版本号
        List apis = new ArrayList<>();
        List rand_devices = new ArrayList<>();
        int devices_size = DEVICEINFOS.length; //实际设备信息条数
        int real_size = devices_size < api_n ? devices_size : api_n ; //最终获取的设备信息数量
        //获取随机设备信息
        for(int i =0 ; i  versions(){
        Map map = new HashMap<>();
        map.put("680","6.8.0");
        map.put("251","2.5.1");
        map.put("140","1.4.0");
        return map;
    }


    /**
     * post请求
     * @param url
     * @return
     */
    public static String reqPost(String url) {
        //编码:UTF-8
        return send(url);
    }


    /**
     * 向指定 URL 发送POST方法的请求
     * @param url 发送请求的 URL
     * @return 所代表远程资源的响应结果
     */
    public static  String send(String url) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("User-Agent", "okhttp/3.10.0.1");
            conn.setRequestProperty("Accept-Encoding", "utf-8");
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);

            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"), Boolean.FALSE);
            out.flush();

            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * get请求
     * @param url
     * @return
     */
    public static String reqGet(String url) {
        //编码:UTF-8
        return httpGet(url,  "UTF-8",false);
    }

    /**
     * get请求
     * @param url
     * @return
     */
    public static String httpGet(String url, String code,boolean flag) {
        System.out.println("GetPage: " + url);
        String content = null;
        HttpClient httpClient = new HttpClient();
        //设置header
        if(flag){
            //httpClient.getParams().setParameter("Accept-Encoding", "utf-8" );
            //httpClient.getParams().setParameter("User-Agent", "okhttp/3.10.0.1");
            httpClient.getParams().setParameter(HttpMethodParams.STRICT_TRANSFER_ENCODING, "utf-8" );
            httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "okhttp/3.10.0.1");
        }else{
            httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/5.0 (Linux; Android 8.0.0; MI 6 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36");
        }
        GetMethod method = new GetMethod(url);
        try {
            int statusCode = httpClient.executeMethod(method);
            System.out.println("httpClientUtils::statusCode=" + statusCode);
            System.out.println(method.getStatusLine());
            content = new String(method.getResponseBody(), code);

        } catch (Exception e) {
            System.out.println("time out");
            e.printStackTrace();
        } finally {
            if (method != null) method.releaseConnection();
            method = null;
            httpClient = null;
        }
        return content;
    }



}

 

你可能感兴趣的:(java)