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.
87 lines
3.0 KiB
87 lines
3.0 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 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];
|
|
|
|
CrawlStrategy strategy = strategyFactory.getStrategy(url);
|
|
if (strategy == null) {
|
|
view.printError("No strategy found for: " + url);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
view.printInfo("Analyzing: " + url);
|
|
Document doc = Jsoup.connect(url).get();
|
|
List<Article> parsed = strategy.parse(url, doc);
|
|
|
|
view.printInfo("=== Analysis Report ===");
|
|
view.printInfo("Total articles found: " + parsed.size());
|
|
|
|
int titlesWithAuthor = 0;
|
|
int titlesWithDate = 0;
|
|
int titlesWithContent = 0;
|
|
|
|
for (Article article : parsed) {
|
|
if (article.getAuthor() != null && !article.getAuthor().isEmpty()) {
|
|
titlesWithAuthor++;
|
|
}
|
|
if (article.getPublishDate() != null && !article.getPublishDate().isEmpty()) {
|
|
titlesWithDate++;
|
|
}
|
|
if (article.getContent() != null && !article.getContent().isEmpty()) {
|
|
titlesWithContent++;
|
|
}
|
|
}
|
|
|
|
view.printInfo("Articles with author: " + titlesWithAuthor);
|
|
view.printInfo("Articles with publish date: " + titlesWithDate);
|
|
view.printInfo("Articles with content: " + titlesWithContent);
|
|
view.printInfo("Strategy used: " + strategy.getClass().getSimpleName());
|
|
|
|
if (parsed.size() > 0) {
|
|
view.printInfo("\nSample article titles:");
|
|
int limit = Math.min(3, parsed.size());
|
|
for (int i = 0; i < limit; i++) {
|
|
view.printInfo("- " + parsed.get(i).getTitle());
|
|
}
|
|
if (parsed.size() > 3) {
|
|
view.printInfo("... and " + (parsed.size() - 3) + " more");
|
|
}
|
|
}
|
|
|
|
view.printSuccess("Analysis completed (not stored)");
|
|
|
|
} catch (Exception e) {
|
|
view.printError("Failed to analyze: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|