You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.7 KiB

package com.example.datacollect.command;
import com.example.datacollect.exception.CrawlerException;
import com.example.datacollect.exception.ErrorCode;
import com.example.datacollect.model.Article;
import com.example.datacollect.repository.ArticleRepository;
import com.example.datacollect.strategy.CrawlStrategy;
import com.example.datacollect.strategy.StrategyFactory;
import com.example.datacollect.view.ConsoleView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.List;
public class AnalyzeCommand implements Command {
private final ConsoleView view;
private final StrategyFactory strategyFactory;
public AnalyzeCommand(ConsoleView view, StrategyFactory strategyFactory) {
this.view = view;
this.strategyFactory = strategyFactory;
}
@Override
public String getName() {
return "analyze";
}
@Override
public void execute(String[] args, ArticleRepository repository) {
if (args.length < 2) {
view.printError("[" + ErrorCode.INVALID_ARGUMENT.getCode() + "] " + ErrorCode.INVALID_ARGUMENT.getMessage() + ",格式:analyze <url>");
return;
}
String url = args[1];
CrawlStrategy strategy = strategyFactory.getStrategy(url);
if (strategy == null) {
throw new CrawlerException(ErrorCode.STRATEGY_NOT_FOUND, "不支持解析该URL:" + url);
}
Document doc;
try {
view.printInfo("正在分析: " + url);
doc = Jsoup.connect(url)
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Connection", "keep-alive")
.header("Cache-Control", "max-age=0")
.get();
} catch (IOException e) {
throw new CrawlerException(ErrorCode.NETWORK_ERROR, "网页爬取失败:" + e.getMessage(), e);
}
List<Article> articles;
try {
articles = strategy.parse(url, doc);
} catch (Exception e) {
throw new CrawlerException(ErrorCode.PARSE_ERROR, "网页解析失败:" + e.getMessage(), e);
}
printAnalysisResult(articles, url);
}
private void printAnalysisResult(List<Article> articles, String url) {
view.printInfo("\n===== 网页分析结果(不存储数据) =====");
view.printInfo("分析URL:" + url);
view.printInfo("解析到文章数量:" + articles.size());
if (!articles.isEmpty()) {
int totalTitleLength = 0;
for (Article article : articles) {
totalTitleLength += article.getTitle().length();
}
double avgTitleLength = (double) totalTitleLength / articles.size();
view.printInfo(String.format("文章标题平均长度:%.2f 字符", avgTitleLength));
view.printInfo("\n前3篇文章标题预览:");
for (int i = 0; i < Math.min(3, articles.size()); i++) {
view.printInfo((i + 1) + ". " + articles.get(i).getTitle());
}
} else {
view.printInfo("未解析到任何文章内容");
}
view.printInfo("=====================================\n");
}
}