package com.yeshi.buwan.funtv;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.buwan.funtv.entity.FunTVAlbum2;
|
import com.yeshi.buwan.funtv.entity.FunTVShortVideo2;
|
import com.yeshi.buwan.funtv.entity.FunTVVideo2;
|
import com.yeshi.buwan.funtv.vo.*;
|
import com.yeshi.buwan.util.HttpUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
import com.yeshi.buwan.util.TimeUtil;
|
import com.yeshi.buwan.vo.video.funtv.Funtv2ResultVO;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
public class FunTVNewApi {
|
private static final String CP = "fk84vly";
|
private static final String SECRET_KEY = "eD*r3dZNQ%7";
|
|
private static String accessToken;
|
private static long accessTokenInvalidTime;
|
|
private static String httpGet(String url) {
|
if (url != null) {
|
try {
|
return new String(HttpUtil.defaultGet(url).getBytes("ISO-8859-1"), "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
return "";
|
}
|
|
public static String getAccessToken() {
|
long time = System.currentTimeMillis() / 1000;
|
String result = httpGet(String.format("http://papi.funshion.com/api/accesstoken?cp=%s&ctime=%s&sign=%s", CP, time, StringUtil.Md5(CP + time + SECRET_KEY)));
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 0)
|
return json.optString("access_token");
|
else
|
return null;
|
}
|
|
private static String baseRequest(String url, Map<String, String> params) throws Exception {
|
long now = System.currentTimeMillis();
|
if (accessTokenInvalidTime <= now || StringUtil.isNullOrEmpty(accessToken)) {
|
String token = getAccessToken();
|
if (StringUtil.isNullOrEmpty(token)) {
|
throw new Exception("access_token获取失败");
|
}
|
accessToken = token;
|
accessTokenInvalidTime = now + 1000 * 60 * 90L;
|
}
|
|
Map<String, String> allParams = new HashMap<>();
|
allParams.put("cp", CP);
|
allParams.put("access_token", accessToken);
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
allParams.put(key, params.get(key));
|
}
|
|
List<String> paramsList = new ArrayList<>();
|
for (Iterator<String> its = allParams.keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
String value = allParams.get(key);
|
paramsList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
|
}
|
|
String paramStr = org.yeshi.utils.StringUtil.concat(paramsList, "&");
|
url += "?" + paramStr;
|
return httpGet(url);
|
}
|
|
/**
|
* @param page
|
* @param pageSize
|
* @param startTime 开始时间
|
* @param endTime 结束时间
|
* @param channelId 频道ID 1-电影 2-电视剧 3-动漫 4-综艺 5-少儿
|
* @param status 0表示不可用媒体,1表示可用媒体,2表示全部媒体
|
* @return
|
*/
|
public static Funtv2ResultVO getAlbums(int page, int pageSize, Long startTime, Long endTime, Integer channelId, Integer status) {
|
String url = "http://papi.funshion.com/cp/syncmv";
|
Map<String, String> params = new HashMap<>();
|
params.put("mtype", "media");
|
if (startTime == null)
|
params.put("start", "0");
|
else
|
params.put("start", TimeUtil.getGernalTime(startTime, "yyyyMMddHHmm"));
|
if (endTime == null)
|
params.put("end", "0");
|
else
|
params.put("end", TimeUtil.getGernalTime(endTime, "yyyyMMddHHmm"));
|
|
if (channelId != null)
|
params.put("channel", channelId + "");
|
|
if (status != null)
|
params.put("status", status + "");
|
|
params.put("page_size", pageSize + "");
|
params.put("page_no", page + "");
|
|
try {
|
String result = baseRequest(url, params);
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 0) {
|
JSONArray array = json.optJSONArray("datas");
|
List<FunTVAlbum2> album2List = new Gson().fromJson(array.toString(), new TypeToken<List<FunTVAlbum2>>() {
|
}.getType());
|
for (FunTVAlbum2 album2 : album2List)
|
if (album2.getEpisodes() != null)
|
for (FunTVVideo2 video2 : album2.getEpisodes())
|
video2.setMediaId(album2.getId());
|
int totalCount = json.optInt("total");
|
return new Funtv2ResultVO(totalCount, album2List);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static FunTVAlbum2 getAlbumsDetail(String aid) {
|
String url = "http://papi.funshion.com/cp/syncmv";
|
Map<String, String> params = new HashMap<>();
|
params.put("mtype", "media");
|
params.put("id", aid);
|
try {
|
String result = baseRequest(url, params);
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 0) {
|
JSONArray array = json.optJSONArray("datas");
|
List<FunTVAlbum2> album2List = new Gson().fromJson(array.toString(), new TypeToken<List<FunTVAlbum2>>() {
|
}.getType());
|
for (FunTVAlbum2 album2 : album2List)
|
if (album2.getEpisodes() != null)
|
for (FunTVVideo2 video2 : album2.getEpisodes())
|
video2.setMediaId(album2.getId());
|
if (album2List != null && album2List.size() > 0)
|
return album2List.get(0);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
public static Funtv2ResultVO getVideos(int page, int pageSize, Long startTime, Long endTime, Integer channelId, Integer status) {
|
String url = "http://papi.funshion.com/cp/syncmv";
|
Map<String, String> params = new HashMap<>();
|
params.put("mtype", "video");
|
if (startTime == null)
|
params.put("start", "0");
|
else
|
params.put("start", TimeUtil.getGernalTime(startTime, "yyyyMMddHHmm"));
|
if (endTime == null)
|
params.put("end", "0");
|
else
|
params.put("end", TimeUtil.getGernalTime(endTime, "yyyyMMddHHmm"));
|
|
if (channelId != null)
|
params.put("channel", channelId + "");
|
|
if (status != null)
|
params.put("status", status + "");
|
|
params.put("page_size", pageSize + "");
|
params.put("page_no", page + "");
|
|
try {
|
String result = baseRequest(url, params);
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 0) {
|
JSONArray array = json.optJSONArray("datas");
|
List<FunTVShortVideo2> videoList = new Gson().fromJson(array.toString(), new TypeToken<List<FunTVShortVideo2>>() {
|
}.getType());
|
int totalCount = json.optInt("total");
|
return new Funtv2ResultVO(totalCount, videoList);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
public static FunTVShortVideo2 getVideoDetail(String vid) {
|
String url = "http://papi.funshion.com/cp/syncmv";
|
Map<String, String> params = new HashMap<>();
|
params.put("mtype", "video");
|
params.put("id", vid);
|
|
try {
|
String result = baseRequest(url, params);
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 0) {
|
JSONArray array = json.optJSONArray("datas");
|
List<FunTVShortVideo2> videoList = new Gson().fromJson(array.toString(), new TypeToken<List<FunTVShortVideo2>>() {
|
}.getType());
|
if (videoList != null && videoList.size() > 0)
|
return videoList.get(0);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
}
|