5 changed files with 189 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
|
|||
public interface Command { |
|||
String getName(); |
|||
void execute(String[] args, ArticleRepository repository); |
|||
} |
|||
@ -0,0 +1,102 @@ |
|||
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; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
public class ExitCommand implements Command { |
|||
private static final Logger logger = LoggerFactory.getLogger(ExitCommand.class); |
|||
private final ConsoleView view; |
|||
|
|||
public ExitCommand(ConsoleView view) { |
|||
this.view = view; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "exit"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
logger.info("Exiting application"); |
|||
view.printSuccess("Bye!"); |
|||
System.exit(0); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
public class HelpCommand implements Command { |
|||
private static final Logger logger = LoggerFactory.getLogger(HelpCommand.class); |
|||
private final ConsoleView view; |
|||
|
|||
public HelpCommand(ConsoleView view) { |
|||
this.view = view; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "help"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
logger.info("Displaying help information"); |
|||
view.printInfo("Commands: crawl <url>, list, help, exit"); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
package com.example.datacollect.command; |
|||
|
|||
import com.example.datacollect.repository.ArticleRepository; |
|||
import com.example.datacollect.view.ConsoleView; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
public class ListCommand implements Command { |
|||
private static final Logger logger = LoggerFactory.getLogger(ListCommand.class); |
|||
private final ConsoleView view; |
|||
|
|||
public ListCommand(ConsoleView view) { |
|||
this.view = view; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "list"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
logger.info("Listing {} articles", repository.size()); |
|||
view.display(repository.getAll()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue