admin
2021-04-25 36aafca8d6c1964bb755fe2ae030b163b6d0f92b
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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.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 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;
    }
 
//    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;
//    }
 
    /**
     * 获取授权码
     *
     * @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", org.yeshi.utils.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;
    }
 
 
}