package com.yeshi.buwan.util.video;
|
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.google.gson.Gson;
|
import com.yeshi.buwan.acFun.AcfunVideoNew;
|
import com.yeshi.buwan.acFun.AcfunVideoResult;
|
import com.yeshi.buwan.service.imp.juhe.AcfunVideoNewService;
|
import com.yeshi.buwan.util.BeanUtil;
|
import com.yeshi.buwan.util.HttpUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class AcfunApiUtil {
|
|
private static String sign(Map<String, String> map) {
|
List<String> list = new ArrayList<>();
|
for (Iterator<String> its = map.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
list.add(key + map.get(key));
|
}
|
Collections.sort(list);
|
Collections.reverse(list);
|
|
String str = "";
|
for (String st : list) {
|
str += st;
|
}
|
str += "BuWanAcfun++#";
|
|
return StringUtil.Md5(str).toLowerCase();
|
}
|
|
private static String baseRequest(String url, Map<String, String> params) {
|
String sign = sign(params);
|
params.put("sign", sign);
|
return HttpUtil.post(url, params);
|
}
|
|
public static AcfunVideoResult videoList(String pcursor) {
|
String url = "https://www.acfun.cn/rest/pc-direct/partner/buwan/list";
|
Map<String, String> params = new HashMap<String, String>();
|
params.put("pcursor", pcursor);
|
params.put("count", "100");
|
String result = baseRequest(url, params);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
if (resultJSON.optInt("code") == 0) {
|
JSONObject data = resultJSON.optJSONObject("data");
|
pcursor = data.optString("pcursor");
|
JSONArray array = data.optJSONArray("results");
|
List<AcfunVideoNew> list = new ArrayList<>();
|
Gson gson = new Gson();
|
for (int i = 0; i < array.size(); i++) {
|
try {
|
list.add(gson.fromJson(array.optJSONObject(i).toString(), AcfunVideoNew.class));
|
} catch (Exception e) {
|
System.out.println(array.optJSONObject(i).toString());
|
e.printStackTrace();
|
}
|
}
|
return new AcfunVideoResult(pcursor, list);
|
}
|
return null;
|
}
|
|
public static List<Boolean> listValid(List<String> ids) {
|
String url = "https://www.acfun.cn/rest/pc-direct/partner/buwan/enable";
|
Map<String, String> params = new HashMap<String, String>();
|
String st = "";
|
for (String id : ids)
|
st += id + ",";
|
if (st.endsWith(","))
|
st = st.substring(0, st.length() - 1);
|
params.put("ids", st);
|
String result = baseRequest(url, params);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
List<Boolean> validList = new ArrayList<>();
|
if (resultJson.optInt("code") == 0) {
|
JSONObject data = resultJson.optJSONObject("data");
|
for (String id : ids)
|
validList.add(data.optBoolean(id));
|
}
|
return validList;
|
}
|
|
public static void main(String[] args) {
|
AcfunVideoNewService acfunVideoNewService = BeanUtil.getBean(AcfunVideoNewService.class);
|
AcfunVideoResult result = videoList("");
|
while (result != null && result.pcursor != null && !result.pcursor.equalsIgnoreCase("no_more")) {
|
if (result.videoList != null)
|
acfunVideoNewService.save(result.videoList);
|
result = videoList(result.pcursor);
|
try {
|
Thread.sleep(2000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// String[] sts = new String[] { "5xzzqkwh73r27gq", "5xzzknzyuvvd9vc" };
|
// List<Boolean> list = listValid(Arrays.asList(sts));
|
// System.out.println(list);
|
}
|
|
|
}
|