admin
2021-02-06 cad915058c3c53bf328a8ae9ca9bc7de099caba7
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
package com.yeshi.buwan.util.zhibo;
 
import com.yeshi.buwan.domain.Config;
import com.yeshi.buwan.service.imp.ConfigService;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.zhibo.entity.Live;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.util.*;
 
@Component
public class ZhiBoUtil {
 
    @Resource
    private ConfigService configService;
 
    private static ZhiBoUtil zhiBoUtil;
 
    @PostConstruct
    public void init() {
        zhiBoUtil = this;
        zhiBoUtil.configService = this.configService;
    }
 
    private static final int LIVE_SIZE = 20;
    private static final int HUAJIA_TOTAL = 300;
 
    private static List<Live> getHuaJiaoHotLive(int count) {
        String response = huaJiaApi(
                "http://api.open.huajiao.com/live/sjlFeeds", null);
        List<Live> parseHuaJiaoLive = parseHuaJiaoHotLive(response);
        sort(parseHuaJiaoLive);
        if (parseHuaJiaoLive.size() > count)
            return parseHuaJiaoLive.subList(0, count);
        else
            return parseHuaJiaoLive;
    }
 
    private static void sort(List<Live> list) {
        Collections.sort(list, new Comparator<Live>() {
 
            public int compare(Live o1, Live o2) {
                return o2.getLiveNum() - o1.getLiveNum();
            }
        });
 
    }
 
    private static String huaJiaApi(String url, Map<String, String> map) {
        HttpClient client = new HttpClient();
//        client.getHostConfiguration().setProxy("192.168.1.200", 8888);
        StringBuffer sb = new StringBuffer("?platform=server");
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                sb.append("&" + key + "=" + map.get(key));
            }
        }
        url += sb.toString();
        GetMethod method = new GetMethod(url);
        try {
            URL Url = new URL(url);
            String uri = Url.getFile();
            String randNum = new Random().nextInt() + "";
            String authtime = System.currentTimeMillis() + "";
            String sign = HuaJiaoSign.sign(randNum, authtime, uri);
            method.setRequestHeader("Authorization", sign);
            method.setRequestHeader("Channelid", "buwanyingshidaquan");
            method.setRequestHeader("Auth-Time", authtime);
            method.setRequestHeader("Rand-Num", randNum);
            client.executeMethod(method);
            String response = new String(method.getResponseBodyAsString()
                    .getBytes("UTF-8"));
            return response;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    private static List<Live> parseHuaJiaoHotLive(String response) {
        if (StringUtil.isNullOrEmpty(response)) {
            return null;
        }
        JSONObject data = JSONObject.fromObject(response);
        String code = String.valueOf(data.opt("errno"));
        if (!"0".equals(code)) {
            return null;
        }
        JSONArray array = data.optJSONObject("data").optJSONArray("feeds");
        if (array != null) {
            List<Live> list = new ArrayList<Live>();
            int size = array.size();
            Live live = null;
            for (int i = 0; i < size; i++) {
                live = new Live();
                String h5url = String.valueOf(array.optJSONObject(i).opt(
                        "h5_url"));
                int num = (Integer) array.optJSONObject(i).optInt("hjnum");
                String name = String.valueOf(array.optJSONObject(i).opt("src"));
                String title = String.valueOf(array.optJSONObject(i).opt(
                        "title"));
                String headPic = String.valueOf(array.optJSONObject(i).opt(
                        "hjimg"));
                String roomPic = String.valueOf(array.optJSONObject(i).opt(
                        "image_url"));
                String url = String.valueOf(array.optJSONObject(i).opt("url"));
                live.setH5Url(h5url);
                live.setHeadPic(headPic);
                live.setName(name);
                live.setRoomPic(roomPic);
                live.setState(0);
                live.setTitle(title);
                live.setUrl(url);
                live.setLiveNum(num);
                list.add(live);
            }
            return list;
        }
        return null;
    }
 
    public static List<Live> getHuaJiaLiveList(Integer page) {
 
        Map<String, String> map = new HashMap<String, String>();
        int size = LIVE_SIZE;
        map.put("num", size + "");
        int offset = (page - 1) * size;
        map.put("offset", offset + "");
        map.put("tag", "live");
        String response = huaJiaApi("http://api.open.huajiao.com/live/getFeeds", map);
        List<Live> list = parseHuaJiaoLiveList(response);
        return list;
    }
 
    private static List<Live> parseHuaJiaoLiveList(String response) {
        JSONObject data = JSONObject.fromObject(response);
        if (data.optInt("errno") != 0) {
            return new ArrayList<Live>();
        }
        String h5url = "http://h.open.huajiao.com/l/index?liveid=%s&channelid=buwanyingshidaquan";
//        ServletRequestAttributes servletContainer = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = servletContainer.getRequest();
//        String ip = request.getLocalAddr();
//        int port = request.getLocalPort();
//        String cpath = request.getContextPath();
//        String path="http://"+ip+":"+port+cpath+"v=%s";
        JSONArray array = data.optJSONObject("data").optJSONArray("feeds");
        List<Live> list = new ArrayList<Live>();
        Live live = null;
        for (int i = 0; i < array.size(); i++) {
            JSONObject hjdate = array.optJSONObject(i);
            int type = hjdate.optInt("type");
            if (type != 1 && type != 4) {
                continue;
            }
            String headPic = String.valueOf(hjdate.optJSONObject("author").opt("avatar"));
            String nickname = String.valueOf(hjdate.optJSONObject("author").opt("nickname"));//主播名字
            String curh5url = String.format(h5url, String.valueOf(hjdate.optJSONObject("feed").opt("relateid")));
//            try{
//                curh5url = String.format(h5url, String.valueOf(hjdate.optJSONObject("feed").opt("relateid")));
//            }catch(Exception e){
//                curh5url = String.format(path, String.valueOf(hjdate.optJSONObject("feed").opt("mp4")));
//            }
 
            String roomPic = String.valueOf(String.valueOf(hjdate.optJSONObject("feed").opt("image")));//
            String title = String.valueOf(String.valueOf(hjdate.optJSONObject("feed").opt("title")));//
            if (StringUtil.isNullOrEmpty(title)) {
                try {
                    title = String.valueOf(String.valueOf(hjdate.optJSONObject("feed").opt("recommend")));
                } catch (Exception e) {
                    title = "";
                }
            }
            int liveNum = hjdate.optJSONObject("feed").optInt("watches");
            live = new Live();
            live.setH5Url(curh5url);
            live.setHeadPic(headPic);
            live.setLiveNum(liveNum);
            live.setName(nickname);
            live.setRoomPic(roomPic);
            live.setTitle(title);
            live.setState(0);
            live.setUrl(curh5url);
            list.add(live);
        }
        return list;
    }
 
    public static List<Live> getHuaJiaoList(int page, String tag) {
        Map<String, String> map = new HashMap<String, String>();
        int size = LIVE_SIZE + 1;
        int offset = (page - 1) * size;
        map.put("tag", tag);
        map.put("num", size + "");
        map.put("offset", offset + "");
        String response = huaJiaApi("http://api.open.huajiao.com/live/getFeeds", map);
        List<Live> list = parseHuaJiaoLiveList(response);
        if (list.size() > LIVE_SIZE) {
            List<Live> sublist = list.subList(0, LIVE_SIZE);
            return sublist;
        }
        return list;
    }
}