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.

93 lines
3.9 KiB

package com.example.datacollect.command;
import com.example.datacollect.exception.CrawlerException;
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;
import java.io.IOException;
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 == null || args.length < 2) {
logger.warn("Invalid arguments for crawl command");
view.printError("Usage: crawl <url>");
return;
}
String url = args[1];
logger.info("Starting crawl for URL: {}", url);
CrawlStrategy strategy = strategyFactory.getStrategy(url);
if (strategy == null) {
logger.warn("No strategy found for URL: {}", url);
view.printError("No strategy found for: " + url);
return;
}
logger.debug("Using strategy: {} for URL: {}", strategy.getClass().getSimpleName(), url);
int retryCount = 0;
while (retryCount < MAX_RETRIES) {
try {
view.printInfo("Crawling: " + url + (retryCount > 0 ? " (attempt " + (retryCount + 1) + ")" : ""));
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.");
logger.info("Successfully crawled {} articles from URL: {}", articles.size(), url);
return;
} catch (IOException e) {
retryCount++;
logger.warn("Network error on attempt {} for URL {}: {}", retryCount, url, e.getMessage());
if (retryCount < MAX_RETRIES) {
view.printInfo("Network error, retrying in " + RETRY_DELAY_MS + "ms...");
try {
Thread.sleep(RETRY_DELAY_MS);
} catch (InterruptedException ie) {
logger.error("Interrupted during retry delay", ie);
Thread.currentThread().interrupt();
break;
}
} else {
logger.error("Failed to crawl URL {} after {} attempts", url, MAX_RETRIES, e);
view.printError("Failed to crawl after " + MAX_RETRIES + " attempts: " + e.getMessage());
}
} catch (ParseException e) {
logger.error("Parse error for URL {}: {}", url, e.getMessage(), e);
view.printError("Parse error for " + url + ": " + e.getMessage());
return;
} catch (Exception e) {
logger.error("Unexpected error crawling URL {}: {}", url, e.getMessage(), e);
view.printError("Unexpected error: " + e.getMessage());
return;
}
}
}
}