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.
73 lines
2.2 KiB
73 lines
2.2 KiB
package com.crawler.controller;
|
|
|
|
import java.util.List;
|
|
|
|
import com.crawler.factory.StrategyFactory;
|
|
import com.crawler.model.Article;
|
|
import com.crawler.repository.ArticleRepository;
|
|
import com.crawler.strategy.CrawlStrategy;
|
|
import com.crawler.util.DataPersistence;
|
|
import com.crawler.view.ConsoleView;
|
|
|
|
public class CrawlerController {
|
|
private final ArticleRepository repository;
|
|
private final ConsoleView view;
|
|
|
|
public CrawlerController(ArticleRepository repository, ConsoleView view) {
|
|
this.repository = repository;
|
|
this.view = view;
|
|
loadSavedData();
|
|
}
|
|
|
|
private void loadSavedData() {
|
|
List<Article> savedArticles = DataPersistence.loadArticles();
|
|
if (!savedArticles.isEmpty()) {
|
|
repository.saveAll(savedArticles);
|
|
view.displayInfo("Loaded " + savedArticles.size() + " saved articles");
|
|
}
|
|
}
|
|
|
|
public void crawl(String url, String strategyName) throws Exception {
|
|
if (url == null || url.trim().isEmpty()) {
|
|
throw new IllegalArgumentException("URL cannot be empty");
|
|
}
|
|
|
|
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
|
url = "https://" + url;
|
|
}
|
|
|
|
if (repository.existsByUrl(url)) {
|
|
view.displayWarning("URL already crawled: " + url);
|
|
return;
|
|
}
|
|
|
|
view.displayInfo("Crawling: " + url);
|
|
view.displayInfo("Using strategy: " + strategyName);
|
|
|
|
CrawlStrategy strategy = StrategyFactory.getStrategy(strategyName);
|
|
List<Article> articles = strategy.crawl(url);
|
|
|
|
for (Article article : articles) {
|
|
repository.save(article);
|
|
view.displaySuccess("Crawled: " + article.getTitle());
|
|
}
|
|
|
|
saveData();
|
|
}
|
|
|
|
public void listArticles() {
|
|
List<Article> articles = repository.findAll();
|
|
view.displayArticleList(articles);
|
|
}
|
|
|
|
public void saveData() {
|
|
List<Article> articles = repository.findAll();
|
|
DataPersistence.saveArticles(articles);
|
|
}
|
|
|
|
public void loadData() {
|
|
repository.deleteAll();
|
|
List<Article> savedArticles = DataPersistence.loadArticles();
|
|
repository.saveAll(savedArticles);
|
|
}
|
|
}
|
|
|