admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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<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 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<String, String> 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<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());
                            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<String, String> 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;
    }
 
 
}