翻译PHP抖音解析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<String> getApis(int api_n){
Map<String,String> version = versions(); //版本号
List<String> apis = new ArrayList<>();
List<String> 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 <real_size ;i++){
int rand = new Random().nextInt(devices_size) ;
String rand_device = DEVICEINFOS;
if (rand_devices.contains(rand_device)) {
continue;
} else {
rand_devices.add(rand_device) ;
}
}
for(String k : version.keySet()){
Stringversion_apis = "" ;
for(String randDevice:rand_devices){
version_apis = "https://aweme.snssdk.com/aweme/v1/aweme/detail/?origin_type=link&retry_type=no_retry&"+randDevice+"&ac=wifi&channel=update&aid=1128&app_name=aweme&version_code="+k+"&version_name="+version.get(k)+"&device_platform=android&ssmix=a&device_type=MI+8&device_brand=xiaomi&language=zh&os_api=22&os_version=5.1.1&uuid=865166029463703&openudid=ec6d541a2f7350cd&manifest_version_code="+k+"&resolution=1080*1920&dpi=480&update_version_code=2512&ts=1561136204&as=a1e500706c54fd8c8d&cp=004ad55fc8d60ac4e1&aweme_id=";
}
apis.add(version_apis);
}
return apis;
}
/**
* 获取awmeId
* @param link
* @return
*/
private static String getAwemeId(String link) {
String itemId = null;
if (link.contains("http://v.douyin.com/")) {
String html = reqGet(link);
itemId = html.substring(html.indexOf("itemId:"));
itemId = itemId.substring(0,itemId.indexOf(","));
itemId = itemId.substring(itemId.indexOf("\"")+1 ,itemId.lastIndexOf("\"") );
System.out.println();
System.out.println("itemId : " + itemId);
} else if (link.contains("https://www.iesdouyin.com")) { //长链接
}
return itemId;
}
/**
* 版本号
* @return
*/
public static Map<String,String> versions(){
Map<String,String> 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 staticString 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;
}
}
这个怎么用?
页:
[1]