Untitled diff

Created Diff never expires
5 removals
145 lines
9 additions
149 lines
package BotFarmServer;
package BotFarmServer;


import java.io.IOException;
import java.io.IOException;
import java.net.URL;
import java.net.URL;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map;
import java.util.Scanner;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.Pattern;


/**
/**
* Utility class for access to the Grand Exchange API.
* Utility class for access to the Grand Exchange API.
*/
*/
public class GrandExchangeApi {
public class GrandExchangeApi {
private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
"/api/catalogue/detail.json?item=%d";
"/api/catalogue/detail.json?item=%d";
private static final long TEN_MINUTES = 600000;
private static final long TEN_MINUTES = 600000;
private final Map<Integer, GELookupResult> cache;
private final Map<Integer, GELookupResult> cache;
private long startTime;
private long startTime;


/**
/**
* Caching-enabled default constructor
* Caching-enabled default constructor
*/
*/
public GrandExchangeApi() {
public GrandExchangeApi() {
this(true);
this(true);
}
}


/**
/**
* Creates a new Grand Exchange API instance. Starts cache-refresh timer.
* Creates a new Grand Exchange API instance. Starts cache-refresh timer.
* @[member='param'] cache Whether to enable caching of results.
* @[member='param'] cache Whether to enable caching of results.
*/
*/
public GrandExchangeApi(boolean cache) {
public GrandExchangeApi(boolean cache) {
startTime = System.currentTimeMillis();
startTime = System.currentTimeMillis();
this.cache = cache ? new HashMap<>() : null;
this.cache = cache ? new HashMap<>() : null;
}
}


/**
/**
* If caching is enabled, clears the cache so that new results are fetched on lookup.
* If caching is enabled, clears the cache so that new results are fetched on lookup.
*/
*/
public void flushCache() {
public void flushCache() {
long currentTime = System.currentTimeMillis();
if(cache != null) {
if(cache != null) {
cache.clear();
for (Map.Entry<int,GELookupResult>> entry : cache) {
if (currentTime - TEN_MINUTES) > entry.getValue().lastUpdatedAt) {
cache.remove(entry.getKey()); // item is old. it needs to be refreshed
}
}
}
}
}
}


/**
/**
* Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result.
* Looks up an item using the Grand Exchange API. This method blocks while waiting for the API result.
* @[member='param'] itemId the id to look up.
* @[member='param'] itemId the id to look up.
* @return the result returned by the api. May be null if an error has occurred.
* @return the result returned by the api. May be null if an error has occurred.
*/
*/
public GELookupResult lookup(int itemId) {
public GELookupResult lookup(int itemId) {
if((System.currentTimeMillis() - TEN_MINUTES) > startTime){ //Flush cache after 10 minutes. Auto-update prices.
flushCache();
flushCache();
startTime = System.currentTimeMillis();
}


if(cache != null && !cache.isEmpty()) {
if(cache != null && !cache.isEmpty()) {
GELookupResult result = cache.get(itemId);
GELookupResult result = cache.get(itemId);
if(result != null) {
if(result != null) {
return result;
return result;
}
}
}
}


String json;
String json;
try {
try {
URL url = new URL(String.format(API_LOCATION, itemId));
URL url = new URL(String.format(API_LOCATION, itemId));
Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
json = scan.next();
json = scan.next();
scan.close();
scan.close();
} catch(IOException e) {
} catch(IOException e) {
return null;
return null;
}
}


GELookupResult result = parse(itemId, json);
GELookupResult result = parse(itemId, json);


if(cache != null) {
if(cache != null) {
cache.put(itemId, result);
cache.put(itemId, result);
}
}


return result;
return result;
}
}


/**
/**
* Parses a GELookupResult from the JSON returned by the API.
* Parses a GELookupResult from the JSON returned by the API.
* @[member='param'] itemId The item ID.
* @[member='param'] itemId The item ID.
* @[member='param'] json The JSON returned by the RuneScape's API.
* @[member='param'] json The JSON returned by the RuneScape's API.
* @return The serialized result.
* @return The serialized result.
*/
*/
private static GELookupResult parse(int itemId, String json) {
private static GELookupResult parse(int itemId, String json) {
Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\"");
Matcher m = pattern.matcher(json);
Matcher m = pattern.matcher(json);
Map<String, String> results = new HashMap<>();
Map<String, String> results = new HashMap<>();
while(m.find()) {
while(m.find()) {
results.put(m.group("key"), m.group("value"));
results.put(m.group("key"), m.group("value"));
}
}


int price = 0;
int price = 0;
Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json);
Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json);
if (priceMatcher.find()) {
if (priceMatcher.find()) {
price = Integer.parseInt(priceMatcher.group("price"));
price = Integer.parseInt(priceMatcher.group("price"));
}
}


return new GELookupResult(
return new GELookupResult(
results.get("icon"),
results.get("icon"),
results.get("icon_large"),
results.get("icon_large"),
results.get("type"),
results.get("type"),
results.get("typeIcon"),
results.get("typeIcon"),
results.get("name"),
results.get("name"),
results.get("description"),
results.get("description"),
Boolean.parseBoolean(results.get("members")),
Boolean.parseBoolean(results.get("members")),
itemId,
itemId,
price
price
);
);
}
}


/**
/**
* A class representing a result from an API lookup.
* A class representing a result from an API lookup.
*/
*/
public static final class GELookupResult {
public static final class GELookupResult {
public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
public final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
public final boolean isMembers;
public final boolean isMembers;
public final int id, price;
public final int id, price;
public final long lastUpdatedAt;


private GELookupResult(String smallIconUrl,
private GELookupResult(String smallIconUrl,
String largeIconUrl,
String largeIconUrl,
String type,
String type,
String typeIcon,
String typeIcon,
String name,
String name,
String itemDescription,
String itemDescription,
boolean isMembers,
boolean isMembers,
int id,
int id,
int price) {
int price) {


this.smallIconUrl = smallIconUrl;
this.smallIconUrl = smallIconUrl;
this.largeIconUrl = largeIconUrl;
this.largeIconUrl = largeIconUrl;
this.type = type;
this.type = type;
this.typeIcon = typeIcon;
this.typeIcon = typeIcon;
this.name = name;
this.name = name;
this.itemDescription = itemDescription;
this.itemDescription = itemDescription;
this.isMembers = isMembers;
this.isMembers = isMembers;
this.id = id;
this.id = id;
this.price = price;
this.price = price;
this.lastUpdatedAt = System.currentTimeMillis();
}
}
}
}
}
}