Jump to content

RSBuddy/OSBuddy Exchange Price Lookup


Recommended Posts

Posted (edited)

RSBuddy/OSBuddy Exchange has more accurate prices than Zybez for the most part.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class PriceLookup {

	private static URLConnection con;
	private static InputStream is;
	private static InputStreamReader isr;
	private static BufferedReader br;

	private static String[] getData(int itemID) {
		try {
			URL url = new URL(
					"https://api.rsbuddy.com/grandExchange?a=guidePrice&i="
							+ itemID);
			con = url.openConnection();
			is = con.getInputStream();
			isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			String line = br.readLine();
			if (line != null) {
				return line.split(",");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null) {
					br.close();
				} else if (isr != null) {
					isr.close();
				} else if (is != null) {
					is.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static int getPrice(int itemID) {
		String[] data = getData(itemID);
		if (data != null && data.length == 3) {
			return Integer.parseInt(data[0].replaceAll("\\D", ""));
		}
		return 0;
	}

	public static int getAverageBuyOffer(int itemID) {
		String[] data = getData(itemID);
		if (data != null && data.length == 3) {
			return Integer.parseInt(data[1].replaceAll("\\D", ""));
		}
		return 0;
	}

	public static int getAverageSellOffer(int itemID) {
		String[] data = getData(itemID);
		if (data != null && data.length == 3) {
			return Integer.parseInt(data[2].replaceAll("\\D", ""));
		}
		return 0;
	}
} 

Dump of all the prices from March 27, 2015: http://pastebin.com/rQ5dk7mp

Edited by VladBots
  • Like 1
Posted (edited)

Oh...Im in my second semester of taking AP Comp Sci. Right now were working on inheritance biggrin.png

 

Are you using Big java for your book?

 

If you are, all the test are based off of a teacher copy of the book. I probably still have mine if you'd want to see it wink.png

Edited by twin 763
  • 4 weeks later...
Posted (edited)

The getPrice(), getAverageBuyOffer(), and getAverageSellOffer() methods are incorrect. String[] data will always be length 5, not length 3, so those methods will always return 0. For a simple fix, you may be able to change it to data.length==5 or just delete that parameter overall.

 

Also, it's fairly simple to split each index of the array by the ":" because the second half will be the price you want and the first half will be the description. For example, if you go to the link for iron ore (https://api.rsbuddy.com/grandExchange?a=guidePrice&i=440), you can see that the stream is:

 

{"overall":54,"buying":54,"buyingQuantity":125855,"selling":53,"sellingQuantity":103603}

 

Split up in the getData() method, It looks like this

 

String[] data = {

   ""overall":54",

   ""buying":54",

   ""buyingQuantity":125855",

   ""selling":53",

   ""sellingQuantity":103603"

}

 

If you split by colon, data[0].split(":") returns the array {""overall"", 54}. then, returning the parse of index 1 gives you 54.

 

Furthermore, the getter methods would return the wrong values, because data[2] is actually the buying quantity, not the selling price. It is important to change the return statement of getAverageSellOffer() to use data[3].

 

Here are the changes I made:

public static int getPrice(int itemID) {
	String[] data = getData(itemID);
	if (data != null && data.length == 5) {
		return Integer.parseInt(data[0].split(":")[1]);
	}
	return 0;
}

public static int getAverageBuyOffer(int itemID) {
	String[] data = getData(itemID);
	if (data != null && data.length == 5) {
		return Integer.parseInt(data[1].split(":")[1]);
	}
	return 0;
}

public static int getAverageSellOffer(int itemID) {
	String[] data = getData(itemID);
	if (data != null && data.length == 5) {
		return Integer.parseInt(data[3].split(":")[1]);
	}
	return 0;
}
Edited by bthebrave

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...