5 changed files with 207 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import repository.PaperRepository; |
||||
|
|
||||
|
public interface Command { |
||||
|
void execute(String[] args, PaperRepository repository); |
||||
|
String getDescription(); |
||||
|
String getName(); |
||||
|
} |
||||
@ -0,0 +1,73 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import model.Paper; |
||||
|
import strategy.CrawlerStrategy; |
||||
|
import strategy.StrategyFactory; |
||||
|
import repository.PaperRepository; |
||||
|
import view.ConsoleView; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class CrawlCommand implements Command { |
||||
|
private ConsoleView view; |
||||
|
private StrategyFactory strategyFactory; |
||||
|
|
||||
|
public CrawlCommand(ConsoleView view, StrategyFactory strategyFactory) { |
||||
|
this.view = view; |
||||
|
this.strategyFactory = strategyFactory; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String[] args, PaperRepository repository) { |
||||
|
if (args.length < 2) { |
||||
|
view.showError("请提供要爬取的论文网站URL,格式: crawl <URL>"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
String url = args[1]; |
||||
|
|
||||
|
if (!isValidUrl(url)) { |
||||
|
view.showError("无效的URL格式,请提供有效的论文网站URL"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
view.showInfo("开始爬取论文..."); |
||||
|
view.showInfo("目标URL: " + url); |
||||
|
|
||||
|
CrawlerStrategy crawler = strategyFactory.createCrawlerByUrl(url); |
||||
|
if (crawler == null) { |
||||
|
view.showError("不支持的网站,请提供支持的论文网站URL"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
String platformName = crawler.getPlatformName(); |
||||
|
repository.init(platformName); |
||||
|
|
||||
|
List<Paper> papers = crawler.crawl(url, 10); |
||||
|
|
||||
|
if (papers.isEmpty()) { |
||||
|
view.showInfo("未获取到论文"); |
||||
|
} else { |
||||
|
List<Paper> uniquePapers = repository.removeDuplicates(papers); |
||||
|
repository.savePapers(uniquePapers); |
||||
|
view.showSuccess("成功爬取 " + uniquePapers.size() + " 篇论文"); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
view.showError("爬取失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private boolean isValidUrl(String url) { |
||||
|
return url != null && (url.startsWith("http://") || url.startsWith("https://")); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getDescription() { |
||||
|
return "爬取指定URL的论文,格式: crawl <论文网站URL>"; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getName() { |
||||
|
return "crawl"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import view.ConsoleView; |
||||
|
import repository.PaperRepository; |
||||
|
|
||||
|
public class ExitCommand implements Command { |
||||
|
private ConsoleView view; |
||||
|
|
||||
|
public ExitCommand(ConsoleView view) { |
||||
|
this.view = view; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String[] args, PaperRepository repository) { |
||||
|
view.showSuccess("程序已退出"); |
||||
|
System.exit(0); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getDescription() { |
||||
|
return "退出程序"; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getName() { |
||||
|
return "exit"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,33 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import view.ConsoleView; |
||||
|
import java.util.List; |
||||
|
import repository.PaperRepository; |
||||
|
|
||||
|
public class HelpCommand implements Command { |
||||
|
private ConsoleView view; |
||||
|
private List<Command> commands; |
||||
|
|
||||
|
public HelpCommand(ConsoleView view, List<Command> commands) { |
||||
|
this.view = view; |
||||
|
this.commands = commands; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String[] args, PaperRepository repository) { |
||||
|
view.showInfo("可用命令:"); |
||||
|
for (Command command : commands) { |
||||
|
view.showCommandInfo(command); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getDescription() { |
||||
|
return "显示帮助信息"; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getName() { |
||||
|
return "help"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import model.Paper; |
||||
|
import repository.PaperRepository; |
||||
|
import view.ConsoleView; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.Map.Entry; |
||||
|
|
||||
|
public class ListCommand implements Command { |
||||
|
private ConsoleView view; |
||||
|
|
||||
|
public ListCommand(ConsoleView view) { |
||||
|
this.view = view; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void execute(String[] args, PaperRepository repository) { |
||||
|
try { |
||||
|
Map<String, List<Paper>> papersByPlatform = repository.loadAllPapersGroupedByPlatform(); |
||||
|
|
||||
|
if (papersByPlatform.isEmpty()) { |
||||
|
view.showInfo("暂无论文,请先执行 crawl 命令爬取论文"); |
||||
|
} else { |
||||
|
int totalCount = 0; |
||||
|
for (Entry<String, List<Paper>> entry : papersByPlatform.entrySet()) { |
||||
|
totalCount += entry.getValue().size(); |
||||
|
} |
||||
|
|
||||
|
view.showInfo("当前已获取 " + totalCount + " 篇论文,分布在 " + papersByPlatform.size() + " 个平台:"); |
||||
|
System.out.println(); |
||||
|
|
||||
|
int globalIndex = 1; |
||||
|
for (Entry<String, List<Paper>> entry : papersByPlatform.entrySet()) { |
||||
|
String platformName = entry.getKey(); |
||||
|
List<Paper> papers = entry.getValue(); |
||||
|
|
||||
|
System.out.println("【" + platformName + "】 (" + papers.size() + " 篇)"); |
||||
|
System.out.println("-".repeat(50)); |
||||
|
|
||||
|
for (int i = 0; i < papers.size(); i++) { |
||||
|
Paper paper = papers.get(i); |
||||
|
view.showPaperInfo(globalIndex, paper); |
||||
|
globalIndex++; |
||||
|
} |
||||
|
|
||||
|
System.out.println(); |
||||
|
} |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
view.showError("查询失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getDescription() { |
||||
|
return "查询已获取的论文列表"; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getName() { |
||||
|
return "list"; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue