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.
76 lines
2.5 KiB
76 lines
2.5 KiB
package com.example.datacollect.command;
|
|
|
|
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 com.example.datacollect.command.Command;
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
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("Usage:analyze <url>");
|
|
return;
|
|
}
|
|
String url = args[1];
|
|
|
|
try {
|
|
CrawlStrategy strategy = strategyFactory.getStrategy(url);
|
|
if (strategy == null) {
|
|
view.printError("No strategy found for: " + url);
|
|
return;
|
|
}
|
|
Document doc = Jsoup.connect(url)
|
|
.userAgent("Mozilla/5.0")
|
|
.timeout(5000)
|
|
.get();
|
|
|
|
List<Article> articles = strategy.parse(doc);
|
|
|
|
|
|
|
|
int total = articles.size();
|
|
int totalTitleLen = 0;
|
|
int totalContentLen = 0;
|
|
|
|
for (Article a : articles) {
|
|
totalTitleLen += a.getTitle() == null ? 0 : a.getTitle().length();
|
|
totalContentLen += a.getContent() == null ? 0 : a.getContent().length();
|
|
}
|
|
|
|
|
|
view.printInfo("===== 分析统计结果 =====");
|
|
view.printInfo("文章总数:" + total + " 篇");
|
|
view.printInfo("标题总长度:" + totalTitleLen);
|
|
view.printInfo("内容总长度:" + totalContentLen);
|
|
if (total > 0) {
|
|
view.printInfo("平均标题长度:" + (totalTitleLen / total));
|
|
view.printInfo("平均内容长度:" + (totalContentLen / total));
|
|
}
|
|
view.printInfo("======================");
|
|
view.printSuccess("分析完成(数据未保存)");
|
|
|
|
} catch (Exception e) {
|
|
view.printError("分析失败:" + e.getMessage());
|
|
}
|
|
}
|
|
}
|