package com.newvideo.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import com.newvideo.domain.AdminInfo; 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; public class MangGuoUtil { public static final int TYPE_ZONGYI = 1; public static final int TYPE_DIANSHIJU = 2; public static final int TYPE_DIANYING = 3; public static final String DIANYING_DONGZUO = "http://list.hunantv.com/3/47-----0-----1---.html"; public static final String DIANYING_MAOXIAN = "http://list.hunantv.com/3/48-----0-----1---.html"; public static final String DIANYING_XIJU = "http://list.hunantv.com/3/49-----0-----1---.html"; public static final String DIANYING_AIQING = "http://list.hunantv.com/3/50-----0-----1---.html"; public static final String DIANYING_ZHANZHENG = "http://list.hunantv.com/3/51-----0-----1---.html"; public static final String DIANYING_KONGBU = "http://list.hunantv.com/3/52-----0-----1---.html"; public static final String DIANYING_FANZUI = "http://list.hunantv.com/3/53-----0-----1---.html"; public static final String DIANYING_XUANYI = "http://list.hunantv.com/3/54-----0-----1---.html"; public static final String DIANYING_JINGSONG = "http://list.hunantv.com/3/55-----0-----1---.html"; public static final String DIANYING_WUXIA = "http://list.hunantv.com/3/56-----0-----1---.html"; public static final String DIANYING_KEHUAN = "http://list.hunantv.com/3/57-----0-----1---.html"; public static final String DIANYING_GEWU = "http://list.hunantv.com/3/59-----0-----1---.html"; public static final String DIANYING_DONGHUA = "http://list.hunantv.com/3/60-----0-----1---.html"; public static final String DIANYING_QIHUAN = "http://list.hunantv.com/3/61-----0-----1---.html"; public static final String DIANYING_JUQING = "http://list.hunantv.com/3/63-----0-----1---.html"; public static final String DIANYING_LUNLI = "http://list.hunantv.com/3/64-----0-----1---.html"; public static final String DIANYING_QINCHUN = "http://list.hunantv.com/3/126-----0-----1---.html"; public static final String DIANYING_JILU = "http://list.hunantv.com/3/65-----0-----1---.html"; public static final String DIANYING_LISHI = "http://list.hunantv.com/3/66-----0-----1---.html"; public static final String DIANYING_ZHUANJI = "http://list.hunantv.com/3/67-----0-----1---.html"; public static final String DIANYING_QITA = "http://list.hunantv.com/3/127-----0-----1---.html"; public static String getUrlByPage(String type, int page) { return type.replace("1---.html", page + "---.html"); } public static VideoType getDianYingVideoType(String type) { VideoType vt = new VideoType(1); if (type.equalsIgnoreCase(DIANYING_AIQING)) { vt.setId(43); } else if (type.equalsIgnoreCase(DIANYING_DONGHUA)) { vt.setId(127); } else if (type.equalsIgnoreCase(DIANYING_DONGZUO)) { vt.setId(44); } else if (type.equalsIgnoreCase(DIANYING_FANZUI)) { vt.setId(46); } else if (type.equalsIgnoreCase(DIANYING_GEWU)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_JILU)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_JINGSONG)) { vt.setId(47); } else if (type.equalsIgnoreCase(DIANYING_JUQING)) { vt.setId(55); } else if (type.equalsIgnoreCase(DIANYING_KEHUAN)) { vt.setId(3); } else if (type.equalsIgnoreCase(DIANYING_KONGBU)) { vt.setId(48); } else if (type.equalsIgnoreCase(DIANYING_LISHI)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_MAOXIAN)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_QIHUAN)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_QITA)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_WUXIA)) { vt.setId(1); } else if (type.equalsIgnoreCase(DIANYING_XIJU)) { vt.setId(41); } else if (type.equalsIgnoreCase(DIANYING_XUANYI)) { vt.setId(49); } else if (type.equalsIgnoreCase(DIANYING_ZHUANJI)) { vt.setId(1); } return vt; } public static List getZongYiByHtml(String url, VideoInfo info) { List detailList = new ArrayList(); Document doc = Jsoup.parse(get(url)); Element els = null; try { els = doc.getElementsContainingOwnText("正片播放").get(0); } catch (Exception e) { return null; } els = Jsoup.parse(get(els.attr("href"))) .getElementsByAttributeValue("class", "clearfix ullist-ele") .get(0); if (els == null) return null; Elements deEls = els.getElementsByTag("li"); for (int i = 0; i < deEls.size(); i++) { VideoDetailInfo detailInfo = new VideoDetailInfo(); Element e = deEls.get(i); String tag = e.getElementsByAttributeValue("class", "a-pic-t1") .get(0).text().replace("第", "").replace("期", ""); detailInfo.setTag(tag); detailInfo.setName(info.getName()); String introduction = e .getElementsByAttributeValue("class", "a-pic-t2").get(0) .text(); detailInfo.setIntroduction(introduction); detailInfo.setAdmin(new AdminInfo("1")); detailInfo.setCreatetime(System.currentTimeMillis() + ""); List list = new ArrayList(); VideoUrl videoUrl = new VideoUrl(); videoUrl.setAdmin(new AdminInfo("1")); videoUrl.setBaseUrl(url); videoUrl.setCreatetime(System.currentTimeMillis() + ""); videoUrl.setInvalid("0"); videoUrl.setResource(new VideoResource("3")); String vurl = e.getElementsByTag("a").get(0).attr("href"); videoUrl.setUrl(vurl); list.add(videoUrl); detailInfo.setUrls(list); detailList.add(detailInfo); if (detailList.size() > 100) break; } return detailList; } public static List getDianShiJuByHtml(String url, VideoInfo info) { List detailList = new ArrayList(); Document doc = Jsoup.parse(get(url)); Element els = doc.getElementById("tvplay-box"); Elements deEls = els.getElementsByTag("li"); for (int i = 0; i < deEls.size(); i++) { VideoDetailInfo detailInfo = new VideoDetailInfo(); Element e = deEls.get(i); String tag = e.getElementsByAttributeValue("class", "a-pic-t1") .get(0).text(); detailInfo.setTag(tag); detailInfo.setName(info.getName()); String introduction = e .getElementsByAttributeValue("class", "a-pic-t2").get(0) .text(); detailInfo.setIntroduction(introduction); detailInfo.setAdmin(new AdminInfo("1")); detailInfo.setCreatetime(System.currentTimeMillis() + ""); List list = new ArrayList(); VideoUrl videoUrl = new VideoUrl(); videoUrl.setAdmin(new AdminInfo("1")); videoUrl.setBaseUrl(url); videoUrl.setCreatetime(System.currentTimeMillis() + ""); videoUrl.setInvalid("0"); videoUrl.setResource(new VideoResource("3")); String vurl = "http://www.hunantv.com" + e.getElementsByTag("a").get(0).attr("href"); videoUrl.setUrl(vurl); list.add(videoUrl); detailInfo.setUrls(list); detailList.add(detailInfo); } return detailList; } public static VideoType getVideoType(String url) { return null; } public static List getDianYingList(String url, int page) { VideoType videoType = getDianYingVideoType(url); Document doc = Jsoup.parse(get(url)); Element root = doc.getElementsByAttributeValue("class", "clearfix ullist-ele").get(0); Elements roots = root.getElementsByTag("li"); List list = new ArrayList(); for (int i = 0; i < roots.size(); i++) { AdminInfo admin = new AdminInfo("1"); VideoInfo info = new VideoInfo(); info.setAdmin(admin); Element e = roots.get(i); e = e.getElementsByAttributeValue("class", "img-box").get(0); String picture = e.getElementsByTag("img").attr("data-original"); String tag = ""; String[] scores = new String[] { "9.0", "8.9", "9.3", "9.6" }; tag = "评分:" + scores[(int) ((Math.random()) * (scores.length))]; String baseUrl = e.getElementsByTag("a").get(0).attr("href"); String name = e.getElementsByTag("a").get(0).attr("title"); info.setName(name); info.setBaseurl(baseUrl); info.setTag(tag); info.setPicture(picture); info.setAdmin(new AdminInfo("1")); info.setCreatetime(System.currentTimeMillis()); info.setOrderby("2"); info.setQulity("高清"); info.setCanSave(true); info.setShare("0"); info.setShow("1"); info.setThirdType("0"); info.setWatchCount("0"); info.setYear("2015"); info.setVideoType(videoType); List detailList = new ArrayList(); List urlList = new ArrayList(); VideoUrl videoUrl = new VideoUrl(); videoUrl.setAdmin(admin); videoUrl.setBaseUrl(baseUrl); videoUrl.setCreatetime(System.currentTimeMillis() + ""); videoUrl.setInvalid("0"); videoUrl.setResource(new VideoResource("3")); videoUrl.setUrl(baseUrl); urlList.add(videoUrl); VideoDetailInfo detailInfo = new VideoDetailInfo(); detailInfo.setAdmin(admin); detailInfo.setCreatetime(System.currentTimeMillis() + ""); detailInfo.setName(name); detailInfo.setUrls(urlList); detailList.add(detailInfo); info.setVideoDetailList(detailList); if (info.getVideoDetailList() != null && info.getVideoDetailList().size() > 0) list.add(info); } return list; } public static List getVideoInfo(String url, int type) { Document doc = Jsoup.parse(get(url)); Element root = doc.getElementsByAttributeValue("class", "clearfix ullist-ele").get(0); Elements roots = root.getElementsByTag("li"); List list = new ArrayList(); for (int i = 0; i < roots.size(); i++) { VideoInfo info = new VideoInfo(); Element e = roots.get(i); e = e.getElementsByAttributeValue("class", "img-box").get(0); String picture = e.getElementsByTag("img").attr("src"); String tag = e.getElementsByAttributeValue("class", "a-pic-t3") .get(0).text(); tag = tag.replace("更新到", "").replace("期", ""); String baseUrl = e.getElementsByTag("a").get(0).attr("href"); String name = e.getElementsByTag("a").get(0).attr("title"); info.setName(name); info.setBaseurl(baseUrl); info.setTag(tag); info.setPicture(picture); info.setAdmin(new AdminInfo("1")); info.setCreatetime(System.currentTimeMillis()); info.setOrderby("2"); info.setQulity("高清"); info.setShare("0"); info.setShow("1"); if (type == TYPE_DIANSHIJU) { info.setVideoType(new VideoType(2)); try { List detailList = getDianShiJuByHtml( baseUrl, info); if (detailList != null && detailList.size() > 0) { info.setVideoDetailList(detailList); } } catch (Exception ee) { ee.printStackTrace(); } } else if (type == TYPE_ZONGYI) { try { info.setVideoType(new VideoType(18)); List detailList = getZongYiByHtml(baseUrl, info); if (detailList != null && detailList.size() > 0) { info.setVideoDetailList(detailList); } } catch (Exception ee) { ee.printStackTrace(); } } info.setThirdType("0"); info.setWatchCount("0"); info.setYear("2015"); if (info.getVideoDetailList() != null && info.getVideoDetailList().size() > 0) list.add(info); } return list; } private static String get(String url) { HttpClient client = new HttpClient(); client.getParams().setContentCharset("UTF-8"); GetMethod method = new GetMethod(url); try { method.addRequestHeader("Content-Type", "text/html;charset=UTF-8"); method.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); client.executeMethod(method); String responseBodyAsString = method.getResponseBodyAsString(); // LogUtil.i(responseBodyAsString); return responseBodyAsString; } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } }