admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.newvideo.util.zhibo;
 
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
 
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 com.newvideo.domain.Config;
import com.newvideo.service.imp.ConfigService;
import com.newvideo.util.Constant;
import com.newvideo.util.StringUtil;
import com.newvideo.zhibo.entity.Live;
import com.newvideo.zhibo.entity.ZhiBoContent;
import com.newvideo.zhibo.ljf.LiuJianFangApi;
import com.newvideo.zhibo.ljf.LiuJianFangUtil;
import com.newvideo.zhibo.ljf.entity.LJFLiveData;
 
@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;
    
    public static ZhiBoContent convertToCommonData(LJFLiveData da) {
        da = LiuJianFangUtil.getLiveData(da);
        ZhiBoContent content = new ZhiBoContent();
        content.setName(da.getUsername());
        content.setOnlineCount(da.getCount());
        content.setPicture(da.getPospic());
        content.setRank(da.getWealthrank());
        content.setUrl(String.format("http://%s/BuWan/html/zhibosts.html?link="
                + da.getLinkm(), Constant.WEBSITE));
        content.setWatchCount(da.getWatchCount());
        content.setRoomId(da.getRid());
        content.setType(1);
        return content;
    }
 
    public static List<Live> getHotLive(String platform) {
        List<Live> list = new ArrayList<Live>();
        int count = 3;
        String value = "";
        if("IOS".equalsIgnoreCase(platform)){
            Config config = zhiBoUtil.configService.getConfigByKey("open_huajiao_IOS");
            value = config.getValue();
        }else{
            Config config = zhiBoUtil.configService.getConfigByKey("open_huajiao_Android");
            value = config.getValue();
        }
        if("是".equals(value)){
            List<Live> huaJiaoHotLive = getHuaJiaoHotLive(count);
            list.addAll(huaJiaoHotLive);
        }
        
        List<Live> ljfHotLive = getLJFHotLive(count,value);
        list.addAll(ljfHotLive);
        return list;
    }
 
    private static List<Live> parseLJF(List<LJFLiveData> lJfList, String value){
        List<Live> list = new ArrayList<Live>();
        if (lJfList == null) {
            return list;
        }
        Live live = null;
        for (LJFLiveData ljfLiveData : lJfList) {
            live = new Live();
            String linkm = ljfLiveData.getLinkm();
            if(!"是".equals(value)){
                linkm = linkm+"?src=ummeda5152";
            }
            live.setH5Url(linkm);
            live.setHeadPic(ljfLiveData.getAvatar());
            live.setLiveNum(ljfLiveData.getCount());
            live.setName(ljfLiveData.getUsername());
            live.setState(0);
            live.setUrl(linkm);
            live.setRoomPic(ljfLiveData.getPic());
            list.add(live);
        }
        return list;
    }
    
    private static List<Live> getLJFHotLive(int count, String value) {
        List<LJFLiveData> lJfList = LiuJianFangApi.liveList(
                LiuJianFangApi.TYPE_ALL, 1);
        List<Live> list = parseLJF(lJfList,value);
        sort(list);
        if (list.size() > 0)
            return list.subList(0, count);
        else
            return list;
    }
 
    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> getLiveList(Integer page, String platform) {
        int offset =  (page-1) * LIVE_SIZE;
        List<Live> list = new ArrayList<Live>();
        String value = "";
        if("IOS".equalsIgnoreCase(platform)){
            Config config = zhiBoUtil.configService.getConfigByKey("open_huajiao_IOS");
            value = config.getValue();
        }else{
            Config config = zhiBoUtil.configService.getConfigByKey("open_huajiao_Android");
            value = config.getValue();
        }
        if("是".equals(value)){
            if(offset < HUAJIA_TOTAL){
                list = getHuaJiaLiveList(page);
            }
        }
        if(list.size() < LIVE_SIZE){
            int nextPage = 1;
            if("是".equals(value)){
                 nextPage = ((HUAJIA_TOTAL - offset)/LIVE_SIZE)+1;
            }else{
                nextPage=page;
            }
            List<LJFLiveData> liveList = LiuJianFangApi.liveList(LiuJianFangApi.TYPE_ALL, nextPage,LIVE_SIZE);
            List<Live> ljf = parseLJF(liveList,value);
            return ljf;
        }
        return list;
    }
 
    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;
    }
}