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.5 KiB
101 lines
4.5 KiB
package com.crawler.view;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
|
|
import com.crawler.model.Article;
|
|
import com.crawler.util.ColorUtil;
|
|
|
|
public class ConsoleView {
|
|
private static final Scanner scanner = new Scanner(System.in);
|
|
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
public void displayWelcome() {
|
|
System.out.println(ColorUtil.cyan("========================================"));
|
|
System.out.println(ColorUtil.cyan(" Welcome to My Crawler "));
|
|
System.out.println(ColorUtil.cyan("========================================"));
|
|
System.out.println();
|
|
}
|
|
|
|
public void displayHelp() {
|
|
System.out.println(ColorUtil.yellow("Available commands:"));
|
|
System.out.println(ColorUtil.green(" crawl <url> [strategy] - Crawl a website"));
|
|
System.out.println(ColorUtil.green(" list - List all crawled articles"));
|
|
System.out.println(ColorUtil.green(" save - Save articles to data file"));
|
|
System.out.println(ColorUtil.green(" load - Load articles from data file"));
|
|
System.out.println(ColorUtil.green(" help - Show this help message"));
|
|
System.out.println(ColorUtil.green(" exit - Exit the application"));
|
|
System.out.println();
|
|
System.out.println(ColorUtil.yellow("Available strategies:"));
|
|
System.out.println(ColorUtil.cyan(" blog - Blog crawling strategy"));
|
|
System.out.println(ColorUtil.cyan(" news - News crawling strategy"));
|
|
System.out.println(ColorUtil.cyan(" jsoup - Generic JSoup strategy (default)"));
|
|
System.out.println();
|
|
}
|
|
|
|
public void displayArticleList(List<Article> articles) {
|
|
if (articles.isEmpty()) {
|
|
System.out.println(ColorUtil.yellow("No articles found."));
|
|
return;
|
|
}
|
|
|
|
System.out.println(ColorUtil.cyan("=== Crawled Articles (" + articles.size() + ") ==="));
|
|
System.out.println();
|
|
|
|
for (int i = 0; i < articles.size(); i++) {
|
|
displayArticleDetail(articles.get(i), i + 1);
|
|
}
|
|
}
|
|
|
|
public void displayArticleDetail(Article article, int index) {
|
|
System.out.println(ColorUtil.bold(ColorUtil.green("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")));
|
|
System.out.println(ColorUtil.bold(ColorUtil.yellow("[" + index + "] " + article.getTitle())));
|
|
System.out.println(ColorUtil.bold(ColorUtil.green("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")));
|
|
System.out.println(ColorUtil.cyan(" ID: ") + article.getId());
|
|
System.out.println(ColorUtil.cyan(" URL: ") + article.getUrl());
|
|
if (article.getAuthor() != null) {
|
|
System.out.println(ColorUtil.cyan(" Author: ") + article.getAuthor());
|
|
}
|
|
if (article.getSource() != null) {
|
|
System.out.println(ColorUtil.cyan(" Source: ") + article.getSource());
|
|
}
|
|
if (article.getPublishDate() != null) {
|
|
System.out.println(ColorUtil.cyan(" Published: ") + article.getPublishDate().format(DATE_FORMATTER));
|
|
}
|
|
System.out.println(ColorUtil.cyan(" Crawled: ") + article.getCrawlDate().format(DATE_FORMATTER));
|
|
System.out.println(ColorUtil.cyan(" Content: "));
|
|
if (article.getContent() != null) {
|
|
String[] lines = article.getContent().split("(?<=\\G.{80})");
|
|
for (String line : lines) {
|
|
System.out.println(" " + line);
|
|
}
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
public void displaySuccess(String message) {
|
|
System.out.println(ColorUtil.green("✓ " + message));
|
|
}
|
|
|
|
public void displayError(String message) {
|
|
System.out.println(ColorUtil.red("✗ " + message));
|
|
}
|
|
|
|
public void displayInfo(String message) {
|
|
System.out.println(ColorUtil.blue("ℹ " + message));
|
|
}
|
|
|
|
public void displayWarning(String message) {
|
|
System.out.println(ColorUtil.yellow("⚠ " + message));
|
|
}
|
|
|
|
public String readInput() {
|
|
System.out.print(ColorUtil.purple("> "));
|
|
return scanner.nextLine().trim();
|
|
}
|
|
|
|
public void displayGoodbye() {
|
|
System.out.println(ColorUtil.cyan("Goodbye! Thank you for using My Crawler."));
|
|
}
|
|
}
|
|
|