Skip to content

Instantly share code, notes, and snippets.

@BigDru
Forked from battleguard/gist:2161363
Last active August 29, 2015 14:11
Show Gist options
  • Save BigDru/1e0ae73105d65f612e1e to your computer and use it in GitHub Desktop.
Save BigDru/1e0ae73105d65f612e1e to your computer and use it in GitHub Desktop.
RS General Exchange Get Prices
/**
* Find item price on the grand exchange
* @param itemName The name of the item
* @return Price of the item
*/
public int getGuidePrice(final String itemName) {
return Integer.parseInt(lookup(itemName)[1]);
}
/**
* Find item price on the grand exchange
* @param itemID The id of the item.
* @return Price of the item
*/
public int getGuidePrice(final int itemID) {
return Integer.parseInt(lookup(itemID)[1]);
}
/**
* Get the ID of itemname (used for injecton bots)
* @param itemName: name of the item
* @return : item id as an integer
*/
public int getID(final String itemName) {
return Integer.parseInt(lookup(itemName)[2]);
}
/**
* Gets the itemname given the itemID
* @param itemID : id of the item
* @return name of the item
*/
public String getName(final int itemID) {
return lookup(itemID)[0];
}
/**
* Looks up grand exchange information and returns a string array with the following contents
* String[0] = item name
* String[1] = item price
* String[2] = item id
* @param itemName: name of the item to grab information about on the grandexchange website
* @return : a string array of grand exchange information on the item id provided
*/
public static String[] lookup(final String itemName) {
try {
final URL url = new URL("http://services.runescape.com/m=itemdb_rs/results.ws?query=" + itemName);
final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String input;
while ((input = br.readLine()) != null) {
if(input.toLowerCase().contains("alt=\"" + itemName.toLowerCase().trim() + "\"")) {
return lookup(Integer.parseInt(input.substring(input.indexOf("id=") + 3, input.lastIndexOf("\" alt"))));
}
}
} catch (final Exception ignored) { }
return null;
}
/**
* Looks up grand exchange information and returns a string array with the following contents
* String[0] = item name
* String[1] = item price
* String[2] = item id
* @param itemID for the item being looked up on the grand exchange
* @return : a string array of grand exchange information on the item id provided
*/
public static String[] lookup(final int itemID) {
try {
String[] info = {"0", "0", "0", "0"};
final URL url = new URL("http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" + itemID);
final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String input;
while ((input = br.readLine()) != null) {
if(input.startsWith("<meta name=\"keywords")) {
info[0] = input.substring(input.lastIndexOf(",") + 1, input.lastIndexOf("\"")).trim();
if(info[0].equals("java")) return null;
}
if(input.contains("Current guide price:")) {
input = br.readLine();
info[1] = formatter(input.substring(4, input.lastIndexOf('<')));
info[2] = ("" + itemID);
return info;
}
}
} catch (final Exception ignored) {}
return null;
}
/**
* Formats a string removing all abbreviations and commas out of it and returning just the raw integer value as a string
* @param num : string number to be formatted. Used for numbers like 3.2m or 20.6k
* @return : a complete number without any abbreviations in it.
*/
public static String formatter(String num) {
try {
return num.replaceAll("\\.","").replaceAll("m", "00000").replaceAll("k", "00").replaceAll(",", "");
} catch (Exception e) {}
return "0";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment