1 changed files with 81 additions and 0 deletions
@ -0,0 +1,81 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.exception.NetworkException; |
|||
import com.example.datacollect.exception.ParseException; |
|||
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 org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
public class CrawlCommand implements Command { |
|||
private static final Logger logger = LoggerFactory.getLogger(CrawlCommand.class); |
|||
private static final int MAX_RETRIES = 3; |
|||
private static final long RETRY_DELAY_MS = 1000; |
|||
|
|||
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]; |
|||
logger.info("Crawl started for: {}", url); |
|||
|
|||
CrawlStrategy strategy = strategyFactory.getStrategy(url); |
|||
if (strategy == null) { |
|||
view.printError("No strategy found for: " + url); |
|||
return; |
|||
} |
|||
|
|||
int retryCount = 0; |
|||
while (retryCount <= MAX_RETRIES) { |
|||
try { |
|||
view.printInfo("Crawling: " + url + (retryCount > 0 ? " (retry " + retryCount + ")" : "")); |
|||
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."); |
|||
return; |
|||
} catch (ParseException e) { |
|||
logger.severe("Parse error for " + url + ": " + e.getMessage()); |
|||
view.printError("Parse error: " + e.getMessage()); |
|||
return; |
|||
} catch (Exception e) { |
|||
retryCount++; |
|||
if (retryCount > MAX_RETRIES) { |
|||
logger.severe("Failed to crawl " + url + " after " + MAX_RETRIES + " retries: " + e.getMessage()); |
|||
view.printError("Failed to crawl after " + MAX_RETRIES + " retries: " + e.getMessage()); |
|||
return; |
|||
} |
|||
logger.warning("Network error for " + url + " (attempt " + retryCount + "/" + MAX_RETRIES + "): " + e.getMessage()); |
|||
try { |
|||
Thread.sleep(RETRY_DELAY_MS); |
|||
} catch (InterruptedException ie) { |
|||
Thread.currentThread().interrupt(); |
|||
logger.severe("Retry interrupted: " + ie.getMessage()); |
|||
view.printError("Crawl interrupted"); |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue