package com.newvideo.util;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import javax.persistence.Entity;
|
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpException;
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
import com.newvideo.dao.BaseDao;
|
import com.newvideo.domain.VideoDetailInfo;
|
import com.newvideo.domain.VideoInfo;
|
import com.newvideo.domain.VideoResource;
|
import com.newvideo.domain.VideoType;
|
import com.newvideo.domain.VideoUrl;
|
import com.newvideo.service.imp.OtherService;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
@Entity
|
public class BaiduVideoUtil {
|
|
private String get(String url) {
|
HttpClient client = new HttpClient();
|
client.setConnectionTimeout(5 * 1000);
|
client.setTimeout(5 * 1000);
|
GetMethod method = new GetMethod(url);
|
LogUtil.i("请求的URL地址:" + url);
|
try {
|
client.executeMethod(method);
|
String responseBodyAsString = method.getResponseBodyAsString();
|
LogUtil.i(responseBodyAsString);
|
return responseBodyAsString;
|
} catch (HttpException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
public List<VideoInfo> searchVideo(String key) throws UnsupportedEncodingException {
|
int randomcode = (int) (Math.random() * 100);
|
String result = get("http://uil.cbs.baidu.com/nvideo/search/?t=" + System.currentTimeMillis()
|
+ "&os=android&app=boxapp&word=" + URLEncoder.encode(key, "UTF-8") + "&fn=jsonp" + randomcode);
|
LogUtil.i(result);
|
result = result.replace("/**/" + "jsonp" + randomcode + "(", "").replace(")", "");
|
JSONObject object = JSONObject.fromObject(result);
|
return parseVideo(object);
|
}
|
|
private List<VideoDetailInfo> parseMovie(String title, JSONArray array) {
|
VideoDetailInfo detailInfo = new VideoDetailInfo();
|
detailInfo.setId((long) 1);
|
detailInfo.setName(title);
|
for (int i = 0; i < array.size(); i++) {
|
if (isContainOurSite(array.optJSONObject(i).optString("site_name"))) {
|
List<VideoUrl> urlList = new ArrayList<VideoUrl>();
|
VideoUrl url = new VideoUrl();
|
url.setUrl(array.optJSONObject(i).optString("site_url"));
|
url.setResource(getVideoResource(url.getUrl()));
|
urlList.add(url);
|
detailInfo.setUrls(urlList);
|
break;
|
}
|
}
|
if (detailInfo.getUrls() == null || detailInfo.getUrls().size() == 0)
|
return null;
|
else {
|
List<VideoDetailInfo> list = new ArrayList<VideoDetailInfo>();
|
list.add(detailInfo);
|
return list;
|
}
|
}
|
|
private VideoInfo parseSingleVideo(JSONObject obj) {
|
JSONArray siteArray = obj.optJSONArray("sites");
|
if (siteArray == null || siteArray.size() == 0)
|
return null;
|
String site = siteArray.optJSONObject(0).optString("site");
|
if (site != null && site.equalsIgnoreCase("iqiyi") && siteArray.size() == 1)
|
return null;
|
|
JSONArray actorArray = obj.optJSONArray("actor");
|
VideoInfo info = new VideoInfo();
|
info.setScore(StringUtil.isNullOrEmpty(obj.optString("rating")) ? "9.0" : obj.optString("rating"));
|
info.setId(obj.optString("bdv_id"));
|
info.setThirdType(StringNumberUtil.convertChar2Int(obj.optString("cate")));
|
if (obj.optString("cate").contains("tvplay")) {
|
VideoType type = new VideoType(2);
|
type.setName("电视剧");
|
info.setVideoType(type);
|
info.setNowNumber(obj.optString("cur_episode"));
|
info.setTotalNumber(obj.optString("max_episode"));
|
if (Integer.parseInt(info.getNowNumber()) == Integer.parseInt(info.getTotalNumber())) {
|
info.setTag("全" + info.getTotalNumber() + "集");
|
} else {
|
info.setTag("更新至" + info.getNowNumber() + "集");
|
}
|
} else if (obj.optString("cate").contains("movie")) {
|
VideoType type = new VideoType(1);
|
type.setName("电影");
|
info.setVideoType(type);
|
info.setTag("评分:" + info.getScore());
|
} else if (obj.optString("cate").contains("tvshow")) {
|
VideoType type = new VideoType(18);
|
type.setName("综艺");
|
info.setVideoType(type);
|
info.setTag(obj.optString("cur_episode"));
|
} else if (obj.optString("cate").contains("comic")) {
|
VideoType type = new VideoType(5);
|
type.setName("动漫");
|
info.setVideoType(type);
|
info.setTag(obj.optString("cur_episode"));
|
info.setNowNumber(obj.optString("cur_episode"));
|
info.setTotalNumber(obj.optString("max_episode"));
|
if (Integer.parseInt(info.getNowNumber()) == Integer.parseInt(info.getTotalNumber())) {
|
info.setTag("全" + info.getTotalNumber() + "集");
|
} else {
|
info.setTag("更新至" + info.getNowNumber() + "集");
|
}
|
} else {
|
VideoType type = new VideoType(0);
|
type.setName("其他");
|
info.setVideoType(type);
|
info.setTag("评分:" + info.getScore());
|
}
|
String actor = "";
|
if (actorArray != null && actorArray.size() > 0)
|
for (int j = 0; j < actorArray.size(); j++)
|
actor += (actorArray.optString(j) + " ");
|
info.setYear(obj.optString("net_show_time"));
|
info.setName(obj.optString("title"));
|
info.setPicture(obj.optString("imgv_url"));
|
info.setDuration("120");
|
info.setIntroduction(obj.optString("intro"));
|
info.setMainActor(actor);
|
info.setQulity("高清");
|
|
info.setShow("1");
|
info.setWatchCount("20");
|
return info;
|
}
|
|
private List<VideoInfo> parseVideo(JSONObject object) {
|
OtherService service = new OtherService();
|
List<VideoInfo> list = new ArrayList<VideoInfo>();
|
if (object != null) {
|
JSONObject ob = object.optJSONObject("data");
|
if (ob == null)
|
return null;
|
JSONArray array = ob.optJSONArray("detail");
|
if (array != null) {
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject obj = array.optJSONObject(i);
|
VideoInfo info = parseSingleVideo(obj);
|
if (info != null && !info.getPicture().contains("qiyi.com")) {
|
list.add(info);
|
}
|
}
|
}
|
}
|
return list;
|
}
|
|
/**
|
* 电影详情获取
|
*
|
* @return
|
*/
|
private VideoInfo getMovieDetail(long type, JSONObject object) {
|
VideoInfo info = new VideoInfo();
|
String actor = "";
|
JSONArray actorArray = object.optJSONArray("actor");
|
if (actorArray != null && actorArray.size() > 0) {
|
for (int i = 0; i < actorArray.size(); i++)
|
actor += actorArray.optString(i) + " ";
|
}
|
info.setMainActor(actor);
|
info.setDuration(object.optString("duration"));
|
info.setId(object.optString("id"));
|
info.setPicture(object.optString("img_url"));
|
info.setIntroduction(object.optString("intro"));
|
info.setScore(object.optString("rating"));
|
info.setName(object.optString("title"));
|
info.setThirdType(type + "");
|
JSONArray array = object.optJSONArray("sites");
|
if (array != null && array.size() > 0) {
|
List<VideoDetailInfo> detailList = parseMovie(info.getName(), array);
|
if (detailList != null && detailList.size() > 0) {
|
info.setVideoDetailList(detailList);
|
return info;
|
} else
|
return null;
|
} else
|
return null;
|
}
|
|
// 获取电视剧信息
|
private VideoInfo getDianShiJuDetail(long type, JSONObject object) {
|
VideoInfo info = new VideoInfo();
|
String actor = "";
|
JSONArray actorArray = object.optJSONArray("actor");
|
if (actorArray != null && actorArray.size() > 0) {
|
for (int i = 0; i < actorArray.size(); i++)
|
actor += actorArray.optString(i) + " ";
|
}
|
info.setMainActor(actor);
|
info.setDuration("0");
|
info.setId(object.optString("id"));
|
info.setPicture(object.optString("img_url"));
|
info.setIntroduction(object.optString("intro"));
|
info.setScore(object.optString("rating"));
|
info.setName(object.optString("title"));
|
info.setThirdType(type + "");
|
return info;
|
}
|
|
// 获取电视剧信息
|
private VideoInfo getZhongYiDetail(int type, JSONObject object) {
|
VideoInfo info = new VideoInfo();
|
String actor = "";
|
JSONArray actorArray = object.optJSONArray("actor");
|
if (actorArray != null && actorArray.size() > 0) {
|
for (int i = 0; i < actorArray.size(); i++)
|
actor += actorArray.optString(i) + " ";
|
}
|
info.setMainActor(actor);
|
info.setDuration("0");
|
info.setId(object.optString("id"));
|
info.setPicture(object.optString("img_url"));
|
info.setIntroduction(object.optString("intro"));
|
info.setScore(object.optString("rating"));
|
info.setName(object.optString("title"));
|
info.setThirdType(type + "");
|
parseDianShiJuDetail("");// 获取详情
|
return info;
|
}
|
|
// 获取视频详情
|
public VideoInfo getVideoInfo(long type, String id) {
|
String callback = "jsonp" + (int) Math.random() * 200;
|
String urlSrc = "";
|
String result = "";
|
String cate = StringNumberUtil.convertInt2Char(type + "");
|
urlSrc = "http://uil.cbs.baidu.com/nvideo/xqinfo?os=android&cate=" + cate + "&bdv_id=" + id + "&version=1.0&fn"
|
+ callback;
|
result = get(urlSrc);
|
result = result.replace("/**/" + callback + "(", "").replace(")", "");
|
JSONObject obj = JSONObject.fromObject(result);
|
JSONObject main = obj.optJSONObject("data");
|
VideoInfo info = parseSingleVideo(main);
|
JSONArray array = main.optJSONArray("sites");
|
|
List<VideoDetailInfo> detailList = new ArrayList<VideoDetailInfo>();
|
for (int i = 0; i < array.size(); i++) {
|
JSONArray listArray = array.optJSONObject(i).optJSONArray("list");
|
if (listArray != null) {
|
for (int j = 0; j < listArray.size(); j++) {
|
VideoDetailInfo detail = new VideoDetailInfo();
|
if (info == null || StringUtil.isNullOrEmpty(info.getId())) {
|
continue;
|
}
|
String ep = listArray.optJSONObject(j).optString("episode");
|
detail.setId(Long.parseLong(info.getId()) + j);
|
detail.setAdmin(info.getAdmin());
|
detail.setBeizhu("网络搜索");
|
detail.setCreatetime(System.currentTimeMillis() + "");
|
detail.setIntroduction("");
|
detail.setName(info.getName());
|
detail.setTag(ep);
|
detail.setVideo(info);
|
List<VideoUrl> urlList = new ArrayList<VideoUrl>();
|
VideoUrl url = new VideoUrl();
|
JSONObject o = listArray.optJSONObject(j);
|
url.setAdmin(info.getAdmin());
|
url.setBeizhu("网络添加");
|
url.setCreatetime("" + System.currentTimeMillis());
|
url.setUrl(o.optString("ori_link"));
|
/*
|
* if (url != null && url.getUrl() != null) { VideoResource
|
* vr = new VideoResource(); if
|
* (url.getUrl().contains("letv.com")) { url.setResource(new
|
* VideoResource("6")); } else if
|
* (url.getUrl().contains("iqiyi.com")) {
|
* url.setResource(new VideoResource("8")); } else if
|
* (url.getUrl().contains("pptv.com")) { url.setResource(new
|
* VideoResource("10")); } else if
|
* (url.getUrl().contains("qq.com")) { url.setResource(new
|
* VideoResource("11")); } else if
|
* (url.getUrl().contains("youku.com")) {
|
* url.setResource(new VideoResource("9")); } else if
|
* (url.getUrl().contains("sohu.com")) { url.setResource(new
|
* VideoResource("7")); } else if
|
* (url.getUrl().contains("tudou.com")) {
|
* url.setResource(new VideoResource("2")); } else if
|
* (url.getUrl().contains("wasu.com")) { url.setResource(new
|
* VideoResource("4")); } url.setResource(vr); }
|
*/
|
url.setResource(getVideoResource(url.getUrl()));
|
url.setInvalid("0");
|
urlList.add(url);
|
detail.setUrls(urlList);
|
if (listArray.size() <= 1) {
|
detail.setTag("");
|
}
|
detailList.add(detail);
|
}
|
break;
|
}
|
}
|
if (info != null && detailList != null)
|
info.setVideoDetailList(detailList);
|
return info;
|
}
|
|
private List<VideoDetailInfo> parseDianShiJu(JSONArray array) {
|
for (int sa = 0; sa < array.size(); sa++) {
|
JSONObject saobject = array.optJSONObject(sa);
|
if (isContainOurSite(saobject.optString("site_name"))) {
|
if (!StringUtil.isNullOrEmpty(saobject.optString("single_url"))) {
|
return parseDianShiJuDetail(get(saobject.optString("single_url")));
|
}
|
}
|
}
|
return null;
|
}
|
|
private List<VideoDetailInfo> parseDianShiJuDetail(String st) {
|
List<VideoDetailInfo> list = new ArrayList<VideoDetailInfo>();
|
JSONObject object = JSONObject.fromObject(st);
|
JSONArray array = object.optJSONArray("videos");
|
for (int i = 0; i < array.size(); i++) {
|
VideoDetailInfo info = new VideoDetailInfo();
|
info.setId((long) (i + 1));
|
info.setName(array.optJSONObject(i).optString("title"));
|
info.setTag("第" + (i + 1) + "集");
|
List<VideoUrl> urlList = new ArrayList<VideoUrl>();
|
VideoUrl url = new VideoUrl();
|
url.setUrl(array.optJSONObject(i).optString("url"));
|
url.setResource(getVideoResource(url.getUrl()));
|
urlList.add(url);
|
info.setUrls(urlList);
|
list.add(info);
|
}
|
if (array == null || array.size() == 0)
|
return null;
|
return list;
|
}
|
|
public List<String> getSuggest(String key) throws UnsupportedEncodingException {
|
String num = "json" + (int) (Math.random() * 100);
|
// http://video.api.baidu.com/platapi/video/sugword?wd=%E7%8B%97%E7%8B%97&os=android&fn=jsonp8
|
|
String result = get("http://video.api.baidu.com/platapi/video/sugword?os=android&fn=" + num + "&wd="
|
+ URLEncoder.encode(key, "UTF-8"));
|
result = result.replace("/**/" + num + "(", "");
|
result = result.replace(")", "");
|
return parseSuggest(result);
|
}
|
|
private List<String> parseSuggest(String st) {
|
List<String> list = new ArrayList<String>();
|
if (!StringUtil.isNullOrEmpty(st)) {
|
JSONObject object = JSONObject.fromObject(st);
|
JSONArray array = object.optJSONArray("data");
|
if (array != null) {
|
for (int i = 0; i < array.size(); i++) {
|
String sts = array.optString(i);
|
list.add(sts);
|
}
|
}
|
}
|
|
return list;
|
}
|
|
public static VideoResource getVideoResource(String url) {
|
BaseDao<VideoResource> dao = new BaseDao<VideoResource>();
|
String key = "";
|
if (url.contains("le.com") || url.contains("letv.com"))
|
key = "乐视";
|
else if (url.contains("hunantv.com") || url.contains("mgtv.com"))
|
key = "芒果";
|
else if (url.contains("sohu.com"))
|
key = "搜狐";
|
else if (url.contains("pptv.com") || url.contains("pptv.com"))
|
key = "PPTV";
|
else if (url.contains("fun.tv"))
|
key = "风行";
|
else if (url.contains("qq.com"))
|
key = "腾讯";
|
else if (url.contains("youku.com") || url.contains("tudou.com"))
|
key = "优酷";
|
else
|
key = "其他";
|
|
List<VideoResource> list = dao.list("from VideoResource vr where vr.name like ?",
|
new String[] { "%" + key + "%" });
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
else
|
return null;
|
}
|
|
private boolean isContainOurSite(String site) {// 是否包含了支持的网站
|
if (site.contains("乐视") || site.contains("芒果") || site.contains("爱奇艺") || site.contains("搜狐")
|
|| site.contains("华数") || site.contains("风行") || site.contains("土豆")) {
|
return true;
|
} else
|
return false;
|
}
|
|
}
|