admin
2021-04-07 35a29882f356542dd9c431714c64d277eb7cad40
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.yeshi.buwan.mogotv;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.buwan.mogotv.entity.MogoTVClipInfo;
import com.yeshi.buwan.mogotv.entity.MogoTVVideo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.yeshi.utils.HttpUtil;
 
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.lang.reflect.Type;
import java.util.*;
 
public class MogoTVApiUtil {
 
//1.列表
//    https://pianku.api.mgtv.com/rider/list/pcweb/v4?platform=pcweb&channelId=2&pn=1&pc=80&hudong=0&_support=10000000&kind=a1&area=a1&sort=c2&abroad=0&src=intelmgtv&callback=jsonp_1616647932020_11155
//
//            2.详情:
//    https://pcweb.api.mgtv.com/video/info?cid=341719&_support=10000000&abroad=0&src=intelmgtv&callback=jsonp_1616648013226_52720
//
//            3.视频列表
//    https://pcweb.api.mgtv.com/episode/list?src=intelmgtv&abroad=0&_support=10000000&version=5.5.35&video_id=11026579&page=0&size=30&abroad=0&src=intelmgtv&callback=jsonp_1616648013585_27530
//
//    isIntact:1表示正片
    private static ScriptEngine jsEngine;
 
 
    /**
     * 解析结果数据
     *
     * @param callback
     * @param result
     * @return
     * @throws Exception
     */
    private static String parseResult(String callback, String result) throws Exception {
        if (jsEngine == null) {
            ScriptEngineManager manager = new ScriptEngineManager();
            jsEngine = manager.getEngineByName("javascript");
        }
        try {
            String script = "function getData(){ return " + result + ";}\n";
            script += String.format("function %s(t){return t;}", callback);
            jsEngine.eval(script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
 
        if (jsEngine instanceof Invocable) {
            Invocable in = (Invocable) jsEngine;
            Object info = in.invokeFunction("getData");
            return new Gson().toJson(info);
        }
        return null;
    }
 
 
    public static String request(String url) throws Exception {
        String callback = "jsonp_" + System.currentTimeMillis();
        url += "&callback=" + callback;
        String result = HttpUtil.get(url);
        return parseResult(callback, result);
    }
 
 
    public static ListResultDTO getVideoList(String videoId, int page, int pageSize) {
        String url = String.format("https://pcweb.api.mgtv.com/episode/list?src=intelmgtv&abroad=0&_support=10000000&version=5.5.35&video_id=%s&page=%s&size=%s&abroad=0&src=intelmgtv", videoId, page, pageSize);
        String result = null;
        try {
            result = request(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject data = JSONObject.fromObject(result);
        if (data.optInt("code") == 200) {
            data = data.optJSONObject("data");
 
            int totalPage = data.optInt("total_page");
            JSONObject list = data.optJSONObject("list");
            JSONArray array = null;
            if (list.isArray()) {
                array = data.optJSONArray("list");
            } else {
                array = new JSONArray();
                for (Iterator<String> its = list.keys(); its.hasNext(); ) {
                    String key = its.next();
                    array.add(list.optJSONObject(key));
                }
 
            }
            Type type = new TypeToken<List<MogoTVVideo>>() {
            }.getType();
            List<MogoTVVideo> videoList = new Gson().fromJson(array.toString(), type);
            for (int i = 0; i < videoList.size(); i++) {
                if (videoList.get(i).getIsIntact() != 1) {
                    videoList.remove(i--);
                }
            }
 
 
            return new ListResultDTO(videoList, totalPage);
        }
        return null;
    }
 
 
    /**
     * 获取视频列表
     *
     * @param videoId
     * @return
     */
    public static List<MogoTVVideo> getVideoList(String videoId) {
        List<MogoTVVideo> videoList = new ArrayList<>();
        ListResultDTO dto = getVideoList(videoId, 1, 30);
        videoList.addAll(dto.getList());
        for (int i = 1; i < dto.getTotal(); i++) {
            dto = getVideoList(videoId, i + 1, 30);
            videoList.addAll(dto.getList());
        }
 
        Comparator<MogoTVVideo> cm = new Comparator<MogoTVVideo>() {
            @Override
            public int compare(MogoTVVideo o1, MogoTVVideo o2) {
                return o1.getT1() - o2.getT1();
            }
        };
        Collections.sort(videoList, cm);
        return videoList;
    }
 
 
    /**
     * 详情获取
     *
     * @param clipInfo
     * @return
     */
    public static MogoTVClipInfo getClipDetail(MogoTVClipInfo clipInfo) {
        String url = String.format("https://pcweb.api.mgtv.com/video/info?cid=%s&vid=%s&_support=10000000&abroad=0&src=intelmgtv", clipInfo.getClipId(), clipInfo.getPlayPartId());
        String result = null;
        try {
            result = request(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject data = JSONObject.fromObject(result);
        if (data.optInt("code") == 200) {
            data = data.optJSONObject("data");
            JSONObject info = data.optJSONObject("info");
            clipInfo.setVpicture(info.optString("clipImage2"));
            clipInfo.setHpicture(info.optString("clipImage"));
            JSONObject detail = info.optJSONObject("detail");
            clipInfo.setArea(detail.optString("area"));
            clipInfo.setUpdateInfo(detail.optString("updateInfo"));
            clipInfo.setLeader(detail.optString("leader"));
            clipInfo.setReleaseTime(detail.optString("releaseTime"));
            clipInfo.setPresenter(detail.optString("presenter"));
            clipInfo.setKind(detail.optString("kind"));
            clipInfo.setDirector(detail.optString("director"));
            clipInfo.setTelevision(detail.optString("television"));
            clipInfo.setLanguage(detail.optString("language"));
            clipInfo.setFstlvlType(detail.optString("fstlvlType"));
            clipInfo.setStory(detail.optString("story"));
            String updateInfo = detail.optString("updateInfo");
            updateInfo = updateInfo.replace("更新到", "更新至");
            if (updateInfo.contains(","))
                updateInfo = updateInfo.split(",")[updateInfo.split(",").length - 1];
            clipInfo.setUpdateInfo(updateInfo);
            return clipInfo;
        }
        return null;
    }
 
    /**
     * 获取专辑列表
     *
     * @param channelId
     * @param page
     * @param pageSize
     * @return
     */
    public static ListResultDTO getClipList(int channelId, int page, int pageSize) {
        String url = String.format("https://pianku.api.mgtv.com/rider/list/pcweb/v4?platform=pcweb&channelId=%s&pn=%s&pc=%s&hudong=0&_support=10000000&kind=a1&area=a1&sort=c2&abroad=0&src=intelmgtv", channelId, page, pageSize);
        String result = null;
        try {
            result = request(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject data = JSONObject.fromObject(result);
        if (data.optInt("code") == 200) {
            data = data.optJSONObject("data");
            int total = data.optInt("totalHits");
            Type type = new TypeToken<List<MogoTVClipInfo>>() {
            }.getType();
            JSONObject hitDocs = data.optJSONObject("hitDocs");
            JSONArray array = new JSONArray();
            if (!hitDocs.isArray()) {
                for (Iterator<String> its = hitDocs.keys(); its.hasNext(); ) {
                    String key = its.next();
                    array.add(hitDocs.optJSONObject(key));
                }
            } else {
                array = data.optJSONArray("hitDocs");
            }
            List<MogoTVClipInfo> clipInfos = new Gson().fromJson(array.toString(), type);
            return new ListResultDTO(clipInfos, total);
        }
        return null;
    }
 
 
    public static void main(String[] args) {
 
        ListResultDTO dto = getClipList(1, 1, 80);
        System.out.println(dto);
    }
 
 
    public static class ListResultDTO {
 
        private List list;
        private int total;
 
        public ListResultDTO(List list, int total) {
            this.list = list;
            this.total = total;
        }
 
        public List getList() {
            return list;
        }
 
        public void setList(List list) {
            this.list = list;
        }
 
        public int getTotal() {
            return total;
        }
 
        public void setTotal(int total) {
            this.total = total;
        }
    }
 
 
}