diff --git a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java index e60de73..857526f 100644 --- a/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java +++ b/src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java @@ -48,23 +48,23 @@ public class APIConnection /** - * Returns a list of the UIDs of all the players friends + * Makes a call to the steam api using the requested url and does + * some error handling where it will re-request data from the steam + * api if it simply throws an internal error. * - * @param steamid - * @return + * @param url address to download data with + * @return string of the data returned */ - public List getFriends(String steamid) + public String querySteamAPI(String url) { - List friendsId = new ArrayList<>(); - - try + boolean downloaded = false; + String apiData = ""; + while(!downloaded) { - String apiData = ""; try { - apiData = WebScraper - .getWebsite(this.baseURL + this.friendListURL + - this.apiKey + "&steamid=" + steamid); + apiData = WebScraper.getWebsite(url); + downloaded = true; } catch (SteamConnectionException e) { @@ -73,39 +73,68 @@ public class APIConnection case RESTRICTED: { //This is fine - System.out.println("Private profile: " + steamid); - return friendsId; + System.out.println("Private profile: "); + System.out.println(url); + return ""; } case CONNECTION: { //spooky 500 error :( - new APIThrottler().wait(120); - - try - { - apiData = WebScraper - .getWebsite(this.baseURL + this.friendListURL + - this.apiKey + "&steamid=" + steamid); - } - catch (SteamConnectionException exception2) - { - throw new Exception("Everything is dead"); - } + //I don't know why but, steam throws 1-3 of these per day + System.out.println("Spooky steam API error"); + new APIThrottler().wait(30); + } + case RATE_LIMITED: + { + //hasn't happened yet + System.out.println("Oof, we are being throttled"); + new APIThrottler().wait(300); + } + case FORBIDDEN: + { + System.out.println("Check your API key."); + System.exit(-1); + } + case BAD_REQUEST: + { + System.out.println("BAD REQUEST:"); + System.out.println(url); + System.out.println("Please modify your query."); + return ""; } } } + } + return apiData; + } - new JSONObject(apiData) - .getJSONObject("friendslist") - .getJSONArray("friends").toList() - .forEach(f-> - friendsId.add(((HashMap)(f)).get("steamid")) - ); + + /** + * Returns a list of the UIDs of all the players friends + * + * @param steamid + * @return + */ + public List getFriends(String steamid) + { + List friendsId = new ArrayList<>(); + + String apiData = this.querySteamAPI(this.baseURL + this.friendListURL + + this.apiKey + "&steamid=" + steamid); + + JSONObject object = new JSONObject(apiData); + + if(object.has("friendslist")) + { + object.getJSONObject("friendslist") + .getJSONArray("friends").toList() + .forEach(f-> + friendsId.add(((HashMap)(f)).get("steamid")) + ); } - catch (Exception ex) + else { - ex.printStackTrace(); - System.exit(-1); + return friendsId; } return friendsId; @@ -139,14 +168,18 @@ public class APIConnection System.out.println(queryUrl); JSONArray names; - try + + String apiResult = this.querySteamAPI(queryUrl); + + JSONObject object = new JSONObject(apiResult); + + if(object.has("response")) { - names = new JSONObject(WebScraper.getWebsite(queryUrl)) - .getJSONObject("response").getJSONArray("players"); + names = object.getJSONObject("response").getJSONArray("players"); } - catch (SteamConnectionException ex) + else { - //meh + //eh return map; } diff --git a/src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java b/src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java index 10fd16d..32745e5 100644 --- a/src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java +++ b/src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java @@ -1,11 +1,32 @@ package net.jrtechs.www.SteamAPI; /** + * Errors thrown by the steam API at times. + * + * @see Steam API Docs + * * @author Jeffery Russell 11-19-18 */ public enum ConnectionErrors { + /** No errors? */ VALID, + + /** 500 connection error with the server */ CONNECTION, - RESTRICTED + + /** The profile is private 401 */ + RESTRICTED, + + /** Invalid Steam api 403 */ + FORBIDDEN, + + /** 429 you are being rate limited */ + RATE_LIMITED, + + /** The steam api threw a 404 not found error */ + NOT_FOUND, + + /** The request being made is in the wrong format */ + BAD_REQUEST } diff --git a/src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java b/src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java index 3def6db..bac6c45 100644 --- a/src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java +++ b/src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java @@ -2,19 +2,37 @@ package net.jrtechs.www.SteamAPI; /** + * Exception to represent the types of HTTP exceptions + * that the steam api throws. + * * @author Jeffery Russell 11-19-18 */ public class SteamConnectionException extends Exception { + /** + * Type of error which the Steam API threw + */ private ConnectionErrors error; + + /** + * Creates a new error with a connection type + * + * @param error + */ public SteamConnectionException(ConnectionErrors error) { this.error = error; } - public ConnectionErrors getError() { + /** + * returns the type of error the scrapper encountered + * + * @return + */ + public ConnectionErrors getError() + { return error; } } diff --git a/src/main/java/net/jrtechs/www/utils/ConfigLoader.java b/src/main/java/net/jrtechs/www/utils/ConfigLoader.java index b6444e4..8cd748f 100644 --- a/src/main/java/net/jrtechs/www/utils/ConfigLoader.java +++ b/src/main/java/net/jrtechs/www/utils/ConfigLoader.java @@ -19,7 +19,7 @@ import java.io.InputStreamReader; public class ConfigLoader { /** Json object which stores configuration contents **/ - JSONObject config; + private JSONObject config; /** diff --git a/src/main/java/net/jrtechs/www/webCrawler/FileReader.java b/src/main/java/net/jrtechs/www/utils/FileReader.java similarity index 94% rename from src/main/java/net/jrtechs/www/webCrawler/FileReader.java rename to src/main/java/net/jrtechs/www/utils/FileReader.java index 6acf1c1..c593d72 100644 --- a/src/main/java/net/jrtechs/www/webCrawler/FileReader.java +++ b/src/main/java/net/jrtechs/www/utils/FileReader.java @@ -1,10 +1,9 @@ -package net.jrtechs.www.webCrawler; +package net.jrtechs.www.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.IOException; -import java.util.ArrayList; import java.util.List; /** diff --git a/src/main/java/net/jrtechs/www/utils/WebScraper.java b/src/main/java/net/jrtechs/www/utils/WebScraper.java index 7a74a5d..c880cb1 100644 --- a/src/main/java/net/jrtechs/www/utils/WebScraper.java +++ b/src/main/java/net/jrtechs/www/utils/WebScraper.java @@ -36,7 +36,14 @@ public class WebScraper case 401: throw new SteamConnectionException(ConnectionErrors.RESTRICTED); case 500: + case 503: throw new SteamConnectionException(ConnectionErrors.CONNECTION); + case 429: + throw new SteamConnectionException(ConnectionErrors.RATE_LIMITED); + case 404: + throw new SteamConnectionException(ConnectionErrors.NOT_FOUND); + case 400: + throw new SteamConnectionException(ConnectionErrors.BAD_REQUEST); default: } @@ -99,7 +106,8 @@ public class WebScraper * @throws IOException -- with br.readLine() */ private static String getBufferedReaderData(BufferedReader br) - throws IOException { + throws IOException + { String html = ""; String line; while ((line = br.readLine()) != null) diff --git a/src/main/java/net/jrtechs/www/webCrawler/FileIO.java b/src/main/java/net/jrtechs/www/webCrawler/FileIO.java index 500e9ba..61fa5eb 100644 --- a/src/main/java/net/jrtechs/www/webCrawler/FileIO.java +++ b/src/main/java/net/jrtechs/www/webCrawler/FileIO.java @@ -1,6 +1,7 @@ package net.jrtechs.www.webCrawler; import net.jrtechs.www.server.Player; +import net.jrtechs.www.utils.FileReader; import org.json.JSONArray; import org.json.JSONObject; @@ -32,6 +33,13 @@ public class FileIO } + /** + * Helper function to piece together the naming convention + * for the JSON file. + * + * @param id player id + * @return path of the file being saved + */ private String getURL(String id) { return baseFilaPath + id + ".json"; @@ -42,8 +50,8 @@ public class FileIO * Determines if we already have the player * on disk. * - * @param id - * @return + * @param id steam id of the player + * @return if the file exists on disk */ public boolean playerExists(String id) { @@ -68,6 +76,15 @@ public class FileIO } + /** + * Reads all the friends from a player on the disk + * + * ** This should only be called if we know the player + * is stored on the disk. + * + * @param id steam id of the player + * @return list of all their friends. + */ public List readFriends(String id) { String fileContents = FileReader.readFile(this.getURL(id));