package com.newvideo.util.video;
|
|
import java.util.List;
|
|
import org.hibernate.Session;
|
import org.springframework.stereotype.Component;
|
|
import com.newvideo.dao.BaseDao;
|
import com.newvideo.domain.BiliBiliUrlId;
|
import com.newvideo.util.HibernateSessionFactory;
|
|
import net.sf.json.JSONObject;
|
|
@Component
|
public class BiliBiliParser {
|
|
static HibernateSessionFactory hibernateSessionFactory;
|
|
public static String getRequestUrl(String aid) {
|
return "http://www.bilibili.com/m/html5?aid=" + aid + "&page=1";
|
}
|
|
public static JSONObject getRequestHeader(String aid) {
|
JSONObject header = new JSONObject();
|
header.put("Referer", "http://www.bilibili.com/mobile/video/av" + aid + ".html");
|
return header;
|
}
|
|
public static String getRealUrl(String result) {
|
JSONObject object = JSONObject.fromObject(result);
|
return object.optString("src");
|
}
|
|
public static BiliBiliUrlId getUrlIdsByAid(String aid) {
|
BaseDao<BiliBiliUrlId> dao = new BaseDao<BiliBiliUrlId>();
|
List<BiliBiliUrlId> list = dao.list("from BiliBiliUrlId b where b.aid=?", new String[] { aid });
|
if (list != null && list.size() > 0)
|
return list.get(0);
|
return null;
|
}
|
|
@SuppressWarnings("rawtypes")
|
public static void saveUrlIds(BiliBiliUrlId bu) {
|
Session session = hibernateSessionFactory.getSession();
|
try {
|
session.getTransaction().begin();
|
List list = session.createQuery("from BiliBiliUrlId b where b.id=?").setParameter(0, bu.getAid()).list();
|
if (list == null || list.size() == 0) {
|
session.persist(bu);
|
}
|
session.getTransaction().commit();
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.close();
|
}
|
}
|
|
public static void updateUrlIds(BiliBiliUrlId bu) {
|
BaseDao<BiliBiliUrlId> dao = new BaseDao<BiliBiliUrlId>();
|
dao.update(bu);
|
}
|
|
}
|