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.
61 lines
2.0 KiB
61 lines
2.0 KiB
package internal.hw.crawler.commands;
|
|
|
|
import com.google.gson.Gson;
|
|
import internal.hw.crawler.models.Article;
|
|
import internal.hw.crawler.repositories.ArticleRepository;
|
|
import internal.hw.crawler.views.CommandOutput;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class SaveCommand implements Command {
|
|
Gson gson = new Gson();
|
|
private final ArticleRepository articleRepository;
|
|
private final CommandOutput out;
|
|
|
|
public SaveCommand(ArticleRepository articleRepository, CommandOutput out) {
|
|
this.articleRepository = articleRepository;
|
|
this.out = out;
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "save";
|
|
}
|
|
|
|
@Override
|
|
public void execute(String[] args) {
|
|
String filename = "articles.output.json";
|
|
|
|
List<Article> articles = getExistingArticles(filename);
|
|
Map<String, Article> articleMap = articles.stream().collect(Collectors.toMap(this::articleMapId, it -> it));
|
|
|
|
// Update existing articles with new articles
|
|
for (Article article : articleRepository.getAll()) {
|
|
articleMap.put(articleMapId(article), article);
|
|
}
|
|
|
|
Article[] articlesToSave = articleMap.values().toArray(new Article[0]);
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename));) {
|
|
writer.write(gson.toJson(articlesToSave));
|
|
out.success(String.format("Wrote %d articles to %s", articlesToSave.length, filename));
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
private String articleMapId(Article article) {
|
|
return String.format("%s-%s", article.getSource(), article.getId());
|
|
}
|
|
|
|
private List<Article> getExistingArticles(String filename) {
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
Article[] articles = gson.fromJson(reader, Article[].class);
|
|
return Arrays.asList(articles);
|
|
} catch (IOException e) {
|
|
return List.of();
|
|
}
|
|
}
|
|
}
|
|
|