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.
71 lines
2.3 KiB
71 lines
2.3 KiB
|
|
|
|
|
|
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);
|
|
view.printInfo("Using strategy: " + strategy.getClass().getSimpleName());
|
|
|
|
Document doc = Jsoup.connect(url).get();
|
|
List<Article> articles = strategy.parse(url, doc);
|
|
|
|
view.printSuccess("=== Analysis Statistics ===");
|
|
view.printSuccess("URL: " + url);
|
|
view.printSuccess("Strategy: " + strategy.getClass().getSimpleName());
|
|
view.printSuccess("Articles found: " + articles.size());
|
|
|
|
if (!articles.isEmpty()) {
|
|
view.printSuccess("Sample titles:");
|
|
int count = Math.min(3, articles.size());
|
|
for (int i = 0; i < count; i++) {
|
|
view.printSuccess(" - " + articles.get(i).getTitle());
|
|
}
|
|
if (articles.size() > 3) {
|
|
view.printSuccess(" ... and " + (articles.size() - 3) + " more");
|
|
}
|
|
}
|
|
|
|
view.printSuccess("Note: Articles were NOT stored in repository");
|
|
|
|
} catch (Exception e) {
|
|
view.printError("Failed to analyze: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|