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.
81 lines
3.4 KiB
81 lines
3.4 KiB
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
import java.util.logging.Logger;
|
|
|
|
public class Main11 {
|
|
private static final Logger logger = Logger.getLogger(Main11.class.getName());
|
|
private static final int MAX_RETRY_TIMES = 3;
|
|
|
|
public static void main(String[] args) {
|
|
StrategyFactory11 strategyFactory = new StrategyFactory11();
|
|
CommandRegistry11 cmdRegistry = new CommandRegistry11();
|
|
HistoryCommand11 historyCmd = new HistoryCommand11();
|
|
ArticleRepository11 repository = new ArticleRepository11();
|
|
|
|
// 模拟爬取策略 + 失败重试逻辑
|
|
CrawlStrategy11 blogStrategy = new CrawlStrategy11() {
|
|
@Override
|
|
public boolean supports(String url) {
|
|
return url != null && url.contains("blog");
|
|
}
|
|
|
|
@Override
|
|
public Article11 crawl(String url) throws ParseException11, NetworkException11 {
|
|
for (int i = 1; i <= MAX_RETRY_TIMES; i++) {
|
|
try {
|
|
double random = Math.random();
|
|
if (random < 0.3) {
|
|
throw new NetworkException11("网络连接超时");
|
|
}
|
|
if (random < 0.2) {
|
|
throw new ParseException11("页面内容解析失败");
|
|
}
|
|
return new Article11("博客文章", url, "正文内容", "博主", "2026-05-30");
|
|
} catch (NetworkException11 e) {
|
|
logger.warning("第 " + i + " 次爬取异常:" + e.getMessage());
|
|
if (i == MAX_RETRY_TIMES) {
|
|
throw new NetworkException11("已重试" + MAX_RETRY_TIMES + "次,任务终止", e);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
};
|
|
strategyFactory.registerStrategy(blogStrategy);
|
|
|
|
cmdRegistry.registerCommand(new HelpCommand11());
|
|
cmdRegistry.registerCommand(historyCmd);
|
|
cmdRegistry.registerCommand(new AnalyzeCommand11(strategyFactory));
|
|
cmdRegistry.registerCommand(new ExitCommand11());
|
|
|
|
List<Article11> articleData = new ArrayList<>();
|
|
Scanner scanner = new Scanner(System.in);
|
|
logger.info("W11 爬虫程序启动成功");
|
|
System.out.println("===== W11 爬虫程序启动 =====");
|
|
|
|
while (true) {
|
|
System.out.print("请输入命令:");
|
|
String input = scanner.nextLine().trim();
|
|
historyCmd.addCommand(input);
|
|
logger.info("用户输入命令:" + input);
|
|
|
|
String[] splitArr = input.split(" ", 2);
|
|
String cmdName = splitArr[0];
|
|
Command11 command = cmdRegistry.getCommand(cmdName);
|
|
|
|
if (command != null) {
|
|
String[] params = splitArr.length > 1 ? new String[]{splitArr[1]} : new String[0];
|
|
try {
|
|
command.execute(params, articleData);
|
|
} catch (Exception e) {
|
|
logger.severe("命令执行发生异常:" + e.getMessage());
|
|
System.out.println("命令执行出错,请查看日志!");
|
|
}
|
|
} else {
|
|
logger.warning("未知命令:" + cmdName);
|
|
System.out.println("未知命令,请输入 help 查看帮助");
|
|
}
|
|
}
|
|
}
|
|
}
|