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.
45 lines
1.3 KiB
45 lines
1.3 KiB
package com.crawler.controller;
|
|
|
|
import com.crawler.crawler.Crawler;
|
|
import com.crawler.exception.handler.ExceptionHandlerFactory;
|
|
import com.crawler.model.CrawlerConfig;
|
|
import com.crawler.model.CrawlerData;
|
|
import com.crawler.view.CrawlerView;
|
|
|
|
import java.util.List;
|
|
|
|
public class CrawlerController {
|
|
private Crawler crawler;
|
|
private CrawlerView view;
|
|
private ExceptionHandlerFactory exceptionHandlerFactory;
|
|
|
|
public CrawlerController(Crawler crawler) {
|
|
this.crawler = crawler;
|
|
this.view = new CrawlerView();
|
|
this.exceptionHandlerFactory = ExceptionHandlerFactory.getInstance();
|
|
}
|
|
|
|
public void setConfig(CrawlerConfig config) {
|
|
crawler.setConfig(config);
|
|
}
|
|
|
|
public void execute() {
|
|
view.showStartMessage(crawler.getCrawlerName());
|
|
|
|
try {
|
|
List<CrawlerData> dataList = crawler.crawl();
|
|
view.showData(dataList);
|
|
view.showSuccessMessage(dataList.size());
|
|
|
|
if (!dataList.isEmpty()) {
|
|
com.crawler.command.CacheCommand.saveDataWithPrompt(dataList, crawler.getCrawlerName());
|
|
}
|
|
} catch (Exception e) {
|
|
exceptionHandlerFactory.handleException(e, view);
|
|
}
|
|
}
|
|
|
|
public List<CrawlerData> crawl() {
|
|
return crawler.crawl();
|
|
}
|
|
}
|