From 3e203f43b31a46817f162b9e31c33138ddfdb3f4 Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Tue, 26 Nov 2019 14:05:44 +0100 Subject: [PATCH] code optimizations --- src/main/java/Controller.java | 56 +---------- src/main/java/Main.java | 2 +- src/main/java/pojo/App.java | 104 +++++++++++++++++++++ src/main/java/pojo/Download.java | 58 ------------ src/main/java/pojo/Rezponze.java | 60 ------------ src/main/java/util/CreamApiConfig.java | 6 +- src/main/java/util/SteamAppsListCache.java | 76 +++++++++++++-- 7 files changed, 180 insertions(+), 182 deletions(-) delete mode 100644 src/main/java/pojo/Download.java delete mode 100644 src/main/java/pojo/Rezponze.java diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index 4cb499e..431de97 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -9,11 +9,6 @@ import javafx.scene.control.Tooltip; import javafx.stage.FileChooser; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.configuration2.ex.ConfigurationException; -import org.jsoup.HttpStatusException; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; import pojo.App; import util.CreamApiConfig; import util.CreamApiDllHandler; @@ -25,11 +20,8 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; public class Controller { @@ -193,54 +185,8 @@ public class Controller { return new Task() { @Override protected Void call() { - Map steamStoreDLCs = new HashMap<>(); - Map steamDbDLCs = new HashMap<>(); - //StringBuilder sb = new StringBuilder(); - try { - // Steam Store - Document steamDoc = Jsoup - .connect("https://store.steampowered.com/app/" + appId_textfield.getText() + "/") - .get(); - Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row"); - for (Element dlc : steamDLCs) { - String dlc_id = dlc.attr("data-ds-appid"); - String dlc_name = dlc - .getElementsByClass("game_area_dlc_name") - .text().replace("\n", "").trim(); - steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name); - } - // SteamDB - Document steamDbDoc = Jsoup - .connect("https://steamdb.info/app/" + appId_textfield.getText() + "/dlc/") - .get(); - Element steamDbDlcSection = steamDbDoc.getElementById("dlc"); - Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app"); - for (Element dlc : steamDbDLCElements) { - String dlc_id = dlc.attr("data-appid"); - String dlc_name = "Unknown DLC " + dlc_id; - Elements td = dlc.getElementsByTag("td"); - if (!td.isEmpty()) { - dlc_name = td.get(1).text().replace("\n", "").trim(); - } - steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); - } - } catch (HttpStatusException e) { - if (e.getStatusCode() == 404) { - System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); - } - } catch (IOException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - // ignore - } - - Map allDLCs = new HashMap<>(steamStoreDLCs); - steamDbDLCs.forEach(allDLCs::putIfAbsent); + Map collect = cache.getDlcMap(appId_textfield.getText()); StringBuilder sb = new StringBuilder(); - LinkedHashMap collect = allDLCs.entrySet().stream() - .sorted(Map.Entry.comparingByKey()) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, - (oldValue, newValue) -> oldValue, LinkedHashMap::new)); collect.forEach((k, v) -> sb.append(k).append("=").append(v).append("\n")); dlc_textarea.setText(sb.toString()); return null; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9499ce1..1c78a9c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -8,7 +8,7 @@ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ - Parent root = FXMLLoader.load(ClassLoader.getSystemResource("mainWindow.fxml"));//getClass().getResource("/mainWindow.fxml") + Parent root = FXMLLoader.load(ClassLoader.getSystemResource("mainWindow.fxml")); primaryStage.setTitle("Auto CreamAPI"); primaryStage.setMinWidth(655 + 25); primaryStage.setMinHeight(400 + 50); diff --git a/src/main/java/pojo/App.java b/src/main/java/pojo/App.java index 3130f12..8ee8e73 100644 --- a/src/main/java/pojo/App.java +++ b/src/main/java/pojo/App.java @@ -3,6 +3,8 @@ package pojo; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; @SuppressWarnings("unused") @@ -107,4 +109,106 @@ public class App { return b1 && b2 && b3 && b4; } + public static class Rezponze { + + @SerializedName("apps") + @Expose + private List apps = new ArrayList<>(); + + public List getApps() { + return apps; + } + + public void setApps(List apps) { + this.apps = apps; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Rezponze.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("apps"); + sb.append('='); + sb.append(((this.apps == null)?"":this.apps)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + + @Override + public int hashCode() { + int result = 1; + result = ((result* 31)+((this.apps == null)? 0 :this.apps.hashCode())); + return result; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof Rezponze)) { + return false; + } + Rezponze rhs = ((Rezponze) other); + return Objects.equals(this.apps, rhs.apps); + //return ((this.apps == rhs.apps)||((this.apps!= null)&&this.apps.equals(rhs.apps))); + } + + public static class Download { + + @SerializedName("response") + @Expose + private Rezponze response; + + public Rezponze getResponse() { + return response; + } + + public void setResponse(Rezponze response) { + this.response = response; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Download.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("response"); + sb.append('='); + sb.append(((this.response == null)?"":this.response)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + + @Override + public int hashCode() { + int result = 1; + result = ((result* 31)+((this.response == null)? 0 :this.response.hashCode())); + return result; + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof Download)) { + return false; + } + Download rhs = ((Download) other); + return Objects.equals(this.response, rhs.response); + //return ((this.response == rhs.response)||((this.response!= null)&&this.response.equals(rhs.response))); + } + + } + } } diff --git a/src/main/java/pojo/Download.java b/src/main/java/pojo/Download.java deleted file mode 100644 index 384ef0d..0000000 --- a/src/main/java/pojo/Download.java +++ /dev/null @@ -1,58 +0,0 @@ -package pojo; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -import java.util.Objects; - -public class Download { - - @SerializedName("response") - @Expose - private Rezponze response; - - public Rezponze getResponse() { - return response; - } - - public void setResponse(Rezponze response) { - this.response = response; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(Download.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); - sb.append("response"); - sb.append('='); - sb.append(((this.response == null)?"":this.response)); - sb.append(','); - if (sb.charAt((sb.length()- 1)) == ',') { - sb.setCharAt((sb.length()- 1), ']'); - } else { - sb.append(']'); - } - return sb.toString(); - } - - @Override - public int hashCode() { - int result = 1; - result = ((result* 31)+((this.response == null)? 0 :this.response.hashCode())); - return result; - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - if (!(other instanceof Download)) { - return false; - } - Download rhs = ((Download) other); - return Objects.equals(this.response, rhs.response); - //return ((this.response == rhs.response)||((this.response!= null)&&this.response.equals(rhs.response))); - } - -} diff --git a/src/main/java/pojo/Rezponze.java b/src/main/java/pojo/Rezponze.java deleted file mode 100644 index d2263dd..0000000 --- a/src/main/java/pojo/Rezponze.java +++ /dev/null @@ -1,60 +0,0 @@ -package pojo; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -public class Rezponze { - - @SerializedName("apps") - @Expose - private List apps = new ArrayList<>(); - - public List getApps() { - return apps; - } - - public void setApps(List apps) { - this.apps = apps; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(Rezponze.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); - sb.append("apps"); - sb.append('='); - sb.append(((this.apps == null)?"":this.apps)); - sb.append(','); - if (sb.charAt((sb.length()- 1)) == ',') { - sb.setCharAt((sb.length()- 1), ']'); - } else { - sb.append(']'); - } - return sb.toString(); - } - - @Override - public int hashCode() { - int result = 1; - result = ((result* 31)+((this.apps == null)? 0 :this.apps.hashCode())); - return result; - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - if (!(other instanceof Rezponze)) { - return false; - } - Rezponze rhs = ((Rezponze) other); - return Objects.equals(this.apps, rhs.apps); - //return ((this.apps == rhs.apps)||((this.apps!= null)&&this.apps.equals(rhs.apps))); - } - -} diff --git a/src/main/java/util/CreamApiConfig.java b/src/main/java/util/CreamApiConfig.java index 57f255e..6100f5d 100644 --- a/src/main/java/util/CreamApiConfig.java +++ b/src/main/java/util/CreamApiConfig.java @@ -155,7 +155,11 @@ public class CreamApiConfig { public void setConfig(String path) throws ConfigurationException, IOException { File file = new File(path); - boolean fileCreated = file.createNewFile(); + if (file.createNewFile()) { + System.out.println("New config file created!"); + } else { + System.out.println("Using existing config file!"); + } this.config = CONFIGS.ini(path); this.config.setCommentLeadingCharsUsedInInput(";"); this.path = path; diff --git a/src/main/java/util/SteamAppsListCache.java b/src/main/java/util/SteamAppsListCache.java index 3a54867..4064820 100644 --- a/src/main/java/util/SteamAppsListCache.java +++ b/src/main/java/util/SteamAppsListCache.java @@ -4,19 +4,27 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import kong.unirest.HttpResponse; import kong.unirest.Unirest; +import org.jsoup.HttpStatusException; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import pojo.App; -import pojo.Download; import pojo.SteamAppsList; import java.io.*; import java.lang.reflect.Type; import java.time.Duration; import java.time.Instant; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class SteamAppsListCache { + private static final String KEY = "E427256C579D3CDF1D504810E8F5B948"; private SteamAppsList list = new SteamAppsList(); private File cacheFile = new File("steamapps.json"); @@ -69,16 +77,20 @@ public class SteamAppsListCache { } private void getListFromApi() { - HttpResponse httpResponse = - Unirest.get("https://api.steampowered.com/IStoreService/GetAppList/v1/" + - "?key=E427256C579D3CDF1D504810E8F5B948&include_games=1&max_results=50000").asString(); - List apps = new Gson() - .fromJson(httpResponse.getBody(), Download.class) - .getResponse().getApps(); + List apps = getApps(); list.setTimestamp(Instant.now()); list.setSteamAppsList(apps); } + private List getApps() { + HttpResponse httpResponse = + Unirest.get("https://api.steampowered.com/IStoreService/GetAppList/v1/" + + "?key=" + KEY + "&include_games=1&max_results=50000").asString(); + return new Gson() + .fromJson(httpResponse.getBody(), App.Rezponze.Download.class) + .getResponse().getApps(); + } + private void saveListToFile() throws IOException { Gson gson = new Gson(); String jsonString = gson.toJson(list); @@ -95,4 +107,54 @@ public class SteamAppsListCache { Gson gson = new Gson(); list = gson.fromJson(json, type); } + + public LinkedHashMap getDlcMap(String appId) { + Map steamStoreDLCs = new HashMap<>(); + Map steamDbDLCs = new HashMap<>(); + //StringBuilder sb = new StringBuilder(); + try { + // Steam Store + Document steamDoc = Jsoup + .connect("https://store.steampowered.com/app/" + appId + "/") + .get(); + Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row"); + for (Element dlc : steamDLCs) { + String dlc_id = dlc.attr("data-ds-appid"); + String dlc_name = dlc + .getElementsByClass("game_area_dlc_name") + .text().replace("\n", "").trim(); + steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name); + } + // SteamDB + Document steamDbDoc = Jsoup + .connect("https://steamdb.info/app/" + appId + "/dlc/") + .get(); + Element steamDbDlcSection = steamDbDoc.getElementById("dlc"); + Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app"); + for (Element dlc : steamDbDLCElements) { + String dlc_id = dlc.attr("data-appid"); + String dlc_name = "Unknown DLC " + dlc_id; + Elements td = dlc.getElementsByTag("td"); + if (!td.isEmpty()) { + dlc_name = td.get(1).text().replace("\n", "").trim(); + } + steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); + } + } catch (HttpStatusException e) { + if (e.getStatusCode() == 404) { + System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + // ignore + } + + Map allDLCs = new HashMap<>(steamStoreDLCs); + steamDbDLCs.forEach(allDLCs::putIfAbsent); + return allDLCs.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, + (oldValue, newValue) -> oldValue, LinkedHashMap::new)); + } }