OSDN Git Service

fix: fix async process order.
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / AlbumTypeEstimate.java
1 package com.ranfa.lib;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.concurrent.CompletableFuture;
6 import java.util.concurrent.ExecutionException;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9
10 import org.jsoup.Jsoup;
11 import org.jsoup.nodes.Document;
12 import org.jsoup.select.Elements;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import com.ranfa.lib.concurrent.CountedThreadFactory;
17
18 public class AlbumTypeEstimate {
19
20         public final static String ALBUM_DATA_URI = "https://imascg-slstage-wiki.gamerch.com/%E6%A5%BD%E6%9B%B2%E8%A9%B3%E7%B4%B0%E4%B8%80%E8%A6%A7%EF%BC%88%E9%9B%A3%E6%98%93%E5%BA%A6%E5%88%A5%EF%BC%89";
21         private static ExecutorService executor = Executors.newCachedThreadPool(new CountedThreadFactory(() -> "DRS", "AlbumEstimateThread"));
22         private static Logger logger = LoggerFactory.getLogger(AlbumTypeEstimate.class);
23
24         //アルバム種類の定義
25         public enum ALBUM_TYPE {
26                 // 全体曲+キュート
27                 ALBUM_A,
28                 //全体曲+クール
29                 ALBUM_B,
30                 //全体曲+パッション
31                 ALBUM_C
32         };
33
34         public enum MASTERPLUS_TYPE {
35                 //新MASTER+
36                 NEWMASTERPLUS,
37                 //旧MASTER+
38                 LEGACYMASTERPLUS
39         }
40
41         public static ArrayList<ArrayList<Album>> getAlbumType() {
42                 long time = System.currentTimeMillis();
43                 ArrayList<ArrayList<Album>> res = new ArrayList<>();
44                 try {
45                         Document document = Jsoup.connect(ALBUM_DATA_URI)
46                                         .userAgent("Java/DelesteRandomSelector  More Information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
47                                         .maxBodySize(0)
48                                         .timeout(0)
49                                         .get();
50                         Elements elements = document.getElementsByTag("tbody");
51                         Elements newMasterPlus = elements.get(4).select("tr");
52                         Elements legacyMasterPlus = elements.get(5).select("tr");
53                         CompletableFuture<ArrayList<Album>> fetchNew = CompletableFuture.supplyAsync(() -> fetch(newMasterPlus), executor);
54                         CompletableFuture<ArrayList<Album>> fetchLegacy = CompletableFuture.supplyAsync(() -> fetch(legacyMasterPlus), executor);
55                         res.add(fetchNew.get());
56                         res.add(fetchLegacy.get());
57                 } catch (IOException | InterruptedException | ExecutionException e) {
58                         logger.warn("Exception was thrown while fetching Album type.", e);
59                 }
60                 logger.info("Album type fetched in {} ms", (System.currentTimeMillis() - time));
61                 return res;
62         }
63
64         private static ArrayList<Album> fetch(Elements elements) {
65                 ArrayList<Album> res = new ArrayList<>();
66                 elements.stream()
67                 .forEach(element -> {
68                         String type = element.select("td").get(0).text().isEmpty() ? "Not-implemented" : element.select("td").get(0).text();
69                         String songName = element.select("td").get(2).text();
70                         Album tmp = new Album();
71                         tmp.setSongName(songName);
72                         tmp.setAlbumType(type);
73                         res.add(tmp);
74                 });
75                 return res;
76         }
77 }