package com.yeshi.buwan.pptv;
|
|
import com.google.gson.Gson;
|
import com.yeshi.buwan.pptv.entity.PPTVSeries;
|
import com.yeshi.buwan.util.StringUtil;
|
import com.yeshi.buwan.util.TimeUtil;
|
import com.yeshi.buwan.util.log.LoggerUtil;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.HttpUtil;
|
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
public class PPTVApiUtil {
|
static Logger logger = LoggerFactory.getLogger(PPTVApiUtil.class);
|
|
//正式
|
public final static String APP_KEY = "9227024a6c540a7f8571d75c469da9ba";
|
private final static String APP_SECRET = "f3e922ac18f9da6c816d3012f8b7e575";
|
|
//测试
|
// private final static String APP_KEY = "eb324ec4439e193c38fd8d7fdbdae9af";
|
// private final static String APP_SECRET = "163cf4fa3780091e61a48c6abb6246d3";
|
|
|
public final static String CHANNEL_ID = "253350";
|
private final static String CANAL = "buwan";
|
|
private static String getBase64(String str) {
|
String st = StringUtil.getBase64(str).replace("\r\n", "");
|
logger.error(st);
|
return st;
|
}
|
|
private static String post(String url, Map<String, String> params, Map<String, String> headers) {
|
HttpClient client = new HttpClient();
|
PostMethod pm = new PostMethod(url);
|
|
|
if (headers != null) {
|
Iterator<String> its = headers.keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
pm.setRequestHeader(key, (String) headers.get(key));
|
}
|
}
|
|
try {
|
pm.setRequestBody(JSONObject.fromObject(params).toString());
|
client.executeMethod(pm);
|
return pm.getResponseBodyAsString();
|
} catch (Exception var9) {
|
var9.printStackTrace();
|
return "";
|
}
|
}
|
|
public static String baseRequest(Map<String, String> params, String method) {
|
String url = "https://coapi.pptv.com/coapi-web/api/http/sopRequest";
|
return baseRequest(url, params, method);
|
}
|
|
|
public static String baseRequest(String url, Map<String, String> params, String method) {
|
Map<String, String> headerMap = new HashMap<>();
|
headerMap.put("appMethod", method);
|
headerMap.put("appKey", APP_KEY);
|
headerMap.put("appRequestTime", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
|
headerMap.put("signInfo", sign(headerMap, params));
|
|
if (params == null) {
|
params = new HashMap<>();
|
}
|
|
return post(url, params, headerMap);
|
}
|
|
private static String sign(Map<String, String> headerMap, Map<String, String> params) {
|
logger.info(JSONObject.fromObject(params).toString());
|
String str = APP_SECRET + headerMap.get("appMethod") + headerMap.get("appRequestTime") + APP_KEY;
|
if (params != null) {
|
str += getBase64(JSONObject.fromObject(params).toString());
|
}
|
logger.error(str);
|
return StringUtil.Md5(str);
|
}
|
|
private static List<String> parseUrls(String response) {
|
List<String> list = new ArrayList<>();
|
net.sf.json.JSONObject root = net.sf.json.JSONObject.fromObject(response);
|
JSONArray array = root.optJSONObject("response").optJSONObject("body").optJSONObject("sitemapindex").optJSONArray("sitemap");
|
for (int i = 0; i < array.size(); i++) {
|
String url = array.optJSONObject(i).optString("loc");
|
list.add(url);
|
}
|
return list;
|
}
|
|
private static List<PPTVSeries> requestProgram(String url) {
|
List<PPTVSeries> list = new ArrayList<>();
|
|
Gson gson = new Gson();
|
String data = HttpUtil.getAsString(url, "ISO-8859-1", "UTF-8");
|
JSONObject root = JSONObject.fromObject(data);
|
JSONArray array = root.optJSONArray("program");
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject item = array.optJSONObject(i);
|
JSONObject serialinfo = item.optJSONObject("data").optJSONObject("serialinfo");
|
PPTVSeries pptvProgram = gson.fromJson(serialinfo.toString(), PPTVSeries.class);
|
list.add(pptvProgram);
|
}
|
return list;
|
}
|
|
private static List<PPTVSeries> getList(String method) {
|
String result = baseRequest(new HashMap<>(), method);
|
logger.error(result);
|
List<String> list = parseUrls(result);
|
List<PPTVSeries> totalList = new ArrayList<>();
|
for (String url : list) {
|
List<PPTVSeries> resultList = requestProgram(url);
|
totalList.addAll(resultList);
|
}
|
return totalList;
|
}
|
|
/**
|
* 获取更新列表
|
*
|
* @return
|
*/
|
public static List<PPTVSeries> getUpdateList() {
|
return getList("pptv.channel.content.recent");
|
}
|
|
|
public static List<PPTVSeries> getTotalList() {
|
return getList("pptv.channel.content.all");
|
}
|
|
public static PPTVSeries getDetail(String seriesCodes) {
|
Map<String, String> params = new HashMap<>();
|
params.put("seriesCodes", seriesCodes);
|
String result = baseRequest(params, "pptv.channel.content.detail");
|
System.out.println(result);
|
return null;
|
}
|
|
|
/**
|
* 获取OpenId
|
*
|
* @param uid
|
* @return
|
*/
|
public static String getOpenId(String uid) {
|
Map<String, String> params = new HashMap<>();
|
params.put("uid", uid);
|
// String result = baseRequest("https://coapi.pptv.com/coapi-web/api/http/sopRequest", params, "pptv.channel.openid.get");
|
String result = baseRequest("https://coapi.pptv.com/coapi-web/api/http/sopRequest", params, "pptv.channel.openid.get");
|
System.out.println(result);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
JSONObject response = resultJSON.optJSONObject("response");
|
if (StringUtil.isNullOrEmpty(response.optString("error_code")))
|
return response.optJSONObject("body").optString("openId");
|
return null;
|
}
|
|
|
public static void getVIPPriceList() {
|
Map<String, String> params = new HashMap<>();
|
String result = baseRequest("https://coapi.pptv.com/coapi-web/api/http/sopRequest", params, "pptv.channel.goods.associator");
|
System.out.println(result);
|
}
|
|
|
/**
|
* 登录
|
*
|
* @param code
|
*/
|
public static boolean login(String code) {
|
String url = String.format("https://coapi.pptv.com/coapi-web/api/getUserToken/%s/%s.htm", APP_KEY,URLEncoder.encode(code));
|
String result = HttpUtil.get(url);
|
System.out.println(result);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
return resultJSON.optInt("errCode") == 1;
|
}
|
|
|
/**
|
* 买商品
|
*
|
* @param openId
|
* @param orderNo
|
* @param goodsNo
|
* @param orderTime
|
* @return
|
*/
|
public static boolean buyGoods(String openId, String orderNo, String goodsNo, Date orderTime) {
|
Map<String, String> params = new HashMap<>();
|
params.put("openId", openId);
|
params.put("canal", CHANNEL_ID);
|
params.put("channel", "yeshi");
|
params.put("goodsNo", goodsNo);
|
params.put("outOrderId", orderNo);
|
params.put("orderTime", TimeUtil.getGernalTime(orderTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
params.put("sign", StringUtil.Md5(String.format("%s&%s&%s&%s", openId, params.get("channel"), orderNo, "2MnD8nCWu7EzbiJ")));
|
String result = get("http://billing.api.pptv.com/cusp/jointMember", params);
|
LoggerUtil.getVIPLogger().info("开通会员-订单号:{},结果:{}", orderNo, result);
|
System.out.println(result);
|
JSONObject data = JSONObject.fromObject(result);
|
if (data.optInt("errorCode") == 0) {
|
return true;
|
}
|
return false;
|
}
|
|
private static String get(String url, Map<String, String> params) {
|
List<String> list = new ArrayList<>();
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
list.add(key + "=" + URLEncoder.encode(params.get(key)));
|
}
|
url += "?" + org.yeshi.utils.StringUtil.concat(list, "&");
|
System.out.println(url);
|
return HttpUtil.get(url);
|
}
|
|
public static void main(String[] args) {
|
//DA7559531560894
|
buyGoods("257dfd950c20ad25dee9f99ef926c0f8", "buwan_100", "DA7559531560894", new Date());
|
}
|
|
|
}
|