diff --git a/PSIA.iml b/PSIA.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/PSIA.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..cdabff4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + net.nexiesy + PSIA + 0.0.1 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + org.projectlombok + lombok + 1.16.18 + + + org.jsoup + jsoup + 1.9.2 + + + org.apache.httpcomponents + httpclient + 4.5.2 + + + + + \ No newline at end of file diff --git a/src/main/java/net/nexiesy/PSIA.java b/src/main/java/net/nexiesy/PSIA.java new file mode 100644 index 0000000..18fd3b2 --- /dev/null +++ b/src/main/java/net/nexiesy/PSIA.java @@ -0,0 +1,67 @@ +package net.nexiesy; + + +import lombok.Getter; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.jsoup.Jsoup; +import org.jsoup.select.Elements; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.List; + +public class PSIA { + + @Getter + public PSIAServer server; + public PSIA(String url){ + HttpClient httpClient = HttpClientBuilder.create().build(); + HttpGet getRequest = new HttpGet(url); + HttpResponse httpResponse = null; + try { + httpResponse = httpClient.execute(getRequest); + } catch (IOException e) { + e.printStackTrace(); + } + String content = null; + try { + assert httpResponse != null; + BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) { + result.append(line); + } + content = result.toString(); + } catch (UnsupportedOperationException | IOException e) { + e.printStackTrace(); + } + + String information = getInformationHtml(content); + List players = Arrays.asList(information.split("Online Players")[1].split("FIND")[0].split(", ")); + server = format(information.split("About")[3].split("Registered")[0], information.split("Statistics")[2].split("Share")[0], players); + } + + public PSIAServer format(String about, String stats, List players){ + String address = about.split("Address")[1].split("Status")[0].split("Hostname")[0].replace(" ", ""); + String hostname = about.split("Hostname")[1].split("Status")[0].replace(" ", ""); + String status = about.split("Status")[1].split("Players")[0].replace(" ", ""); + String oplayers = about.replace("Hostname"+hostname, "").split("Players")[1].split("Location")[0].replace(" ", ""); + String location = about.split("Location")[1].split("Version")[0].replace(" ", ""); + String version = about.split("Version")[1].split("Website")[0].replace(" ", ""); + String website = about.split("Website")[1].replace(" ", ""); + + String[] datas = stats.split(" "); + return new PSIAServer(address, hostname, status, oplayers, location, version, website, datas[2], Integer.parseInt(datas[4]), Integer.parseInt(datas[6]), Integer.parseInt(datas[8]), Integer.parseInt(datas[10]), Integer.parseInt(datas[12]), players); + } + + private String getInformationHtml(String content){ + Elements information = Jsoup.parse(content).getAllElements(); + return information.get(1).text(); + } +} diff --git a/src/main/java/net/nexiesy/PSIAServer.java b/src/main/java/net/nexiesy/PSIAServer.java new file mode 100644 index 0000000..2eb6477 --- /dev/null +++ b/src/main/java/net/nexiesy/PSIAServer.java @@ -0,0 +1,38 @@ +package net.nexiesy; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.List; + +@AllArgsConstructor +public class PSIAServer { + @Getter + private String address; + @Getter + private String hostname; + @Getter + private String status; + @Getter + private String players; + @Getter + private String location; + @Getter + private String version; + @Getter + private String website; + @Getter + private String uptime; + @Getter + private int vote; + @Getter + private int rank; + @Getter + private int score; + @Getter + private int favorited; + @Getter + private int discussion; + @Getter + private List allPlayers; +}