4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.util.ArrayList;
9 import java.util.concurrent.CompletableFuture;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
14 import javax.swing.JOptionPane;
16 import org.jsoup.Jsoup;
17 import org.jsoup.nodes.Document;
18 import org.jsoup.select.Elements;
20 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import com.fasterxml.jackson.databind.ObjectWriter;
23 import com.ranfa.lib.AlbumTypeEstimate.MASTERPLUS_TYPE;
25 public class Scraping {
27 private final static String 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";
28 private final static String DBPATH = "generated/database.json";
29 public final static String NONSELECTED = "指定なし";
30 public final static String ALL = "全タイプ";
31 public final static String CUTE = "キュート";
32 public final static String COOL = "クール";
33 public final static String PASSION = "パッション";
34 public final static String DEBUT = "DEBUT";
35 public final static String REGULAR = "REGULAR";
36 public final static String PRO = "PRO";
37 public final static String MASTER = "MASTER";
38 public final static String MASTERPLUS = "MASTER+";
39 public final static String LEGACYMASTERPLUS = "ⓁMASTER+";
40 public final static String LIGHT = "LIGHT";
41 public final static String TRICK = "TRICK";
42 public final static String PIANO = "PIANO";
43 public final static String FORTE = "FORTE";
44 public final static String WITCH = "WITCH";
46 public static boolean databaseExists() {
47 Path path = Paths.get(DBPATH);
48 return Files.exists(path);
51 public static String getDBPath() {
55 public static ArrayList<Song> getWholeData() {
56 long time = System.currentTimeMillis();
57 ExecutorService es = Executors.newWorkStealingPool();
58 CompletableFuture<ArrayList<ArrayList<Album>>> typeFetchFuture = CompletableFuture.supplyAsync(() -> AlbumTypeEstimate.getAlbumType(), es);
59 // if(databaseExists())
61 ArrayList<Song> res = new ArrayList<>();
64 Document document = Jsoup.connect(URI)
65 .userAgent("Java/DeresteRandomSelector More information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
69 Elements rows = document.getElementsByTag("tbody").get(0).select("tr");
70 ArrayList<ArrayList<Album>> typeLists = typeFetchFuture.get();
71 for(int i = 0; i < rows.size(); i++) {
72 String attribute = rows.get(i).select("td").get(0).text();
73 String name = rows.get(i).select("td").get(1).text();
74 String difficulty = rows.get(i).select("td").get(2).text();
75 int level = Integer.parseInt(rows.get(i).select("td").get(3).text());
77 if(rows.get(i).select("td").get(5).text().indexOf(",") == -1) {
78 notes = Integer.parseInt(rows.get(i).select("td").get(5).text());
80 String temp = rows.get(i).select("td").get(5).text();
81 String first = temp.substring(0, temp.indexOf(","));
82 String end = temp.substring(temp.indexOf(",") + 1);
83 notes = Integer.parseInt(first + end);
85 Song tmp = new Song();
86 tmp.setAttribute(attribute);
88 tmp.setDifficulty(difficulty);
91 if(difficulty.equals(LEGACYMASTERPLUS)) {
92 ArrayList<Album> newTypeList = typeLists.get(MASTERPLUS_TYPE.LEGACYMASTERPLUS.ordinal());
93 for(int j = 0; j < newTypeList.size(); j++) {
94 if(newTypeList.get(j).getSongName().equals(name))
95 tmp.setAlbumType(newTypeList.get(j).getAlbumType());
97 } else if(difficulty.equals(MASTERPLUS)) {
98 ArrayList<Album> legacyTypeList = typeLists.get(MASTERPLUS_TYPE.NEWMASTERPLUS.ordinal());
99 for(int j = 0; j < legacyTypeList.size(); j++) {
100 if(legacyTypeList.get(j).getSongName().equals(name))
101 tmp.setAlbumType(legacyTypeList.get(j).getAlbumType());
104 tmp.setAlbumType("Not-Implemented");
108 } catch (IOException | InterruptedException | ExecutionException e) {
111 LimitedLog.println(Scraping.class + ":[INFO]: scraping compeleted in " + (System.currentTimeMillis() - time)+ "ms");
115 public static ArrayList<Song> getSpecificAttributeSongs(ArrayList<Song> data, String attribute) {
116 if(!attribute.equals(ALL)
117 && !attribute.equals(CUTE)
118 && !attribute.equals(COOL)
119 && !attribute.equals(PASSION)
120 && !attribute.equals(NONSELECTED))
121 throw new IllegalArgumentException("Illegal attribute value: " + attribute);
123 JOptionPane.showMessageDialog(null, "指定された属性の曲は存在しません。\n条件を変えてみてください");
124 throw new IllegalArgumentException("ArrayList must not empty.");
126 ArrayList<Song> res = new ArrayList<Song>();
127 if(attribute.equals(NONSELECTED)) {
131 .filter(element -> element.getAttribute().equals(attribute))
137 public static ArrayList<Song> getSpecificDifficultySongs(ArrayList<Song> data, String difficulty) {
138 if(!difficulty.equals(DEBUT)
139 && !difficulty.equals(REGULAR)
140 && !difficulty.equals(PRO)
141 && !difficulty.equals(MASTER)
142 && !difficulty.equals(MASTERPLUS)
143 && !difficulty.equals(LEGACYMASTERPLUS)
144 && !difficulty.equals(LIGHT)
145 && !difficulty.equals(TRICK)
146 && !difficulty.equals(PIANO)
147 && !difficulty.equals(FORTE)
148 && !difficulty.equals(WITCH)
149 && !difficulty.equals(NONSELECTED))
150 throw new IllegalArgumentException("Illegal difficulty value.");
152 throw new IllegalArgumentException("ArrayList must not empty.");
153 ArrayList<Song> res = new ArrayList<Song>();
154 if(difficulty.equals(NONSELECTED)) {
158 .filter(element -> element.getDifficulty().equals(difficulty))
164 public static ArrayList<Song> getSpecificLevelSongs(ArrayList<Song> data, int level, boolean isLess, boolean isMore) {
166 throw new IllegalArgumentException("Level must not negative.");
168 throw new IllegalArgumentException("ArrayList must not empty.");
169 if(!(isLess || isMore))
170 throw new IllegalArgumentException("Illegal boolean value.");
172 return getOnlyLevelSongs(data, level);
173 ArrayList<Song> res = new ArrayList<Song>();
176 .filter(element -> element.getLevel() < level)
180 .filter(element -> element.getLevel() > level)
186 private static ArrayList<Song> getOnlyLevelSongs(ArrayList<Song> data, int level) {
187 ArrayList<Song> res = new ArrayList<Song>();
189 .filter(element -> element.getLevel() == level)
195 public static ArrayList<Song> getFromJson() {
196 long time = System.currentTimeMillis();
197 SongJSONProperty property = null;
199 property = new ObjectMapper().readValue(new File(DBPATH), SongJSONProperty.class);
200 } catch (IOException e) {
201 // TODO 自動生成された catch ブロック
204 ArrayList<Song> res = new ArrayList<Song>();
205 res.addAll(property.getList());
206 LimitedLog.println(Scraping.class + ":[INFO]: JSON reading compeleted in " + (System.currentTimeMillis() - time) + "ms");
210 public static boolean writeToJson(ArrayList<Song> list) {
212 SongJSONProperty property = new SongJSONProperty();
213 property.setList(list);
214 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
216 writer.writeValue(Paths.get(DBPATH).toFile(), property);
217 } catch (IOException e) {