mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-05-31 19:12:04 +08:00
v1.0.3一些bug修改和一些模块重构优化
This commit is contained in:
@@ -7,6 +7,12 @@ package org.example.constpool;
|
||||
*/
|
||||
public class ConstPool {
|
||||
|
||||
/**
|
||||
* 文件路径
|
||||
*/
|
||||
//弹幕保存文件的根目录
|
||||
public static final String BARRAGE_ROOT = (String)GlobalFileCache.ModuleSrcConfigFile.get("barrage","src");
|
||||
|
||||
/**
|
||||
* 下载的方式
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package org.example.core.pipeline;
|
||||
|
||||
import org.example.cache.FileCache;
|
||||
import org.example.cache.FileCacheManagerInstance;
|
||||
import org.example.exception.FileCacheException;
|
||||
import org.example.pojo.Barrage;
|
||||
import org.example.pojo.configfile.BarrageSaveFile;
|
||||
import org.example.pojo.download.LoadConfig;
|
||||
import us.codecraft.webmagic.ResultItems;
|
||||
import us.codecraft.webmagic.Task;
|
||||
@@ -19,45 +23,47 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
*/
|
||||
public class PipelineWriteJson implements Pipeline {
|
||||
|
||||
// Barrage缓存
|
||||
private final ConcurrentLinkedQueue<Barrage> cache;
|
||||
private FileCache filecache;
|
||||
private final ConcurrentLinkedQueue<Barrage> cache;
|
||||
LoadConfig loadConfig;
|
||||
|
||||
LoadConfig loadConfig;
|
||||
private BarrageSaveFile barrageSaveFile;
|
||||
|
||||
public PipelineWriteJson(LoadConfig loadConfig) {
|
||||
this.loadConfig = loadConfig;
|
||||
this.cache = new ConcurrentLinkedQueue<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(ResultItems resultItems, Task task) {
|
||||
List<Barrage> barrageList = resultItems.get("barrageList");
|
||||
if (barrageList != null) {
|
||||
cache.addAll(barrageList);
|
||||
public PipelineWriteJson(LoadConfig loadConfig) throws FileCacheException {
|
||||
this.loadConfig = loadConfig;
|
||||
this.cache = new ConcurrentLinkedQueue<>();
|
||||
this.barrageSaveFile = new BarrageSaveFile(loadConfig,cache);
|
||||
this.filecache = new FileCache(barrageSaveFile);
|
||||
FileCacheManagerInstance.getInstance().addFileCache(filecache);
|
||||
}
|
||||
}
|
||||
|
||||
public int getCacheSize() {
|
||||
return cache.size();
|
||||
}
|
||||
@Override
|
||||
public void process(ResultItems resultItems, Task task) {
|
||||
List<Barrage> barrageList = resultItems.get("barrageList");
|
||||
if (barrageList != null) {
|
||||
cache.addAll(barrageList);
|
||||
}
|
||||
}
|
||||
|
||||
// 将缓存写入到别处并且清空缓存
|
||||
public int writeDataToFileAndFlushCache(String key) {
|
||||
int successCount = 0;
|
||||
public int getCacheSize() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
String filePath = "F:\\" + key + ".txt";
|
||||
// 将缓存写入到别处并且清空缓存
|
||||
public int writeDataToFileAndFlushCache(String key) {
|
||||
int successCount = 0;
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
|
||||
Barrage barrage;
|
||||
while ((barrage = cache.poll()) != null) {
|
||||
writer.write(barrage.toString());
|
||||
writer.newLine();
|
||||
try {
|
||||
filecache.append(barrage,"-1");
|
||||
} catch (InterruptedException |FileCacheException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
successCount++;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
return successCount;
|
||||
}
|
||||
return successCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.example.pojo.configfile;
|
||||
|
||||
import org.example.common.ConfigFile;
|
||||
import org.example.constpool.GlobalFileCache;
|
||||
import org.example.exception.FileCacheException;
|
||||
import org.example.pojo.Barrage;
|
||||
import org.example.pojo.download.LoadConfig;
|
||||
import org.example.util.FileUtil;
|
||||
import org.example.util.JsonFileUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import static org.example.constpool.ConstPool.BARRAGE_ROOT;
|
||||
|
||||
/**
|
||||
* @author Genius
|
||||
* @date 2023/04/25 22:08
|
||||
**/
|
||||
|
||||
/**
|
||||
* 弹幕保存文件配置类,通过LoadConfig获取主播信息,平台信息,弹幕爬取时间,生成弹幕文件
|
||||
*/
|
||||
public class BarrageSaveFile extends ConfigFile<ConcurrentLinkedQueue<Barrage>> {
|
||||
private LoadConfig loadConfig;
|
||||
public BarrageSaveFile(LoadConfig loadConfig,ConcurrentLinkedQueue<Barrage> data) throws FileCacheException {
|
||||
super();
|
||||
this.loadConfig = loadConfig;
|
||||
if (!init(data)) {
|
||||
throw new FileCacheException("File init Error");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动生成主播弹幕文件夹以及当天直播弹幕数据文本
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private boolean init(ConcurrentLinkedQueue<Barrage> data) {
|
||||
String fileName = this.filaName();
|
||||
setFileName(fileName);
|
||||
String rootPath = Paths.get( BARRAGE_ROOT,loadConfig.getAnchorName()).toString(); //获取当前主播的文件夹路径
|
||||
setFilePath(rootPath);
|
||||
|
||||
//TODO 待移除 建立主播文件夹
|
||||
try {
|
||||
Files.createFile(Path.of(rootPath));
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setData(data);
|
||||
if (!FileUtil.isFileExist(Paths.get(rootPath,fileName).toString())) {
|
||||
|
||||
return !(JsonFileUtil.writeJsonFile(rootPath,fileName,this.packageConfig())==null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String filaName(){
|
||||
String format = "%s_%s_%s.json";
|
||||
return String.format(format,loadConfig.getPlatform(),loadConfig.getAnchorName(),loadConfig.getStartTime());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user