admin
2020-09-12 2778cf2a16823f9b1153a0549b47f7b503176a17
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
package com.yeshi.buwan.util.video;
 
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
 
import com.yeshi.buwan.dao.base.BaseDao;
import com.yeshi.buwan.domain.LeShiUrlIds;
import com.yeshi.buwan.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class LeShiVideoParser {
    private static String get(String url) {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);
            String result = get.getResponseBodyAsString();
            return result;
 
        } catch (HttpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
 
    public static LeShiUrlIds getInfoFromLocal(String url) {
        BaseDao<LeShiUrlIds> dao = new BaseDao<LeShiUrlIds>();
        List<LeShiUrlIds> list = dao.list("from LeShiUrlIds vp where vp.url=?", new String[] { url });
        if (list != null && list.size() > 0)
            return list.get(0);
        return null;
    }
 
    public static LeShiUrlIds getVideoInfo(String url) {
        BaseDao<LeShiUrlIds> dao = new BaseDao<LeShiUrlIds>();
        List<LeShiUrlIds> list = dao.list("from LeShiUrlIds v where v.url=?", new String[] { url });
        if (list != null && list.size() > 0)
            return list.get(0);
        LeShiUrlIds vip = new LeShiUrlIds();
        org.jsoup.nodes.Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36")
                    .timeout(20000).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Elements els = doc.getElementsByTag("script");
        for (int i = 0; i < els.size(); i++) {
            if (els.get(i).toString().contains("var __INFO__ =")) {
                String info = els.get(i).toString();
                int endIndex = info.indexOf("};");
                int startIndex = info.indexOf("var __INFO__ =");
                info = info.substring(startIndex, endIndex) + "}";
                info = info.replace("var __INFO__ =", "");
                String vid = info.split("vid:")[1];
 
                String pid = info.split("pid:")[1];
                int pidEndIndex = pid.indexOf(",");
                pid = vid.substring(0, pidEndIndex).trim();
 
                endIndex = vid.indexOf(",");
                vid = vid.substring(0, endIndex).trim();
                String midUrl = "http://api.mob.app.letv.com/play/tabs?pcode=010110041&version=5.9.8&devid=b1b54bfso6cd0b1e1e1f840ac9c54a59&zid=0&vid="
                        + vid + "&pid=" + pid + "&cid=0";
                JSONObject obj = JSONObject.fromObject(get(midUrl));
                String mmsid = obj.optJSONObject("body").optJSONObject("videoInfo").optString("mid");
 
                vip.setMid(mmsid);
                vip.setVid(vid);
                vip.setCreatetime(System.currentTimeMillis() + "");
                vip.setUrl(url);
                dao.create(vip);
                break;
            }
        }
        return vip;
    }
 
    // http://api.mob.app.letv.com/play/tabs?pcode=010110041&version=5.9.8&devid=b1b54bf99bcd0b1e1e1f840ac9c54a59&zid=0&vid=0&pid=10011284&cid=0
 
    public static void addVipInfo(LeShiUrlIds vip) {
        BaseDao<LeShiUrlIds> dao = new BaseDao<LeShiUrlIds>();
        List<LeShiUrlIds> list = dao.list("from LeShiUrlIds v where v.url=?", new String[] { vip.getUrl() });
        if (list == null || list.size() == 0) {
            dao.create(vip);
        }
    }
 
    // 获取第一次请求的url
    public static String getFirstRequestUrl(LeShiUrlIds vip) {
        String time = System.currentTimeMillis() / 1000 + "";
 
        return "http://dynamic.app.m.letv.com/android/dynamic.php?mod=minfo&ctl=videofile&act=index&mmsid="
                + vip.getMid() + "&playid=2&tss=no&tm=" + time + "&key="
                + StringUtil.Md5(vip.getMid() + "," + time + ",bh65OzqYYYmHRQ")
                + "&pcode=010110041&version=5.9.8&levelType=1&vid=" + vip.getVid();
    }
 
    @SuppressWarnings("unchecked")
    public static String parseFirstResponse(String result) {
        JSONObject root = JSONObject.fromObject(result);
        if (root != null) {
            JSONObject data = root.optJSONObject("body");
            if (data != null) {
                data = data.optJSONObject("videofile");
                if (data != null) {
                    data = data.optJSONObject("infos");
                    if (data != null) {
 
                        if (data.optJSONObject("mp4_1000") != null) {
                            return data.optJSONObject("mp4_1000").optString("mainUrl");
                        } else if (data.optJSONObject("mp4_1300") != null) {
                            return data.optJSONObject("mp4_1300").optString("mainUrl");
                        } else if (data.optJSONObject("mp4_350") != null) {
                            return data.optJSONObject("mp4_350").optString("mainUrl");
                        } else if (data.optJSONObject("mp4_180") != null) {
                            return data.optJSONObject("mp4_180").optString("mainUrl");
                        } else {
                            Iterator<String> its = data.keys();
                            if (its.hasNext()) {
                                String key = its.next();
                                return data.optJSONObject(key).optString("mainUrl");
                            }
                        }
 
                    }
 
                }
            }
        }
        return "";
    }
 
    // 解析第二次返回的结果
    public static String parseSecondResponse(String result) {
        JSONObject root = JSONObject.fromObject(result);
        if (root != null) {
            JSONArray array = root.optJSONArray("nodelist");
            if (array.size() > 0)
                return array.optJSONObject(0).optString("location");
        }
        return "";
    }
}