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.
102 lines
4.2 KiB
102 lines
4.2 KiB
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];
|
|
|
|
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 + ")" : ""));
|
|
logger.info("Crawling URL: {} (attempt {})", url, retryCount + 1);
|
|
Document doc = Jsoup.connect(url)
|
|
.timeout(10000)
|
|
.get();
|
|
var articles = strategy.parse(url, doc);
|
|
for (var article : articles) {
|
|
repository.add(article);
|
|
}
|
|
logger.info("Successfully crawled {} articles from {}", articles.size(), url);
|
|
view.printSuccess("Crawled " + articles.size() + " articles.");
|
|
return;
|
|
} catch (NetworkException e) {
|
|
retryCount++;
|
|
logger.warn("Network error on attempt {} for {}: {}", retryCount, url, e.getMessage());
|
|
if (retryCount >= MAX_RETRIES) {
|
|
view.printError("Failed to crawl after " + MAX_RETRIES + " retries: " + e.getMessage());
|
|
return;
|
|
}
|
|
try {
|
|
Thread.sleep(RETRY_DELAY_MS);
|
|
} catch (InterruptedException ie) {
|
|
Thread.currentThread().interrupt();
|
|
view.printError("Retry interrupted: " + ie.getMessage());
|
|
return;
|
|
}
|
|
} catch (ParseException e) {
|
|
logger.error("Parse error for {}: {}", url, e.getMessage());
|
|
view.printError("Parse error: " + e.getMessage());
|
|
return;
|
|
} catch (Exception e) {
|
|
if (e.getMessage() != null && e.getMessage().contains("timeout")) {
|
|
retryCount++;
|
|
logger.warn("Timeout on attempt {} for {}: {}", retryCount, url, e.getMessage());
|
|
if (retryCount >= MAX_RETRIES) {
|
|
view.printError("Failed to crawl after " + MAX_RETRIES + " retries: " + e.getMessage());
|
|
return;
|
|
}
|
|
try {
|
|
Thread.sleep(RETRY_DELAY_MS);
|
|
} catch (InterruptedException ie) {
|
|
Thread.currentThread().interrupt();
|
|
view.printError("Retry interrupted: " + ie.getMessage());
|
|
return;
|
|
}
|
|
} else {
|
|
logger.error("Unexpected error crawling {}: {}", url, e.getMessage());
|
|
view.printError("Failed to crawl: " + e.getMessage());
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|