package com.newvideo.funtv; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.newvideo.funtv.entity.FunTVAlbum2; import com.newvideo.funtv.entity.FunTVVideo2; import com.newvideo.util.HttpUtil; import com.newvideo.util.StringUtil; import com.newvideo.util.TimeUtil; 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 final String APP_ID = "b22whcwhkc4uczk7"; private static final String APP_SECRET = "wrLDM2QLJWPM1Oem"; 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 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 allParams = new HashMap<>(); allParams.put("cp", CP); allParams.put("access_token", accessToken); for (Iterator its = params.keySet().iterator(); its.hasNext(); ) { String key = its.next(); allParams.put(key, params.get(key)); } List paramsList = new ArrayList<>(); for (Iterator 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 status 0表示不可用媒体,1表示可用媒体,2表示全部媒体 * @return */ public static Funtv2ResultVO getAlbums(int page, int pageSize, Long startTime, Long endTime, Integer status) { String url = "http://pfmg.funshion.com/v1/cp/syncmv"; Map params = new HashMap<>(); params.put("mtype", "media"); if (startTime != null) { params.put("start", TimeUtil.getGernalTime(startTime, "yyyyMMddHHmm")); } if (endTime != null) { params.put("end", TimeUtil.getGernalTime(endTime, "yyyyMMddHHmm")); } if (status != null) { params.put("status", status + ""); } params.put("page_size", pageSize + ""); params.put("page_no", page + ""); try { String result = baseRequest(url, params); System.out.println(result); JSONObject json = JSONObject.fromObject(result); if (json.optInt("code") == 0) { JSONArray array = json.optJSONArray("data"); List album2List = new Gson().fromJson(array.toString(), new TypeToken>() { }.getType()); for (FunTVAlbum2 album2 : album2List) { if (album2.getEpisodes() != null) { for (FunTVVideo2 video2 : album2.getEpisodes()) { video2.setMediaId(album2.getId()); video2.setFunH5Url(String.format("http://m.fun.tv/focplay/?mid=%s&vid=%s&malliance=fk84vly", album2.getId(), video2.getId())); } } } int totalCount = json.optInt("total"); return new Funtv2ResultVO(totalCount, album2List); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取授权码 * * @return */ public static String getAuthCode() { String url = "http://pfmg.funshion.com/v1/config/authcode"; Map params = new HashMap<>(); params.put("ctime", System.currentTimeMillis() + ""); params.put("appid", APP_ID); params.put("sign", StringUtil.Md5(APP_ID + "_" + params.get("ctime") + "_" + APP_SECRET)); String result = HttpUtil.get(url, params); System.out.println(result); JSONObject json = JSONObject.fromObject(result); if (json.optInt("retcode") == 200) { return json.optJSONObject("data").optString("auth_code"); } return null; } }