diff --git a/src/main/java/top/yuchat/crawler/video/models/entity/MadouVideoInfo.java b/src/main/java/top/yuchat/crawler/video/models/entity/MadouVideoInfo.java index cf70cec..bad4c23 100644 --- a/src/main/java/top/yuchat/crawler/video/models/entity/MadouVideoInfo.java +++ b/src/main/java/top/yuchat/crawler/video/models/entity/MadouVideoInfo.java @@ -20,4 +20,6 @@ public class MadouVideoInfo { private Boolean m3u8; + private Long size; + } diff --git a/src/main/java/top/yuchat/crawler/video/models/service/MadouVideoService.java b/src/main/java/top/yuchat/crawler/video/models/service/MadouVideoService.java index 23dcd58..8acd6f3 100644 --- a/src/main/java/top/yuchat/crawler/video/models/service/MadouVideoService.java +++ b/src/main/java/top/yuchat/crawler/video/models/service/MadouVideoService.java @@ -134,7 +134,6 @@ public class MadouVideoService extends ServiceImpl wrapper = new QueryWrapper<>(); wrapper.eq("m3u8", true); @@ -176,4 +175,31 @@ public class MadouVideoService extends ServiceImpl wrapper = new QueryWrapper<>(); + wrapper.isNull("size"); + List madouVideoInfos = list(wrapper); + for (MadouVideoInfo videoInfo : madouVideoInfos) { + scheduledThreadPool.submit(() -> { + try { + String result = httpRequestComponent.get(videoInfo.getM3u8Url()); + List tss = Arrays.stream(result.split("\n")).filter(t -> t.contains(".ts")).toList(); + long size = 0; + for (int i = 0; i < tss.size(); i++) { + String ts = tss.get(i); + log.info("正在处理ts文件,已下载:{},共:{},当前进度:{}%", i, tss.size(), String.format("%.2f", (i * 100.0 / tss.size()))); + size += httpRequestComponent.getFileSize(videoInfo.getM3u8Url().replace("index.m3u8", ts)); + } + videoInfo.setSize(size); + updateById(videoInfo); + } catch (IOException e) { + log.error("处理失败,标题:{}, 失败原因:{}", videoInfo.getTitle(), e.getMessage(), e); + } + }); + + } + + } } diff --git a/src/main/java/top/yuchat/crawler/video/utils/HttpRequestComponent.java b/src/main/java/top/yuchat/crawler/video/utils/HttpRequestComponent.java index cc40c2c..7e3da8b 100644 --- a/src/main/java/top/yuchat/crawler/video/utils/HttpRequestComponent.java +++ b/src/main/java/top/yuchat/crawler/video/utils/HttpRequestComponent.java @@ -110,4 +110,18 @@ public class HttpRequestComponent { Files.copy(entity.getContent(), path); } + public long getFileSize(String url) throws IOException { + HttpGet httpGet = new HttpGet(url); + httpGet.setConfig(requestConfig); + CloseableHttpResponse response = closeableHttpClient.execute(httpGet); + int statusCode = response.getStatusLine().getStatusCode(); + if (HttpStatus.SC_OK != statusCode) { + log.error("文件下载失败,状态码:{}", statusCode); + throw new RuntimeException("文件下载失败"); + } + HttpEntity entity = response.getEntity(); + long contentLength = entity.getContentLength(); + response.close(); + return contentLength; + } }