3 changed files with 85 additions and 0 deletions
@ -0,0 +1,71 @@ |
|||
|
|||
|
|||
|
|||
import com.example.datacollect.model.Article; |
|||
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 java.util.List; |
|||
|
|||
public class AnalyzeCommand implements Command { |
|||
private final ConsoleView view; |
|||
private final StrategyFactory strategyFactory; |
|||
|
|||
public AnalyzeCommand(ConsoleView view, StrategyFactory strategyFactory) { |
|||
this.view = view; |
|||
this.strategyFactory = strategyFactory; |
|||
} |
|||
|
|||
@Override |
|||
public String getName() { |
|||
return "analyze"; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(String[] args, ArticleRepository repository) { |
|||
if (args.length < 2) { |
|||
view.printError("Usage: analyze <url>"); |
|||
return; |
|||
} |
|||
String url = args[1]; |
|||
|
|||
CrawlStrategy strategy = strategyFactory.getStrategy(url); |
|||
if (strategy == null) { |
|||
view.printError("No strategy found for: " + url); |
|||
return; |
|||
} |
|||
|
|||
try { |
|||
view.printInfo("Analyzing: " + url); |
|||
view.printInfo("Using strategy: " + strategy.getClass().getSimpleName()); |
|||
|
|||
Document doc = Jsoup.connect(url).get(); |
|||
List<Article> articles = strategy.parse(url, doc); |
|||
|
|||
view.printSuccess("=== Analysis Statistics ==="); |
|||
view.printSuccess("URL: " + url); |
|||
view.printSuccess("Strategy: " + strategy.getClass().getSimpleName()); |
|||
view.printSuccess("Articles found: " + articles.size()); |
|||
|
|||
if (!articles.isEmpty()) { |
|||
view.printSuccess("Sample titles:"); |
|||
int count = Math.min(3, articles.size()); |
|||
for (int i = 0; i < count; i++) { |
|||
view.printSuccess(" - " + articles.get(i).getTitle()); |
|||
} |
|||
if (articles.size() > 3) { |
|||
view.printSuccess(" ... and " + (articles.size() - 3) + " more"); |
|||
} |
|||
} |
|||
|
|||
view.printSuccess("Note: Articles were NOT stored in repository"); |
|||
|
|||
} catch (Exception e) { |
|||
view.printError("Failed to analyze: " + e.getMessage()); |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||
|
|||
|
|||
import com.example.datacollect.model.Article; |
|||
import org.jsoup.nodes.Document; |
|||
import java.util.List; |
|||
|
|||
public interface CrawlStrategy { |
|||
List<Article> parse(String url, Document doc); |
|||
boolean supports(String url); |
|||
|
|||
default int getPriority() { |
|||
return 0; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue