OSDN Git Service

f0b650a757eee875fc986b48e368280c6466638c
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / Scraping.java
1 package com.ranfa.lib;
2
3 import java.io.File;
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
10 import javax.swing.JOptionPane;
11
12 import org.jsoup.Jsoup;
13 import org.jsoup.nodes.Document;
14 import org.jsoup.select.Elements;
15
16 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import com.fasterxml.jackson.databind.ObjectWriter;
19
20 public class Scraping {
21
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";
40
41         public static boolean databaseExists() {
42                 Path path = Paths.get(DBPATH);
43                 return Files.exists(path);
44         }
45
46         public static String getDBPath() {
47                 return DBPATH;
48         }
49
50         public static ArrayList<Song> getWholeData() {
51                 long time = System.currentTimeMillis();
52                 // if(databaseExists())
53                 //      return null;
54                 ArrayList<Song> res = new ArrayList<>();
55                 try {
56
57                         Document document = Jsoup.connect(URI)
58                                         .userAgent("Java/DeresteRandomSelector  More information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
59                                         .maxBodySize(0)
60                                         .timeout(0)
61                                         .get();
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());
68                                 int notes = 0;
69                                 if(rows.get(i).select("td").get(5).text().indexOf(",") == -1) {
70                                         notes = Integer.parseInt(rows.get(i).select("td").get(5).text());
71                                 } else {
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);
76                                 }
77                                 Song tmp = new Song();
78                                 tmp.setAttribute(attribute);
79                                 tmp.setName(name);
80                                 tmp.setDifficulty(difficulty);
81                                 tmp.setLevel(level);
82                                 tmp.setNotes(notes);
83                                 res.add(tmp);
84                         }
85                 } catch (IOException e) {
86                         e.printStackTrace();
87                 }
88                 LimitedLog.println(Scraping.class + ":[INFO]: scraping compeleted in " + (System.currentTimeMillis() - time)+ "ms");
89                 return res;
90         }
91
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);
99                 if(data.isEmpty()) {
100                         JOptionPane.showMessageDialog(null, "指定された属性の曲は存在しません。\n条件を変えてみてください");
101                         throw new IllegalArgumentException("ArrayList must not empty.");
102                 }
103                 ArrayList<Song> res = new ArrayList<Song>();
104                 if(attribute.equals(NONSELECTED)) {
105                         res = data;
106                 } else {
107                         data.stream()
108                         .filter(element -> element.getAttribute().equals(attribute))
109                         .forEach(res::add);
110                 }
111                 return res;
112         }
113
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.");
128                 if(data.isEmpty())
129                         throw new IllegalArgumentException("ArrayList must not empty.");
130                 ArrayList<Song> res = new ArrayList<Song>();
131                 if(difficulty.equals(NONSELECTED)) {
132                         res = data;
133                 } else {
134                         data.stream()
135                         .filter(element -> element.getDifficulty().equals(difficulty))
136                         .forEach(res::add);
137                 }
138                 return res;
139         }
140
141         public static  ArrayList<Song> getSpecificLevelSongs(ArrayList<Song> data, int level, boolean isLess, boolean isMore) {
142                 if(level <= 0)
143                         throw new IllegalArgumentException("Level must not negative.");
144                 if(data.isEmpty())
145                         throw new IllegalArgumentException("ArrayList must not empty.");
146                 if(!(isLess || isMore))
147                         throw new IllegalArgumentException("Illegal boolean value.");
148                 if(isLess && isMore)
149                         return getOnlyLevelSongs(data, level);
150                 ArrayList<Song> res = new ArrayList<Song>();
151                 if(isLess) {
152                         data.stream()
153                         .filter(element -> element.getLevel() < level)
154                         .forEach(res::add);
155                 } else if (isMore) {
156                         data.stream()
157                         .filter(element -> element.getLevel() > level)
158                         .forEach(res::add);
159                 }
160                 return res;
161         }
162
163         private static ArrayList<Song> getOnlyLevelSongs(ArrayList<Song> data, int level) {
164                 ArrayList<Song> res = new ArrayList<Song>();
165                 data.stream()
166                 .filter(element -> element.getLevel() == level)
167                 .forEach(res::add);
168                 return res;
169
170         }
171
172         public static ArrayList<Song> getFromJson() {
173                 long time = System.currentTimeMillis();
174                 SongJSONProperty property = null;
175                 try {
176                         property = new ObjectMapper().readValue(new File(DBPATH), SongJSONProperty.class);
177                 } catch (IOException e) {
178                         // TODO 自動生成された catch ブロック
179                         e.printStackTrace();
180                 }
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");
184                 return res;
185         }
186
187         public static boolean writeToJson(ArrayList<Song> list) {
188                 boolean res = true;
189                 SongJSONProperty property = new SongJSONProperty();
190                 property.setList(list);
191                 ObjectWriter writer = new ObjectMapper().writer(new DefaultPrettyPrinter());
192                 try {
193                         writer.writeValue(Paths.get(DBPATH).toFile(), property);
194                 } catch (IOException e) {
195                         res = false;
196                 }
197                 return res;
198         }
199 }