import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class Price {
private static final String BASE = "https://api.rsbuddy.com/grandExchange?a=guidePrice&i=";
public int getOverallPrice(int itemID) throws IOException {
return this.parse(itemID, "overall");
}
public int getBuyingPrice(int itemID) throws IOException {
return this.parse(itemID, "buying");
}
public int getSellingPrice(int itemID) throws IOException {
return this.parse(itemID, "selling");
}
private int parse(int itemID, String choice) throws IOException {
String line;
URL url = new URL("https://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + itemID);
BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
String price = null;
while ((line = file.readLine()) != null) {
if (!line.contains("{")) continue;
price = line.trim();
}
price = choice.equals("buying") ? price.substring(price.indexOf(",") + 10, this.nthOccurrence(price, ',', 1)).trim() : (choice.equals("selling") ? price.substring(this.nthOccurrence(price, ',', 2) + 11, price.indexOf("sellingQuantity") - 2).trim() : price.substring(price.indexOf(":") + 1, price.indexOf(",")).trim());
file.close();
return Integer.parseInt(price);
}
private int nthOccurrence(String str, char c, int n) {
int pos = str.indexOf(c, 0);
while (n-- > 0 && pos != -1) {
pos = str.indexOf(c, pos + 1);
}
return pos;
}
}
Then you use it like so:
public class Main extends Script{
Price price = Price();
int priceOfItem;
public void onStart(){
priceOfItem = price.getSellingPrice(ITEMID);
}
}