Browse Source

Updated some docs and changed the web crawler to only crash if it is in dire need.

pull/8/head
Jeffery Russell 6 years ago
parent
commit
80041892ca
7 changed files with 143 additions and 47 deletions
  1. +72
    -39
      src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java
  2. +22
    -1
      src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java
  3. +19
    -1
      src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java
  4. +1
    -1
      src/main/java/net/jrtechs/www/utils/ConfigLoader.java
  5. +1
    -2
      src/main/java/net/jrtechs/www/utils/FileReader.java
  6. +9
    -1
      src/main/java/net/jrtechs/www/utils/WebScraper.java
  7. +19
    -2
      src/main/java/net/jrtechs/www/webCrawler/FileIO.java

+ 72
- 39
src/main/java/net/jrtechs/www/SteamAPI/APIConnection.java View File

@ -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<String> getFriends(String steamid)
public String querySteamAPI(String url)
{
List<String> 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<String, String>)(f)).get("steamid"))
);
/**
* Returns a list of the UIDs of all the players friends
*
* @param steamid
* @return
*/
public List<String> getFriends(String steamid)
{
List<String> 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<String, String>)(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;
}

+ 22
- 1
src/main/java/net/jrtechs/www/SteamAPI/ConnectionErrors.java View File

@ -1,11 +1,32 @@
package net.jrtechs.www.SteamAPI;
/**
* Errors thrown by the steam API at times.
*
* @see <a href="https://partner.steamgames.com/doc/webapi_overview/responses">Steam API Docs</a>
*
* @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
}

+ 19
- 1
src/main/java/net/jrtechs/www/SteamAPI/SteamConnectionException.java View File

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

+ 1
- 1
src/main/java/net/jrtechs/www/utils/ConfigLoader.java View File

@ -19,7 +19,7 @@ import java.io.InputStreamReader;
public class ConfigLoader
{
/** Json object which stores configuration contents **/
JSONObject config;
private JSONObject config;
/**

src/main/java/net/jrtechs/www/webCrawler/FileReader.java → src/main/java/net/jrtechs/www/utils/FileReader.java View File

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

+ 9
- 1
src/main/java/net/jrtechs/www/utils/WebScraper.java View File

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

+ 19
- 2
src/main/java/net/jrtechs/www/webCrawler/FileIO.java View File

@ -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<String> readFriends(String id)
{
String fileContents = FileReader.readFile(this.getURL(id));

Loading…
Cancel
Save