admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
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
package com.yeshi.buwan.util.video;
 
import java.io.IOException;
import java.util.List;
 
import org.apache.log4j.Logger;
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.SoHuVideoUrlId;
import com.yeshi.buwan.domain.VideoDetailInfo;
import com.yeshi.buwan.domain.VideoInfo;
import com.yeshi.buwan.domain.VideoUrl;
import com.yeshi.buwan.util.LogUtil;
import com.yeshi.buwan.util.StringUtil;
 
import net.sf.json.JSONObject;
 
public class SoHuVideoParser {
    static Logger logger = Logger.getLogger(SoHuVideoParser.class);
 
    public static String getUrl(SoHuVideoUrlId urlid) {
        if (!StringUtil.isNullOrEmpty(urlid.getAid()))
            return "http://api.tv.sohu.com/v4/video/info/" + urlid.getVid()
                    + ".json?site=1&plat=6&poid=1&api_key=9854b2afa779e1a6bff1962447a09dbd&sver=5.0.1&sysver=4.4.2&partner=282&aid="
                    + urlid.getAid();
        else
            return "http://api.tv.sohu.com/v4/video/info/" + urlid.getVid()
                    + ".json?site=1&plat=6&poid=1&api_key=9854b2afa779e1a6bff1962447a09dbd&sver=5.0.1&sysver=4.4.2&partner=282";
    }
 
    public static void addVideoUrlId(SoHuVideoUrlId urlid) {
        BaseDao<SoHuVideoUrlId> dao = new BaseDao<SoHuVideoUrlId>();
        List<SoHuVideoUrlId> list = dao.list("from SoHuVideoUrlId ids where ids.url=?",
                new String[] { urlid.getUrl() });
        if (list == null || list.size() == 0) {
            dao.create(urlid);
        }
    }
 
    public static void addVideoUrlIds(VideoInfo info) {
        for (VideoDetailInfo detail : info.getVideoDetailList()) {
            for (VideoUrl videoUrl : detail.getUrls()) {
                addVideoUrlId(getVideoUrlIdFromUrl(videoUrl.getUrl()));
            }
        }
    }
 
    public static SoHuVideoUrlId getVideoUrlIdFromUrl(String url) {
        SoHuVideoUrlId urlid = new SoHuVideoUrlId();
        urlid.setUrl(url);
        urlid.setCreatetime(System.currentTimeMillis() + "");
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(10000).get();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Elements els = doc.getElementsByTag("script");
        for (int i = 0; i < els.size(); i++) {
            org.jsoup.nodes.Element el = els.get(i);
            if (!url.contains("m.tv.sohu.com")) {
                if (el.toString().contains("var vid=")) {
                    String text = el.childNode(0).toString();
                    String vid = text.split("var vid=")[1].split(";")[0].replace("\"", "");
                    String aid = text.split("var playlistId=")[1].split(";")[0].replace("\"", "");
                    LogUtil.i("VID:" + vid + "--AID:" + aid);
                    urlid.setVid(vid);
                    urlid.setAid(aid);
                    if (StringUtil.isNullOrEmpty(vid)) {
                        logger.error("VID���ܽ���:" + url);
                    }
                    break;
                }
            } else {
                if (el.toString().contains("var videoData")) {
                    String text = el.childNode(0).toString();
                    String vid = text.split("vid:")[1].split(",")[0].replace("\"", "");
                    String aid = null;
                    try {
                        aid = text.split("\"aid\":")[1].split(",")[0].replace("\"", "");
                    } catch (Exception e) {
 
                    }
                    LogUtil.i("VID:" + vid + "--AID:" + aid);
                    urlid.setVid(vid);
                    urlid.setAid(aid);
                    break;
                }
            }
 
        }
        return urlid;
    }
 
    public static String parseRequestResult(String result) {
        JSONObject obj =JSONObject.fromObject(result);
        try {
            return obj.optJSONObject("data").optString("download_url");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
}