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.
55 lines
2.1 KiB
55 lines
2.1 KiB
package edu.homework.crawler.command;
|
|
|
|
import edu.homework.crawler.exception.CommandException;
|
|
import edu.homework.crawler.exception.CrawlException;
|
|
import edu.homework.crawler.model.CrawlRequest;
|
|
import edu.homework.crawler.repository.OutputFormat;
|
|
|
|
import java.nio.file.Path;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Map;
|
|
|
|
public class CrawlCommand implements Command {
|
|
private static final DateTimeFormatter FILE_TIME = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
|
|
|
|
@Override
|
|
public String name() {
|
|
return "crawl";
|
|
}
|
|
|
|
@Override
|
|
public String description() {
|
|
return "Crawl news and save to a JSON or CSV file.";
|
|
}
|
|
|
|
@Override
|
|
public void execute(CommandContext context, String[] args) throws CrawlException {
|
|
Map<String, String> options = context.commandRegistry().parseOptions(args);
|
|
String site = options.getOrDefault("site", "all");
|
|
int limit = parseLimit(options.getOrDefault("limit", "10"));
|
|
OutputFormat format = OutputFormat.from(options.getOrDefault("format", "json"));
|
|
Path outputPath = Path.of(options.getOrDefault("out", defaultOutput(format)));
|
|
|
|
context.view().printInfo("Starting crawl: site=" + site + ", limit=" + limit + ", format=" + format.name().toLowerCase());
|
|
CrawlRequest request = new CrawlRequest(site, limit, format, outputPath);
|
|
context.view().printSummary(context.controller().crawl(request));
|
|
}
|
|
|
|
private int parseLimit(String value) throws CommandException {
|
|
try {
|
|
int limit = Integer.parseInt(value);
|
|
if (limit <= 0 || limit > 100) {
|
|
throw new CommandException("Limit must be between 1 and 100.");
|
|
}
|
|
return limit;
|
|
} catch (NumberFormatException e) {
|
|
throw new CommandException("Limit must be a number: " + value);
|
|
}
|
|
}
|
|
|
|
private String defaultOutput(OutputFormat format) {
|
|
String suffix = format == OutputFormat.JSON ? ".json" : ".csv";
|
|
return "data/news-" + LocalDateTime.now().format(FILE_TIME) + suffix;
|
|
}
|
|
}
|
|
|