package com.yeshi.buwan.util.video;
|
|
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.hibernate.Session;
|
|
import com.yeshi.buwan.dao.base.BaseDao;
|
import com.yeshi.buwan.domain.FengXingUrlId;
|
import com.yeshi.buwan.domain.FengXingVideoUrl;
|
import com.yeshi.buwan.domain.VideoDetailInfo;
|
import com.yeshi.buwan.domain.VideoInfo;
|
import com.yeshi.buwan.domain.VideoUrl;
|
import com.yeshi.buwan.util.HibernateSessionFactory;
|
import com.yeshi.buwan.util.LogUtil;
|
import com.yeshi.buwan.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class FengXingVideoParser {
|
static HibernateSessionFactory hibernateSessionFactory;
|
// 根据url获取实体
|
public static FengXingUrlId getFengXingVideoId(String url) {
|
FengXingUrlId fx = new FengXingUrlId();
|
fx.setUrl(url);
|
HttpClient client = new HttpClient();
|
GetMethod method = new GetMethod(url);
|
method.setRequestHeader("User-Agent",
|
"Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; HUAWEI G750-T01 Build/HuaweiG750-T01) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025469 Mobile Safari/533.1 V1_AND_SQ_5.8.0_264_YYB_D QQ/5.8.0.2505 NetType/WIFI WebP/0.3.0");
|
try {
|
client.executeMethod(method);
|
String result = method.getQueryString();
|
LogUtil.i(result);
|
String sts[] = result.split("&");
|
for (String st : sts)
|
if (st.contains("vid=")) {
|
fx.setVid(st.replace("vid=", ""));
|
} else if (st.contains("mid=")) {
|
fx.setMid(st.replace("mid=", ""));
|
}
|
} catch (HttpException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return fx;
|
}
|
|
public static void addUrlIds(VideoInfo info) {
|
for (VideoDetailInfo detail : info.getVideoDetailList())
|
for (VideoUrl videoUrl : detail.getUrls()) {
|
FengXingUrlId ui = getFengXingVideoId(videoUrl.getUrl());
|
BaseDao<FengXingUrlId> dao = new BaseDao<FengXingUrlId>();
|
List<FengXingUrlId> list = dao.list("from FengXingUrlId u where u.url=?", new String[] { ui.getUrl() });
|
if (list == null || list.size() == 0)
|
dao.create(ui);
|
}
|
}
|
|
public static String getFirstRequestUrl(FengXingUrlId ids) {
|
if (!StringUtil.isNullOrEmpty(ids.getMid())) {
|
return "http://pm.funshion.com/v5/media/play/?id=" + ids.getMid() + "&cl=mweb&uc=30";
|
} else {
|
return "http://pv.funshion.com/v5/video/play?id=" + ids.getVid()
|
+ "&cl=aphone&ve=2.4.0.4&mac=9cc372a7d0e4&uc=99";
|
}
|
}
|
|
public static List<String> parseSecondResponse(String result) {
|
JSONObject obj = JSONObject.fromObject(result);
|
JSONArray array = obj.optJSONArray("playlist");
|
array = array.optJSONObject(0).optJSONArray("urls");
|
List<String> list = new ArrayList<String>();
|
for (int i = 0; i < array.size(); i++) {
|
list.add(array.optString(i));
|
}
|
return list;
|
}
|
|
public static List<String> parseFirstResponse(String data) {
|
List<String> list = new ArrayList<String>();
|
JSONObject obj =JSONObject.fromObject(data);
|
JSONArray array = obj.optJSONArray("mp4");
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject object = array.optJSONObject(i);
|
list.add(object.optString("http"));
|
}
|
return list;
|
}
|
|
public static String getNewRequestUrl(String url) {
|
return FlvCdVideoParser.getRequestUrl(url);
|
}
|
|
public static FengXingVideoUrl getVideoUrl(String url) {
|
BaseDao<FengXingVideoUrl> dao = new BaseDao<FengXingVideoUrl>();
|
List<FengXingVideoUrl> list = dao.list("from FengXingVideoUrl fx where fx.url=?", new String[] { url });
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
else
|
return null;
|
}
|
|
@SuppressWarnings("unchecked")
|
public static void saveOrUpdateVideoUrl(FengXingVideoUrl fx) {
|
Session session = hibernateSessionFactory.getSession();
|
try {
|
List<FengXingVideoUrl> list = session.createQuery("from FengXingVideoUrl fx where fx.url=?")
|
.setParameter(0, fx.getUrl()).list();
|
session.getTransaction().begin();
|
if (list != null && list.size() > 0) {
|
list.get(0).setRealUrl(fx.getRealUrl());
|
list.get(0).setUpdatetime(System.currentTimeMillis() + "");
|
session.update(list.get(0));
|
} else
|
session.persist(fx);
|
session.getTransaction().commit();
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.close();
|
}
|
}
|
|
public static String parseNewResponse(String data) {
|
|
return FlvCdVideoParser.parseResult(data);
|
}
|
}
|