package com.newvideo.util.video; import java.io.IOException; 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.select.Elements; import com.newvideo.dao.BaseDao; import com.newvideo.domain.AcfunUrlId; import com.newvideo.domain.VideoDetailInfo; import com.newvideo.domain.VideoUrl; import com.newvideo.service.imp.VideoManager; import com.newvideo.util.LogUtil; import com.newvideo.util.StringUtil; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class AcfunParser { private static String get(String url) { HttpClient client = new HttpClient(); client.getParams().setContentCharset("UTF-8"); GetMethod method = new GetMethod(url); try { method.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); // method.setRequestHeader("User-Agent", // "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, // like Gecko) Chrome/46.0.2490.80 Safari/537.36"); method.setRequestHeader("deviceType", "1"); client.executeMethod(method); String responseBodyAsString = method.getResponseBodyAsString(); return responseBodyAsString; } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static AcfunUrlId getAcfunUrl(String url) { BaseDao dao = new BaseDao(); List list = dao.list("from AcfunUrlId u where u.url=?", new String[] { url }); if (list == null || list.size() == 0) { String vid = getVideoId(url); if (!StringUtil.isNullOrEmpty(vid)) { AcfunUrlId ac = new AcfunUrlId(); ac.setCreatetime(System.currentTimeMillis() + ""); ac.setUrl(url); ac.setVideoid(vid); dao.create(ac); return ac; } else { return null; } } else { return list.get(0); } } public static String getVideoId(String url) { try { Document doc = Jsoup.connect(url) .userAgent( "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36") .timeout(20 * 1000).get(); Elements els = doc.getElementById("area-part-view").getElementsByTag("a"); for (int i = 0; i < els.size(); i++) { if (!StringUtil.isNullOrEmpty(els.get(i).attr("data-vid"))) { LogUtil.i(els.get(i).attr("data-vid")); return els.get(i).attr("data-vid"); } } if (els != null && els.size() > 0) els.get(0); } catch (Exception e) { e.printStackTrace(); } return null; } public static String getRequestUrl(String id) { // http: // // api.acfun.tv/apiserver/content/info?contentId=2342631&cd=1&_=1448355562810&NAME=sanae return "http://api.acfun.tv/apiserver/content/info?contentId=" + id + "&cd=1&_=" + System.currentTimeMillis() + "&NAME=sanae"; } public static String getRequestUrl(AcfunUrlId urlid) { // return "http://www.acfun.tv/video/getVideo.aspx?id=" // + urlid.getVideoid(); return "http://api.aixifan.com/plays/" + urlid.getVideoid() + "/realSource"; } public static String parseUrl(String url, String detailId, String result) { JSONObject object =JSONObject.fromObject(result.replace("system.tv=", "")).optJSONObject("data") .optJSONObject("fullContent").optJSONArray("videos").getJSONObject(0); JSONArray array = object.optJSONArray("videoList"); if (array != null && array.size() > 0) return array.optJSONObject(0).optString("playUrl"); else { String vu = object.optString("sourceId"); if (!StringUtil.isNullOrEmpty(vu)) { VideoManager videoManager = new VideoManager(); VideoDetailInfo detail = videoManager.getVideoDetailById(Long.parseLong(detailId)); List list = detail.getUrls(); for (VideoUrl vurl : list) if (vurl.getUrl().equalsIgnoreCase(url)) { BaseDao dao = new BaseDao(); vurl.setUrl("http://cloud.letv.com?vid=http://www.acfun.tv/v/vu" + vu); dao.update(vurl); break; } } } return null; } public static String parseRequestUrl(String url, String detailId, String result) { JSONObject object =JSONObject.fromObject(result); JSONArray array = object.optJSONArray("videoList"); if (array != null && array.size() > 0) return array.optJSONObject(0).optString("playUrl"); else { String vu = object.optString("sourceId"); if (!StringUtil.isNullOrEmpty(vu)) { VideoManager videoManager = new VideoManager(); VideoDetailInfo detail = videoManager.getVideoDetailById(Long.parseLong(detailId)); List list = detail.getUrls(); for (VideoUrl vurl : list) if (vurl.getUrl().equalsIgnoreCase(url)) { BaseDao dao = new BaseDao(); vurl.setUrl("http://cloud.letv.com?vid=http://www.acfun.tv/v/vu" + vu); dao.update(vurl); break; } } } return null; } public static String parseAppRequest(String url, String detailId, String result) { JSONObject object =JSONObject.fromObject(result); if (object.optInt("code") == 200) { JSONArray array = object.optJSONObject("data").optJSONArray("files"); if (array != null && array.size() > 0) return array.optJSONObject(0).optJSONArray("url").optString(0); } return ""; } }