package com.crawler.view; import com.crawler.model.CrawlerData; import java.util.List; public class CrawlerView { public void showStartMessage(String crawlerName) { System.out.println("========================================"); System.out.println("开始运行爬虫: " + crawlerName); System.out.println("========================================"); } public void showProgress(int current, int total) { System.out.printf("进度: %d/%d%n", current, total); } public void showData(List dataList) { System.out.println("========================================"); System.out.println("爬取结果:"); System.out.println("========================================"); if (dataList == null || dataList.isEmpty()) { System.out.println("未获取到数据"); return; } int index = 1; for (CrawlerData data : dataList) { System.out.println("[" + index++ + "]"); System.out.println("标题: " + data.getTitle()); System.out.println("链接: " + data.getUrl()); System.out.println("来源: " + data.getSource()); if (data.getPublishDate() != null && !data.getPublishDate().isEmpty()) { System.out.println("发布日期: " + data.getPublishDate()); } if (data.getContent() != null && !data.getContent().isEmpty()) { String preview = data.getContent().length() > 100 ? data.getContent().substring(0, 100) + "..." : data.getContent(); System.out.println("内容预览: " + preview); } System.out.println("----------------------------------------"); } } public void showSuccessMessage(int count) { System.out.println("========================================"); System.out.println("爬虫运行完成,共获取 " + count + " 条数据"); System.out.println("========================================"); } public void showErrorMessage(String error) { System.err.println("========================================"); System.err.println("错误: " + error); System.err.println("========================================"); } public void showCacheSuccess(String filePath, int recordCount, long fileSize) { System.out.println("========================================"); System.out.println("缓存操作结果:"); System.out.println("========================================"); System.out.println("状态: 成功"); System.out.println("信息: 保存成功"); System.out.println("路径: " + filePath); System.out.println("记录数: " + recordCount); System.out.println("文件大小: " + formatFileSize(fileSize)); System.out.println("========================================"); } private String formatFileSize(long size) { if (size < 1024) { return size + " B"; } else if (size < 1024 * 1024) { return String.format("%.2f KB", size / 1024.0); } else { return String.format("%.2f MB", size / (1024.0 * 1024)); } } }