OSDN Git Service

Squashed commit of the following:
authorhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Sat, 3 Jul 2021 01:30:18 +0000 (10:30 +0900)
committerhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Sat, 3 Jul 2021 01:30:18 +0000 (10:30 +0900)
commit 9b9616efa521c9f0f8b097155fb94c6dd8ff0059
Author: hizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Date:   Sat Jul 03 10:25:20 2021 +0900

スクレイピングメソッド完成

commit 5f6afccb4beb5440a447627b2c9d5ca15d7df9c6
Author: hizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Date:   Fri Jul 02 21:01:51 2021 +0900

メソッド改良

commit a0666d9d65e47a457d7cd84c60747904e23e4587
Author: hizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Date:   Thu Jul 01 23:40:19 2021 +0900

途中まで、Document文字数制限?

src/com/ranfa/lib/Scraping.java
src/com/ranfa/lib/Song.java [new file with mode: 0644]
src/com/ranfa/lib/Version.java
src/com/ranfa/main/DelesteRandomSelector.java

index 9141f4f..b7fdfda 100644 (file)
@@ -1,7 +1,6 @@
 package com.ranfa.lib;
 
 import java.io.IOException;
-import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -13,23 +12,118 @@ import org.jsoup.select.Elements;
 
 public class Scraping {
 
-       private final static String URI = "https://imascg-slstage-wiki.gamerch.com/楽曲詳細一覧";
+       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";
+       private final static String DBPATH = "database.json";
+       public final static String ALL = "全タイプ";
+       public final static String CUTE = "キュート";
+       public final static String COOL = "クール";
+       public final static String PASSION = "パッション";
+       public final static String DEBUT = "DEBUT";
+       public final static String REGULAR = "REGULAR";
+       public final static String PRO = "PRO";
+       public final static String MASTER = "MASTER";
+       public final static String MASTERPLUS = "MASTER+";
+       public final static String LEGACYMASTERPLUS = "ⓁMASTER+";
 
-       public static ArrayList<String> getWholeData() {
-               if(databaseExists())
-                       return null;
-               ArrayList<String> res = new ArrayList<>();
+       public static boolean databaseExists() {
+               Path path = Paths.get(DBPATH);
+               return Files.exists(path);
+       }
+
+       public static synchronized ArrayList<Song> getWholeData() {
+               // if(databaseExists())
+               //      return null;
+               ArrayList<Song> res = new ArrayList<>();
                try {
-                       Document document = Jsoup.parse(new URL(URI), 0);
+
+                       Document document = Jsoup.connect(URI)
+                                       .userAgent("Java/DeresteRandomSelector  More information is available at https://github.com/hizumiaoba/DeresteRandomSelector/")
+                                       .maxBodySize(0)
+                                       .timeout(0)
+                                       .get();
                        Elements rows = document.getElementsByTag("tbody").get(0).select("tr");
+                       for(int i = 0; i < rows.size(); i++) {
+                               String attribute = rows.get(i).select("td").get(0).text();
+                               String name = rows.get(i).select("td").get(1).text();
+                               String difficulty = rows.get(i).select("td").get(2).text();
+                               int level = Integer.parseInt(rows.get(i).select("td").get(3).text());
+                               int notes = 0;
+                               if(rows.get(i).select("td").get(5).text().indexOf(",") == -1) {
+                                       notes = Integer.parseInt(rows.get(i).select("td").get(5).text());
+                               } else {
+                                       String temp = rows.get(i).select("td").get(5).text();
+                                       String first = temp.substring(0, temp.indexOf(","));
+                                       String end = temp.substring(temp.indexOf(",") + 1);
+                                       notes = Integer.parseInt(first + end);
+                               }
+                               Song tmp = new Song(attribute, name, difficulty, level, notes);
+                               res.add(tmp);
+                       }
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return res;
        }
 
-       public static boolean databaseExists() {
-               Path path = Paths.get(URI);
-               return Files.exists(path);
+       public static synchronized ArrayList<Song> getSpecificAttributeSongs(ArrayList<Song> data, String attribute) {
+               if(!attribute.equals(ALL) && !attribute.equals(CUTE) && !attribute.equals(COOL) && !attribute.equals(PASSION))
+                       throw new IllegalArgumentException("Illegal attribute value.");
+               if(data.isEmpty())
+                       throw new IllegalArgumentException("ArrayList must not empty.");
+               ArrayList<Song> res = new ArrayList<Song>();
+               for(int i = 0; i < data.size(); i ++) {
+                       if(data.get(i).getAttribute().equals(attribute))
+                               res.add(data.get(i));
+               }
+               return res;
+       }
+
+       public static synchronized ArrayList<Song> getSpecificDifficultySongs(ArrayList<Song> data, String difficulty) {
+               if(!difficulty.equals(DEBUT) && !difficulty.equals(REGULAR) && !difficulty.equals(PRO) && !difficulty.equals(MASTER) && !difficulty.equals(MASTERPLUS) && !difficulty.equals(LEGACYMASTERPLUS))
+                       throw new IllegalArgumentException("Illegal difficulty value.");
+               if(data.isEmpty())
+                       throw new IllegalArgumentException("ArrayList must not empty.");
+               ArrayList<Song> res = new ArrayList<Song>();
+               for(int i = 0; i < data.size(); i++) {
+                       if(data.get(i).getDifficulty().equals(difficulty))
+                               res.add(data.get(i));
+               }
+               return res;
+       }
+
+       public static synchronized ArrayList<Song> getSpecificLevelSongs(ArrayList<Song> data, int level, boolean isLess, boolean isOnly) {
+               if(level <= 0)
+                       throw new IllegalArgumentException("Level must not negative.");
+               if(data.isEmpty())
+                       throw new IllegalArgumentException("ArrayList must not empty.");
+               if(isOnly)
+                       return getOnlyLevelSongs(data, level);
+               ArrayList<Song> res = new ArrayList<Song>();
+               if(isLess) {
+                       for(int i = 0; i < data.size(); i++) {
+                               if(data.get(i).getLevel() < level)
+                                       res.add(data.get(i));
+                       }
+               } else {
+                       for (int i = 0; i < data.size(); i++) {
+                               if(data.get(i).getLevel() > level)
+                                       res.add(data.get(i));
+                       }
+               }
+               return res;
+       }
+
+       private static synchronized ArrayList<Song> getOnlyLevelSongs(ArrayList<Song> data, int level) {
+               if(level < 0)
+                       throw new IllegalArgumentException("Level must not negative.");
+               if(data.isEmpty())
+                       throw new IllegalArgumentException("ArrayList must not empty");
+               ArrayList<Song> res = new ArrayList<Song>();
+               for(int i = 0; i < data.size(); i++) {
+                       if(data.get(i).getLevel() == level)
+                               res.add(data.get(i));
+               }
+               return res;
+
        }
 }
diff --git a/src/com/ranfa/lib/Song.java b/src/com/ranfa/lib/Song.java
new file mode 100644 (file)
index 0000000..91fffe5
--- /dev/null
@@ -0,0 +1,46 @@
+package com.ranfa.lib;
+
+public class Song {
+
+       private String attribute;
+       private String name;
+       private String difficulty;
+       private int level;
+       private int notes;
+
+       public Song(String attribute, String name, String difficulty, int level, int notes) {
+               this.attribute = attribute;
+               this.name = name;
+               this.difficulty = difficulty;
+               this.level = level;
+               this.notes = notes;
+       }
+
+       public String getAttribute() {
+               return this.attribute;
+       }
+
+       public String getName() {
+               return this.name;
+       }
+
+       public String getDifficulty() {
+               return this.difficulty;
+       }
+
+       public int getLevel() {
+               return this.level;
+       }
+
+       public int getNotes() {
+               return this.notes;
+       }
+
+       public String toString() {
+               return "Attribute: " + getAttribute()
+                               + ", Song Name: " + getName()
+                               + ", Difficulty: " + getDifficulty()
+                               + ", Level :" + String.valueOf(getLevel())
+                               + ", Notes :" + String.valueOf(getNotes());
+       }
+}
index a193d9e..0d8ca81 100644 (file)
@@ -8,5 +8,7 @@ import java.lang.annotation.Target;
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Version {
-       String value();
+       int major();
+       int minor();
+       int patch();
 }
index c87a5e3..4db3c9c 100644 (file)
@@ -2,14 +2,17 @@ package com.ranfa.main;
 
 import java.awt.BorderLayout;
 import java.awt.EventQueue;
+import java.util.ArrayList;
 
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 
+import com.ranfa.lib.Scraping;
+import com.ranfa.lib.Song;
 import com.ranfa.lib.Version;
 
-@Version("v1.0.0")
+@Version(major = 1, minor = 0, patch = 0)
 public class DelesteRandomSelector extends JFrame {
 
        private JPanel contentPane;
@@ -34,7 +37,11 @@ public class DelesteRandomSelector extends JFrame {
         * Create the frame.
         */
        public DelesteRandomSelector() {
-               // System.out.println(getVersion());
+               System.out.println(getVersion());
+               ArrayList<Song> tmp = Scraping.getWholeData();
+               for(int i = 0; i < tmp.size(); i++) {
+                       System.out.println(tmp.get(i).toString());
+               }
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 450, 300);
                contentPane = new JPanel();
@@ -50,8 +57,26 @@ public class DelesteRandomSelector extends JFrame {
         * @return アノテーションで定義されているバージョン
         */
        public static String getVersion() {
+               String value = "v"
+                               + getMajorVersion() + "."
+                               + getMinorVersion() + "."
+                               + getPatchVersion();
+               return value;
+       }
+
+       public static int getMajorVersion() {
+               Version version = (Version) DelesteRandomSelector.class.getAnnotation(Version.class);
+               return version.major();
+       }
+
+       public static int getMinorVersion() {
+               Version version = (Version) DelesteRandomSelector.class.getAnnotation(Version.class);
+               return version.minor();
+       }
+
+       public static int getPatchVersion() {
                Version version = (Version) DelesteRandomSelector.class.getAnnotation(Version.class);
-               return version.value();
+               return version.patch();
        }
 
 }