package com.yeshi.buwan.iqiyi.util;
|
|
import com.yeshi.buwan.domain.AdminInfo;
|
import com.yeshi.buwan.domain.VideoDetailInfo;
|
import com.yeshi.buwan.domain.VideoType;
|
import com.yeshi.buwan.iqiyi.IqiYiNewAPI;
|
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
|
import com.yeshi.buwan.iqiyi.vo.IqiyiAlbumListResult;
|
import com.yeshi.buwan.service.inter.juhe.Iqiyi2Service;
|
import com.yeshi.buwan.util.StringUtil;
|
import com.yeshi.buwan.util.TimeUtil;
|
import com.yeshi.buwan.util.mq.CMQManager;
|
import com.yeshi.buwan.util.video.VideoConstant;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.io.IOException;
|
import java.util.*;
|
|
@Component
|
public class IqiyiUtil2 {
|
public final static int PLAY_NONE = 0;// 不能播放
|
public final static int PLAY_HTML = 1;// 跳转移动端网页播放
|
public final static int PLAY_SWF = 2;// 嵌套网页播放
|
|
public final static int RESOURCE_ID = 22;
|
public final static String RESOURCE_NAME = "爱奇艺.";
|
@Resource
|
private Iqiyi2Service iqiyi2Service;
|
|
private void saveAlbumAndVideo(List<IqiyiAlbum2> list) {
|
if (list == null)
|
return;
|
for (IqiyiAlbum2 album : list) {
|
if (album.getContentType() != 1)
|
continue;
|
if (!albumIsValid(album)) {//如果是无效的专辑就需要删除
|
iqiyi2Service.deleteByAid(album.getId());
|
continue;
|
}
|
if (album.getTvQipuIds() != null && album.getTvQipuIds().size() > 0) {
|
//查询最新的一集是否缓存
|
boolean needSaveVideos = true;
|
if (album.getLatestVideo() != null) {
|
if (iqiyi2Service.countById(album.getLatestVideo().getId()) > 0L)
|
needSaveVideos = false;
|
}
|
|
|
if (needSaveVideos) {
|
int pageSize = 10;
|
int page = album.getTvQipuIds().size() % pageSize == 0 ? album.getTvQipuIds().size() / pageSize : album.getTvQipuIds().size() / pageSize + 1;
|
for (int p = 0; p < page; p++) {
|
int startIndex = p * pageSize;
|
int toIndex = (startIndex + pageSize) > album.getTvQipuIds().size() ? album.getTvQipuIds().size() : (startIndex + pageSize);
|
List<Long> tvQipuIds = new ArrayList<>();
|
tvQipuIds.addAll(album.getTvQipuIds().subList(startIndex, toIndex));
|
|
//查询是否已经存在了
|
List<IqiyiAlbum2> existList = iqiyi2Service.listByIds(tvQipuIds);
|
Set<Long> sets = new HashSet<>();
|
if (existList != null && existList.size() > 0) {
|
for (IqiyiAlbum2 a : existList)
|
sets.add(a.getId());
|
|
//删除已经存在的
|
for (int i = 0; i < tvQipuIds.size(); i++) {
|
if (sets.contains(tvQipuIds.get(i))) {
|
tvQipuIds.remove(i--);
|
}
|
}
|
}
|
|
if (tvQipuIds.size() > 0) {
|
List<IqiyiAlbum2> detailList = IqiYiNewAPI.getAlbumOrVideoDetail(tvQipuIds);
|
if (detailList != null)
|
for (IqiyiAlbum2 video : detailList)
|
iqiyi2Service.saveIqiyiAlbum(video);
|
}
|
}
|
}
|
}
|
iqiyi2Service.saveIqiyiAlbum(album);
|
CMQManager.getInstance().addIqiyiAlbumUpdateMsg(album.getId());
|
}
|
}
|
|
/**
|
* 同步所有的专辑与视频
|
*
|
* @param categoryId
|
* @param isAlbum
|
*/
|
private void syncAlbumAndVideo(int categoryId, boolean isAlbum, Long minId) {
|
IqiyiAlbumListResult result = IqiYiNewAPI.getAllAlbumAndVideoList(categoryId + "", minId, isAlbum, 10);
|
if (result.getAlbum2List() != null)
|
saveAlbumAndVideo(result.getAlbum2List());
|
while (result.getMinId() != null) {
|
result = IqiYiNewAPI.getAllAlbumAndVideoList(categoryId + "", result.getMinId(), isAlbum, 10);
|
if (result.getAlbum2List() != null) {
|
saveAlbumAndVideo(result.getAlbum2List());
|
}
|
}
|
}
|
|
|
/**
|
* 根据专辑ID查询
|
*
|
* @param aid
|
*/
|
public void syncByAid(Long aid) {
|
List<Long> aidList = new ArrayList<>();
|
aidList.add(aid);
|
List<IqiyiAlbum2> album2List = IqiYiNewAPI.getAlbumOrVideoDetail(aidList);
|
saveAlbumAndVideo(album2List);
|
}
|
|
//拉取所有的电影
|
public void syncAllDianYing() {
|
syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANYING, false, null);
|
}
|
|
//拉取所有的电视剧
|
public void syncAllDianShiJu() {
|
syncAlbumAndVideo(IqiYiNewAPI.TYPE_DIANSHIJU, true, null);
|
}
|
|
//拉取所有的动漫
|
public void syncAllDongMan() {
|
syncAlbumAndVideo(IqiYiNewAPI.TYPE_DONGMAN, true, 217289601L);
|
}
|
|
//拉取所有的综艺
|
public void syncAllZongYi() {
|
syncAlbumAndVideo(IqiYiNewAPI.TYPE_ZONGYI, true, 246881501L);
|
}
|
|
//更新专辑
|
public void updateAlbum(int categoryId, boolean isAlbum, Date startTime, Date endTime) {
|
IqiyiAlbumListResult result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, null, isAlbum, 10);
|
if (result.getAlbum2List() != null)
|
saveAlbumAndVideo(result.getAlbum2List());
|
while (result.getMinId() != null) {
|
result = IqiYiNewAPI.getUpdateAlbumList(categoryId + "", startTime, endTime, result.getMinId(), isAlbum, 10);
|
if (result.getAlbum2List() != null)
|
saveAlbumAndVideo(result.getAlbum2List());
|
}
|
}
|
|
//更新视频
|
public void updateVideo(int categoryId, Date startTime, Date endTime) {
|
IqiyiAlbumListResult result = IqiYiNewAPI.getUpdateVideoList(categoryId + "", startTime, endTime, null, true, 10);
|
if (result.getAlbum2List() != null)
|
saveAlbumAndVideo(result.getAlbum2List());
|
while (result.getMinId() != null) {
|
result = IqiYiNewAPI.getUpdateVideoList(categoryId + "", startTime, endTime, result.getMinId(), true, 10);
|
if (result.getAlbum2List() != null)
|
saveAlbumAndVideo(result.getAlbum2List());
|
}
|
}
|
|
|
//更新最近一天的的专辑与视频
|
public void updateTodayAlbumAndVideo(int channelId) {
|
long now = System.currentTimeMillis();
|
for (int i = 0; i < 8; i++) {
|
Date endTime = new Date(now - 1000 * 60 * 60L * 3 * i);
|
Date startTime = new Date(endTime.getTime() - 1000 * 60 * 60L * 3);
|
|
switch (channelId) {
|
case IqiYiNewAPI.TYPE_DIANYING:
|
updateAlbum(channelId, true, startTime, endTime);
|
break;
|
case IqiYiNewAPI.TYPE_DIANSHIJU:
|
case IqiYiNewAPI.TYPE_DONGMAN:
|
case IqiYiNewAPI.TYPE_ZONGYI:
|
updateAlbum(channelId, true, startTime, endTime);
|
break;
|
}
|
}
|
}
|
|
|
public static VideoType getVideoType(IqiyiAlbum2 album) {
|
if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING) {
|
return new VideoType(VideoConstant.VIDEO_CATEGORY_DIANYING);
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU) {
|
return new VideoType(VideoConstant.VIDEO_CATEGORY_DIANSHIJU);
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN) {
|
return new VideoType(VideoConstant.VIDEO_CATEGORY_DONGMAN);
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI) {
|
return new VideoType(VideoConstant.VIDEO_CATEGORY_ZONGYI);
|
}
|
return null;
|
}
|
|
public static String getAlbumTag(IqiyiAlbum2 album) {
|
//电影为评分
|
//电视剧和动漫为更新到多少集
|
//综艺为更新到多少期
|
|
String tag = "";
|
if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING) {
|
if (album.getStatistics() != null)
|
tag = "评分:" + album.getStatistics().getScore();
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU || album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN) {
|
if (album.getLatestVideo() != null) {
|
if (album.isSourceAlbum()) {//专辑
|
tag = "更新至" + TimeUtil.getGernalTime(TimeUtil.convertGernalTime(album.getLatestVideo().getPeriod(), "yyyyMMdd"), "yyyy-MM-dd") + "期";
|
} else {
|
if (album.getLatestVideo().getOrder() == album.getVideoCount())
|
tag = album.getVideoCount() + "集全";
|
else
|
tag = "更新至" + album.getLatestVideo().getOrder() + "集";
|
}
|
}
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
|
if (album.getLatestVideo() != null) {
|
tag = "更新至" + TimeUtil.getGernalTime(TimeUtil.convertGernalTime(album.getLatestVideo().getPeriod(), "yyyyMMdd"), "yyyy-MM-dd") + "期";
|
}
|
return tag;
|
}
|
|
|
public static VideoDetailInfo convertAlbumToVideoDetail(IqiyiAlbum2 album) {
|
VideoDetailInfo vi = new VideoDetailInfo();
|
vi.setAdmin(new AdminInfo("1"));
|
vi.setId(album.getId());
|
vi.setIntroduction(album.getDesc());
|
vi.setExtraId(album.getId() + "");
|
vi.setName(album.getName());
|
if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANYING)
|
vi.setTag(album.getName());
|
else if (album.getChannelId() == IqiYiNewAPI.TYPE_DIANSHIJU || album.getChannelId() == IqiYiNewAPI.TYPE_DONGMAN) {
|
if (album.isSourceAlbum())
|
vi.setTag(album.getPeriod() + "\n" + album.getSubTitle());
|
else
|
vi.setTag(album.getOrder() + "");
|
} else if (album.getChannelId() == IqiYiNewAPI.TYPE_ZONGYI)
|
vi.setTag(album.getPeriod() + "\n" + album.getSubTitle());
|
vi.setType("album");
|
return vi;
|
}
|
|
|
public static int getPlayType(IqiyiAlbum2 album) {
|
int type = PLAY_NONE;
|
|
if (album.getEffect() == 0)
|
type = PLAY_NONE;
|
|
for (IqiyiAlbum2.PlayControlsBean pc : album.getPlayControls()) {
|
if (pc.getPlatformId() == 15)// 移动端
|
{
|
type = PLAY_HTML;
|
}
|
}
|
return type;
|
}
|
|
|
/**
|
* 通过列表链接获取爱奇艺中的视频
|
*
|
* @param url
|
* @return
|
*/
|
public static List<String> getAlbumUrlsFromUrl(String url) {
|
List<String> list = new ArrayList<>();
|
try {
|
Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36").timeout(60000).get();
|
Element root = doc.getElementsByAttributeValue("class", "qy-mod-ul").get(0);
|
Elements items = root.getElementsByAttributeValue("class", "qy-mod-li");
|
for (int i = 0; i < items.size(); i++) {
|
String href = items.get(i).getElementsByTag("a").get(0).attr("href");
|
if (href.startsWith("//"))
|
href = "http:" + href;
|
list.add(href);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return list;
|
}
|
|
/**
|
* 专辑是否有效
|
*
|
* @param album2
|
* @return
|
*/
|
public static boolean albumIsValid(IqiyiAlbum2 album2) {
|
if (album2.getEffect() != 1)
|
return false;
|
|
if (album2.isSupportDrm())
|
return false;
|
|
List<IqiyiAlbum2.PlayControlsBean> playControls = album2.getPlayControls();
|
if (playControls == null)
|
return false;
|
for (IqiyiAlbum2.PlayControlsBean bean : playControls) {
|
if (bean.getPlatformId() == 15 && bean.getAvailableStatus() == 1)
|
return true;
|
}
|
return false;
|
}
|
|
|
}
|