1 changed files with 0 additions and 50 deletions
@ -1,50 +0,0 @@ |
|||||
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()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue