This commit is contained in:
2024-11-03 15:32:20 +08:00
parent 51f961b054
commit 3b190b8a44
7 changed files with 120 additions and 41 deletions

View File

@@ -74,6 +74,7 @@ public class VideoFileController {
public String getTmpImg(String url) {
try {
// 获取图片流
System.out.println("url = " + url);
InputStream inputStream = new URL(url).openStream();
// 将输入流转换为字节数组

View File

@@ -1,19 +1,27 @@
package com.yutou.bilibili.services;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.api.LiveApi;
import com.yutou.biliapi.bean.live.LiveAnchorInfo;
import com.yutou.biliapi.bean.live.LiveRoomInfo;
import com.yutou.biliapi.bean.live.LiveRoomPlayInfo;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.biliapi.net.BiliLiveNetApiManager;
import com.yutou.bilibili.datas.web.LiveData;
import com.yutou.common.okhttp.BaseBean;
import com.yutou.common.okhttp.HttpLoggingInterceptor;
import com.yutou.common.utils.Log;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@Service
public class LiveService {
@@ -32,33 +40,61 @@ public class LiveService {
return liveConfigDatabase.getAllConfig().size();
}
public List<LiveData> getLiveList(int page,int limit) {
List<LiveConfigDatabaseBean> allConfig = liveConfigDatabase.getConfigs(page,limit);
List<LiveConfigDatabaseBean> allConfig = liveConfigDatabase.getAllConfig();
List<LiveData> liveDataList = new ArrayList<>();
for (LiveConfigDatabaseBean config : allConfig) {
LiveData liveData = new LiveData();
liveData.setRoomId(config.getRoomId());
liveData.setAnchorUid(config.getAnchorUid());
liveData.setAnchorName(config.getAnchorName());
liveData.setAnchorFace(config.getAnchorFace());
liveData.setDownloadVideo(videoDownloadService.checkDownload(config.getRoomId()));
liveData.setDanmu(danmuService.check(config.getRoomId()));
try {
LiveRoomInfo body = api.getRoomInfo(config.getRoomId()).execute().body().getData();
if (body != null) {
liveData.setTitle(body.getTitle());
liveData.setLive(body.getLiveStatus() == 1);
if (body.getLiveStatus() == 1) {
liveData.setCover(body.getKeyframe());
} else {
liveData.setCover(body.getUserCover());
}
JSONArray uids=new JSONArray();
JSONObject param=new JSONObject();
for (LiveConfigDatabaseBean bean : allConfig) {
uids.add(bean.getAnchorUid());
}
param.put("uids",uids);
try {
Map<String, LiveAnchorInfo> map = api.getLiveRoomStatus(param).execute().body().getData();
List<LiveAnchorInfo> onlineList = new ArrayList<>();
List<LiveAnchorInfo> offlineList = new ArrayList<>();
for (LiveAnchorInfo info : map.values()) {
if(info.getLiveStatus()==1){
onlineList.add(info);
}else{
offlineList.add(info);
}
}
onlineList.addAll(offlineList);
int totalSize = onlineList.size();
int fromIndex = (page - 1) * limit;
int toIndex = Math.min(fromIndex + limit, totalSize);
if (fromIndex >= totalSize) {
return new ArrayList<>(); // 返回空列表
}
List<LiveAnchorInfo> list = onlineList.subList(fromIndex, toIndex);
for (LiveAnchorInfo info : list) {
LiveData liveData = new LiveData();
liveData.setRoomId(info.getRoomId());
liveData.setAnchorUid(info.getUid());
liveData.setAnchorName(info.getUname());
liveData.setAnchorFace(info.getFace());
liveData.setDownloadVideo(videoDownloadService.checkDownload(info.getRoomId()));
liveData.setDanmu(danmuService.check(info.getRoomId()));
liveData.setTitle(info.getTitle());
liveData.setLive(info.getLiveStatus() == 1);
if (info.getLiveStatus() == 1 && StringUtils.hasText(info.getKeyframe())) {
liveData.setCover(info.getKeyframe());
} else {
liveData.setCover(info.getCoverFromUser());
}
liveDataList.add(liveData);
} catch (IOException e) {
Log.e(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
liveDataList.sort(Comparator.comparing(LiveData::isLive).reversed());
return liveDataList;
}
public static void main(String[] args) {
HttpLoggingInterceptor.setLog(true);
LiveService service=new LiveService();
List<LiveData> data = service.getLiveList(1, 16);
System.out.println(data.size());
}
}

View File

@@ -175,10 +175,17 @@ public class LiveVideoDownloadService {
config.setRoomInfo(roomInfo);
config.setRootPath(bean.getRecordPath());
database = new BiliLiveDatabase(config);
HttpDownloadUtils.download(new HttpDownloadUtils.Builder().setUrl(roomInfo.getKeyframe())
.setPath(rootPath.getAbsolutePath())
.setFileName("poster.jpg"));
LiveInfoNfoTools.saveLiveInfoNfo(roomInfo, rootPath.getAbsolutePath(), new File(savePath).getName().replace(".flv", ".nfo"));
try {
var keyframe = roomInfo.getKeyframe();
if(!StringUtils.hasText(keyframe)){
keyframe= roomInfo.getUserCover();
}
HttpDownloadUtils.download(new HttpDownloadUtils.Builder().setUrl(keyframe)
.setPath(rootPath.getAbsolutePath())
.setFileName("poster.jpg"));
}catch (Exception e){
e.printStackTrace();
}
saveLiveInfo(roomInfo);
api.getLiveRoomPlayInfo(
bean.getRoomId().toString(),
@@ -292,6 +299,8 @@ public class LiveVideoDownloadService {
videoDatabaseBean.setRoomInfoJson(JSONObject.toJSONString(roomInfo));
videoDatabaseBean.setStartTime(new Date());
database.addLiveInfo(videoDatabaseBean);
LiveInfoNfoTools.saveLiveInfoNfo(roomInfo, rootPath.getAbsolutePath(), new File(savePath).getName().replace(".flv", ".nfo"));
}
}