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.
 
 

101 lines
4.2 KiB

import command.AnalyzeCommand;
import command.HeroCrawlCommand;
import command.MovieCrawlCommand;
import command.WeatherAnalyzeCommand;
import command.WeatherCrawlCommand;
import command.CommandInvoker;
import controller.CrawlerContext;
import exception.CrawlerException;
import exception.NetworkException;
import exception.ParseException;
import model.Hero;
import model.Movie;
import model.Weather;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.DataUtil;
import view.CrawlerView;
import java.io.IOException;
import java.util.List;
// ===================== 10. 主程序入口 =====================
public class CrawlerMain {
private static final Logger logger = LoggerFactory.getLogger(CrawlerMain.class);
public static void main(String[] args) {
logger.info("===== 爬虫程序启动(CLI+MVC+Command+策略模式) =====");
CrawlerView view = new CrawlerView();
CrawlerContext context = new CrawlerContext();
CommandInvoker invoker = new CommandInvoker();
MovieCrawlCommand movieCmd = new MovieCrawlCommand(context);
HeroCrawlCommand heroCmd = new HeroCrawlCommand(context);
WeatherCrawlCommand weatherCmd = new WeatherCrawlCommand(context);
AnalyzeCommand analyzeCmd = new AnalyzeCommand(movieCmd, heroCmd);
WeatherAnalyzeCommand weatherAnalyzeCmd = new WeatherAnalyzeCommand(weatherCmd);
DataUtil.initFolder();
while (true) {
try {
view.showMenu();
int op = view.getInput();
switch (op) {
case 1:
invoker.runCommand(movieCmd);
view.showMsg("电影爬取完成");
break;
case 2:
invoker.runCommand(heroCmd);
view.showMsg("英雄爬取完成");
break;
case 3:
invoker.runCommand(weatherCmd);
view.showMsg("天气爬取完成");
break;
case 4:
analyzeCmd.execute();
break;
case 5:
weatherAnalyzeCmd.execute();
break;
// 第6项:导入历史数据(无fileName:,无报错)
case 6:
try {
List<Movie> movieList = DataUtil.importJson("movie.json", Movie.class);
List<Hero> heroList = DataUtil.importJson("hero.json", Hero.class);
List<Weather> weatherList = DataUtil.importJson("weather.json", Weather.class);
view.showMsg("✅ 历史数据导入成功!");
view.showMsg("电影:" + movieList.size() + " 条");
view.showMsg("英雄:" + heroList.size() + " 条");
view.showMsg("天气:" + weatherList.size() + " 条");
} catch (IOException e) {
view.showMsg("导入失败:" + e.getMessage());
}
break;
case 0:
view.showMsg("程序退出");
System.exit(0);
break;
default:
view.showMsg("指令错误,请重新输入");
}
} catch (NumberFormatException e) {
view.showMsg("请输入数字!");
} catch (NetworkException e) {
logger.error("网络异常:", e);
view.showMsg("网络异常:" + e.getMessage());
} catch (ParseException e) {
logger.error("解析异常:", e);
view.showMsg("解析异常:" + e.getMessage());
} catch (CrawlerException e) {
logger.error("爬虫异常:", e);
view.showMsg("爬虫异常:" + e.getMessage());
}catch (IOException e) {
logger.error("IO异常:", e);
view.showMsg("IO异常:" + e.getMessage());
}
}
}
}