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.
50 lines
1.5 KiB
50 lines
1.5 KiB
package com.example.datacollect.command;
|
|
|
|
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;
|
|
|
|
public class CrawlCommand implements Command {
|
|
private final ConsoleView view;
|
|
private final StrategyFactory strategyFactory;
|
|
|
|
public CrawlCommand(ConsoleView view, StrategyFactory strategyFactory) {
|
|
this.view = view;
|
|
this.strategyFactory = strategyFactory;
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "crawl";
|
|
}
|
|
|
|
@Override
|
|
public void execute(String[] args, ArticleRepository repository) {
|
|
if (args.length < 2) {
|
|
view.printError("Usage: crawl <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("Crawling: " + url);
|
|
Document doc = Jsoup.connect(url).get();
|
|
var articles = strategy.parse(url, doc);
|
|
for (var article : articles) {
|
|
repository.add(article);
|
|
}
|
|
view.printSuccess("Crawled " + articles.size() + " articles.");
|
|
} catch (Exception e) {
|
|
view.printError("Failed to crawl: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|