code optimizations

This commit is contained in:
Jeddunk 2019-11-26 14:05:44 +01:00
parent d33c14bab8
commit 3e203f43b3
7 changed files with 180 additions and 182 deletions

View File

@ -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<Void>() {
@Override
protected Void call() {
Map<Integer, String> steamStoreDLCs = new HashMap<>();
Map<Integer, String> 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<Integer, String> allDLCs = new HashMap<>(steamStoreDLCs);
steamDbDLCs.forEach(allDLCs::putIfAbsent);
Map<Number, String> collect = cache.getDlcMap(appId_textfield.getText());
StringBuilder sb = new StringBuilder();
LinkedHashMap<Number, String> 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;

View File

@ -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);

View File

@ -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<App> apps = new ArrayList<>();
public List<App> getApps() {
return apps;
}
public void setApps(List<App> 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)?"<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)?"<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)));
}
}
}
}

View File

@ -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)?"<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)));
}
}

View File

@ -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<App> apps = new ArrayList<>();
public List<App> getApps() {
return apps;
}
public void setApps(List<App> 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)?"<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)));
}
}

View File

@ -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;

View File

@ -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<String> httpResponse =
Unirest.get("https://api.steampowered.com/IStoreService/GetAppList/v1/" +
"?key=E427256C579D3CDF1D504810E8F5B948&include_games=1&max_results=50000").asString();
List<App> apps = new Gson()
.fromJson(httpResponse.getBody(), Download.class)
.getResponse().getApps();
List<App> apps = getApps();
list.setTimestamp(Instant.now());
list.setSteamAppsList(apps);
}
private List<App> getApps() {
HttpResponse<String> 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<Number, String> getDlcMap(String appId) {
Map<Integer, String> steamStoreDLCs = new HashMap<>();
Map<Integer, String> 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<Integer, String> 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));
}
}