OSDN Git Service

feat: added Album type fetch class
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / AlbumTypeEstimate.java
index d9fb834..4df9aac 100644 (file)
@@ -1,7 +1,9 @@
 package com.ranfa.lib;
 
 import java.io.IOException;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 
 import org.jsoup.Jsoup;
 import org.jsoup.nodes.Document;
@@ -21,19 +23,47 @@ public class AlbumTypeEstimate {
                ALBUM_C
        };
 
-       public static Map<String, String> getLastModified() {
+       public enum MASTERPLUS_TYPE {
+               //新MASTER+
+               NEWMASTERPLUS,
+               //旧MASTER+
+               LEGACYMASTERPLUS
+       }
+
+       public static ArrayList<ArrayList<Album>> getAlbumType() {
                long time = System.currentTimeMillis();
+               ArrayList<ArrayList<Album>> res = new ArrayList<ArrayList<Album>>();
                try {
                        Document document = Jsoup.connect(ALBUM_DATA_URI)
                                        .userAgent("Java/DelesteRandomSelector  More Information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
                                        .maxBodySize(0)
                                        .timeout(0)
                                        .get();
-                       Elements elements = document.head().getElementsByAttribute("Last-Modified");
-               } catch (IOException e) {
-                       // TODO 自動生成された catch ブロック
+                       Elements elements = document.getElementsByTag("tbody");
+                       Elements newMasterPlus = elements.get(4).select("tr");
+                       Elements legacyMasterPlus = elements.get(5).select("tr");
+                       CompletableFuture<ArrayList<Album>> fetchNew = CompletableFuture.supplyAsync(() -> fetch(newMasterPlus));
+                       CompletableFuture<ArrayList<Album>> fetchLegacy = CompletableFuture.supplyAsync(() -> fetch(legacyMasterPlus));
+                       res.add(fetchNew.get());
+                       res.add(fetchLegacy.get());
+               } catch (IOException | InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                }
+               LimitedLog.println(AlbumTypeEstimate.class + ":[INFO]: " + "Album type fetched in " + (System.currentTimeMillis() - time) + "ms");
+               return res;
        }
 
+       private static ArrayList<Album> fetch(Elements elements) {
+               ArrayList<Album> res = new ArrayList<>();
+               elements.stream()
+                       .forEach(element -> {
+                               String type = element.select("td").get(0).text().isEmpty() ? "Not-implemented" : element.select("td").get(0).text();
+                               String songName = element.select("td").get(2).text();
+                               Album tmp = new Album();
+                               tmp.setSongName(songName);
+                               tmp.setAlbumType(type);
+                               res.add(tmp);
+                       });
+               return res;
+       }
 }