6 changed files with 81 additions and 4 deletions
@ -0,0 +1,61 @@ |
|||||
|
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(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue