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;
10 import javax.swing.JOptionPane;
12 import org.jsoup.Jsoup;
13 import org.jsoup.nodes.Document;
14 import org.jsoup.select.Elements;
16 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import com.fasterxml.jackson.databind.ObjectWriter;
20 public class Scraping {
22 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";
23 private final static String DBPATH = "database.json";
24 public final static String NONSELECTED = "指定なし";
25 public final static String ALL = "全タイプ";
26 public final static String CUTE = "キュート";
27 public final static String COOL = "クール";
28 public final static String PASSION = "パッション";
29 public final static String DEBUT = "DEBUT";
30 public final static String REGULAR = "REGULAR";
31 public final static String PRO = "PRO";
32 public final static String MASTER = "MASTER";
33 public final static String MASTERPLUS = "MASTER+";
34 public final static String LEGACYMASTERPLUS = "ⓁMASTER+";
35 public final static String LIGHT = "LIGHT";
36 public final static String TRICK = "TRICK";
37 public final static String PIANO = "PIANO";
38 public final static String FORTE = "FORTE";
39 public final static String WITCH = "WITCH";
41 public static boolean databaseExists() {
42 Path path = Paths.get(DBPATH);
43 return Files.exists(path);
46 public static String getDBPath() {
50 public static ArrayList<Song> getWholeData() {
51 long time = System.currentTimeMillis();
52 // if(databaseExists())
54 ArrayList<Song> res = new ArrayList<>();
57 Document document = Jsoup.connect(URI)
58 .userAgent("Java/DeresteRandomSelector More information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
62 Elements rows = document.getElementsByTag("tbody").get(0).select("tr");
63 for(int i = 0; i < rows.size(); i++) {
64 String attribute = rows.get(i).select("td").get(0).text();
65 String name = rows.get(i).select("td").get(1).text();
66 String difficulty = rows.get(i).select("td").get(2).text();
67 int level = Integer.parseInt(rows.get(i).select("td").get(3).text());
69 if(rows.get(i).select("td").get(5).text().indexOf(",") == -1) {
70 notes = Integer.parseInt(rows.get(i).select("td").get(5).text());
72 String temp = rows.get(i).select("td").get(5).text();
73 String first = temp.substring(0, temp.indexOf(","));
74 String end = temp.substring(temp.indexOf(",") + 1);
75 notes = Integer.parseInt(first + end);
77 Song tmp = new Song();
78 tmp.setAttribute(attribute);
80 tmp.setDifficulty(difficulty);
85 } catch (IOException e) {
88 LimitedLog.println(Scraping.class + ":[INFO]: scraping compeleted in " + (System.currentTimeMillis() - time)+ "ms");
92 public static ArrayList<Song> getSpecificAttributeSongs(ArrayList<Song> data, String attribute) {
93 if(!attribute.equals(ALL)
94 && !attribute.equals(CUTE)
95 && !attribute.equals(COOL)
96 && !attribute.equals(PASSION)
97 && !attribute.equals(NONSELECTED))
98 throw new IllegalArgumentException("Illegal attribute value: " + attribute);
100 JOptionPane.showMessageDialog(null, "指定された属性の曲は存在しません。\n条件を変えてみてください");
101 throw new IllegalArgumentException("ArrayList must not empty.");
103 ArrayList<Song> res = new ArrayList<Song>();
104 if(attribute.equals(NONSELECTED)) {
108 .filter(element -> element.getAttribute().equals(attribute))
114 public static ArrayList<Song> getSpecificDifficultySongs(ArrayList<Song> data, String difficulty) {
115 if(!difficulty.equals(DEBUT)
116 && !difficulty.equals(REGULAR)
117 && !difficulty.equals(PRO)
118 && !difficulty.equals(MASTER)
119 && !difficulty.equals(MASTERPLUS)
120 && !difficulty.equals(LEGACYMASTERPLUS)
121 && !difficulty.equals(LIGHT)
122 && !difficulty.equals(TRICK)
123 && !difficulty.equals(PIANO)
124 && !difficulty.equals(FORTE)
125 && !difficulty.equals(WITCH)
126 && !difficulty.equals(NONSELECTED))
127 throw new IllegalArgumentException("Illegal difficulty value.");
129 throw new IllegalArgumentException("ArrayList must not empty.");
130 ArrayList<Song> res = new ArrayList<Song>();
131 if(difficulty.equals(NONSELECTED)) {
135 .filter(element -> element.getDifficulty().equals(difficulty))
141 public static ArrayList<Song> getSpecificLevelSongs(ArrayList<Song> data, int level, boolean isLess, boolean isMore) {
143 throw new IllegalArgumentException("Level must not negative.");
145 throw new IllegalArgumentException("ArrayList must not empty.");
146 if(!(isLess || isMore))
147 throw new IllegalArgumentException("Illegal boolean value.");
149 return getOnlyLevelSongs(data, level);
150 ArrayList<Song> res = new ArrayList<Song>();
153 .filter(element -> element.getLevel() < level)
157 .filter(element -> element.getLevel() > level)
163 private static ArrayList<Song> getOnlyLevelSongs(ArrayList<Song> data, int level) {
164 ArrayList<Song> res = new ArrayList<Song>();
166 .filter(element -> element.getLevel() == level)
172 public static ArrayList<Song> getFromJson() {
173 long time = System.currentTimeMillis();
174 SongJSONProperty property = null;
176 property = new ObjectMapper().readValue(new File(DBPATH), SongJSONProperty.class);
177 } catch (IOException e) {
178 // TODO 自動生成された catch ブロック
181 ArrayList<Song> res = new ArrayList<Song>();
182 res.addAll(property.getList());
183 LimitedLog.println(Scraping.class + ":[INFO]: JSON reading compeleted in " + (System.currentTimeMillis() - time) + "ms");
187 public static boolean writeToJson(ArrayList<Song> list) {
189 SongJSONProperty property = new SongJSONProperty();
190 property.setList(list);
191 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
193 writer.writeValue(Paths.get(DBPATH).toFile(), property);
194 } catch (IOException e) {