admin
2021-07-30 a66b556036c2b3936a51fd7b7e54a204eb31dc14
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.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.nodes.Document;
import org.jsoup.select.Elements;
 
import com.yeshi.buwan.dao.base.BaseDao;
import com.yeshi.buwan.domain.AcfunUrlId;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoUrl;
import com.yeshi.buwan.service.imp.VideoManager;
import com.yeshi.buwan.util.LogUtil;
import com.yeshi.buwan.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class AcfunParser {
    private static String get(String url) {
        HttpClient client = new HttpClient();
        client.getParams().setContentCharset("UTF-8");
        GetMethod method = new GetMethod(url);
        try {
            method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
            // method.setRequestHeader("User-Agent",
            // "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,
            // like Gecko) Chrome/46.0.2490.80 Safari/537.36");
            method.setRequestHeader("deviceType", "1");
            client.executeMethod(method);
            String responseBodyAsString = method.getResponseBodyAsString();
            return responseBodyAsString;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static AcfunUrlId getAcfunUrl(String url) {
 
        BaseDao<AcfunUrlId> dao = new BaseDao<AcfunUrlId>();
        List<AcfunUrlId> list = dao.list("from AcfunUrlId u where u.url=?", new String[] { url });
        if (list == null || list.size() == 0) {
            String vid = getVideoId(url);
            if (!StringUtil.isNullOrEmpty(vid)) {
                AcfunUrlId ac = new AcfunUrlId();
                ac.setCreatetime(System.currentTimeMillis() + "");
                ac.setUrl(url);
                ac.setVideoid(vid);
                dao.create(ac);
                return ac;
            } else {
                return null;
            }
        } else {
            return list.get(0);
        }
 
    }
 
    public static String getVideoId(String url) {
        try {
            Document 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(20 * 1000).get();
            Elements els = doc.getElementById("area-part-view").getElementsByTag("a");
            for (int i = 0; i < els.size(); i++) {
                if (!StringUtil.isNullOrEmpty(els.get(i).attr("data-vid"))) {
                    LogUtil.i(els.get(i).attr("data-vid"));
                    return els.get(i).attr("data-vid");
                }
            }
            if (els != null && els.size() > 0)
                els.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getRequestUrl(String id) {
        // http: //
        // api.acfun.tv/apiserver/content/info?contentId=2342631&cd=1&_=1448355562810&NAME=sanae
        return "http://api.acfun.tv/apiserver/content/info?contentId=" + id + "&cd=1&_=" + System.currentTimeMillis()
                + "&NAME=sanae";
    }
 
    public static String getRequestUrl(AcfunUrlId urlid) {
        // return "http://www.acfun.tv/video/getVideo.aspx?id="
        // + urlid.getVideoid();
 
        return "http://api.aixifan.com/plays/" + urlid.getVideoid() + "/realSource";
    }
 
    public static String parseUrl(String url, String detailId, String result) {
        JSONObject object =JSONObject.fromObject(result.replace("system.tv=", "")).optJSONObject("data")
                .optJSONObject("fullContent").optJSONArray("videos").getJSONObject(0);
        JSONArray array = object.optJSONArray("videoList");
        if (array != null && array.size() > 0)
            return array.optJSONObject(0).optString("playUrl");
        else {
            String vu = object.optString("sourceId");
            if (!StringUtil.isNullOrEmpty(vu)) {
                VideoManager videoManager = new VideoManager();
                VideoDetailInfo detail = videoManager.getVideoDetailById(Long.parseLong(detailId));
                List<VideoUrl> list = detail.getUrls();
                for (VideoUrl vurl : list)
                    if (vurl.getUrl().equalsIgnoreCase(url)) {
                        BaseDao<VideoUrl> dao = new BaseDao<VideoUrl>();
                        vurl.setUrl("http://cloud.letv.com?vid=http://www.acfun.tv/v/vu" + vu);
                        dao.update(vurl);
                        break;
                    }
            }
        }
        return null;
    }
 
    public static String parseRequestUrl(String url, String detailId, String result) {
        JSONObject object =JSONObject.fromObject(result);
        JSONArray array = object.optJSONArray("videoList");
        if (array != null && array.size() > 0)
            return array.optJSONObject(0).optString("playUrl");
        else {
            String vu = object.optString("sourceId");
            if (!StringUtil.isNullOrEmpty(vu)) {
                VideoManager videoManager = new VideoManager();
                VideoDetailInfo detail = videoManager.getVideoDetailById(Long.parseLong(detailId));
                List<VideoUrl> list = detail.getUrls();
                for (VideoUrl vurl : list)
                    if (vurl.getUrl().equalsIgnoreCase(url)) {
                        BaseDao<VideoUrl> dao = new BaseDao<VideoUrl>();
                        vurl.setUrl("http://cloud.letv.com?vid=http://www.acfun.tv/v/vu" + vu);
                        dao.update(vurl);
                        break;
                    }
            }
        }
        return null;
    }
 
    public static String parseAppRequest(String url, String detailId, String result) {
        JSONObject object =JSONObject.fromObject(result);
        if (object.optInt("code") == 200) {
            JSONArray array = object.optJSONObject("data").optJSONArray("files");
            if (array != null && array.size() > 0)
                return array.optJSONObject(0).optJSONArray("url").optString(0);
        }
        return "";
    }
 
}