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;
|
}
|
}
|