diff --git a/w11/CrawlCommand.java b/w11/CrawlCommand.java new file mode 100644 index 0000000..d638a38 --- /dev/null +++ b/w11/CrawlCommand.java @@ -0,0 +1,93 @@ +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 "); + 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; + } + } + } +} diff --git a/w11/CrawlerException.java b/w11/CrawlerException.java new file mode 100644 index 0000000..e81c3c9 --- /dev/null +++ b/w11/CrawlerException.java @@ -0,0 +1,11 @@ +package com.example.datacollect.exception; + +public class CrawlerException extends Exception { + public CrawlerException(String message) { + super(message); + } + + public CrawlerException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/w11/Main.java b/w11/Main.java new file mode 100644 index 0000000..6bf358a --- /dev/null +++ b/w11/Main.java @@ -0,0 +1,33 @@ +package com.example.datacollect; + +import com.example.datacollect.controller.CrawlerController; +import com.example.datacollect.repository.ArticleRepository; +import com.example.datacollect.strategy.StrategyFactory; +import com.example.datacollect.view.ConsoleView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Main { + private static final Logger logger = LoggerFactory.getLogger(Main.class); + + public static void main(String[] args) { + logger.info("Starting CLI Crawler application"); + + ConsoleView view = new ConsoleView(); + ArticleRepository repository = new ArticleRepository(); + StrategyFactory strategyFactory = new StrategyFactory(); + CrawlerController controller = new CrawlerController(view, repository, strategyFactory); + + view.printSuccess("Welcome to CLI Crawler (w10_3)! Type help for commands."); + logger.debug("Application started successfully"); + + while (true) { + try { + controller.handle(view.readLine()); + } catch (Exception e) { + logger.error("Unexpected error in main loop", e); + view.printError("Unexpected error: " + e.getMessage()); + } + } + } +} diff --git a/w11/NetworkException.java b/w11/NetworkException.java new file mode 100644 index 0000000..8069ca2 --- /dev/null +++ b/w11/NetworkException.java @@ -0,0 +1,19 @@ +package com.example.datacollect.exception; + +public class NetworkException extends CrawlerException { + private final String url; + + public NetworkException(String message, String url) { + super(message); + this.url = url; + } + + public NetworkException(String message, String url, Throwable cause) { + super(message, cause); + this.url = url; + } + + public String getUrl() { + return url; + } +} \ No newline at end of file diff --git a/w11/ParseException.java b/w11/ParseException.java new file mode 100644 index 0000000..4bf6f73 --- /dev/null +++ b/w11/ParseException.java @@ -0,0 +1,19 @@ +package com.example.datacollect.exception; + +public class ParseException extends CrawlerException { + private final String url; + + public ParseException(String message, String url) { + super(message); + this.url = url; + } + + public ParseException(String message, String url, Throwable cause) { + super(message, cause); + this.url = url; + } + + public String getUrl() { + return url; + } +} \ No newline at end of file