3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Paths;
6 import java.text.SimpleDateFormat;
8 import java.util.HashMap;
11 import java.util.stream.Collectors;
13 import org.jsoup.Jsoup;
14 import org.jsoup.nodes.Document;
15 import org.jsoup.select.Elements;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.fasterxml.jackson.core.type.TypeReference;
20 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import com.fasterxml.jackson.databind.ObjectWriter;
26 private final Date TODAY;
27 private final String dATE_FORMAT_STRING = "M月d日";
28 private final String BIRTH_DATA_URL = "generated/easter.json";
29 private final String WEBURL = "https://imas.gamedbs.jp/cg/idol/birthday";
31 private Logger logger = LoggerFactory.getLogger(Easter.class);
32 private Map<String, String> BirthData = this.fetchBirthData();
35 if(Files.notExists(Paths.get(this.BIRTH_DATA_URL))) {
36 this.writeBirthData(this.BirthData);
38 this.logger.info("LeT's mAkE ThIs aPp FuNnY...");
39 this.TODAY = new Date();
42 public Map<String, String> readBirthData() {
43 Map<String, String> res = null;
45 res = new ObjectMapper().readValue(Paths.get(this.BIRTH_DATA_URL).toFile(), new TypeReference<Map<String, String>>() {});
46 } catch (IOException e) {
47 this.logger.warn("Error while reading database from local.", e);
52 public void writeBirthData(Map<String, String> data) {
53 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
55 writer.writeValue(Paths.get(this.BIRTH_DATA_URL).toFile(), data);
56 } catch (IOException e) {
57 this.logger.error("Error while writing to database for easter.", e);
61 public Map<String, String> fetchBirthData() {
62 Map<String, String> res = new HashMap<>();
64 Document document = Jsoup.connect(this.WEBURL)
65 .userAgent("java/DelesteRandomSelector More information is available at https;//github.com/hizumiaoba/DelesteRandomSelector/")
69 Elements elements = document.getElementsByClass("font0");
71 .forEachOrdered(element -> {
72 String[] data = element.text().split(" ");
73 res.put(data[1], data[0]);
75 if(elements.size() != res.size()) {
76 throw new IllegalArgumentException(new IOException());
78 } catch (IOException e) {
79 this.logger.error("Error while fetching data for easter. this is not good...", e);
84 public String getTodaysBirth() {
85 Map<String, String> birthData = this.readBirthData();
86 SimpleDateFormat format = new SimpleDateFormat(this.dATE_FORMAT_STRING);
87 String todayDateString = format.format(this.TODAY);
88 List<String> res = birthData.entrySet().stream()
89 .filter((e) -> e.getValue().equals(todayDateString))
91 .collect(Collectors.toList());
92 StringBuilder builder = new StringBuilder("Happy Birth Day! : ");
93 for(String element : res) {
94 builder.append(element);
96 return res.isEmpty() ? "" : builder.toString();