SXForce Posted May 11, 2013 Share Posted May 11, 2013 (edited) For script writers. I made this for myself. It gets the current price in Zybez for a given item. public class Exchange { private final String BASE_URL = "http://forums.zybez.net/runescape-2007-prices/api/"; public double getPrice(String itemName) { String json = getJSON(BASE_URL + getFormattedStringForUrl(itemName)); return getPriceFromJson(json); } private String getFormattedStringForUrl(String string) { while(string.contains(" ")) { string = string.replace(" ", "+"); } return string; } private double getPriceFromJson(String json) { int startPosition = json.indexOf("average\":\"") + "average\":\"".length(); int endPosition = json.indexOf("\",\"", startPosition); return Double.parseDouble(json.substring(startPosition, endPosition)); } private String getJSON(String url) { try { URL u = new URL(url); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setRequestProperty("Content-length", "0"); c.setUseCaches(false); c.setAllowUserInteraction(false); c.setConnectTimeout(3000); c.setReadTimeout(3000); c.connect(); int status = c.getResponseCode(); switch (status) { case 200: case 201: BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line+"\n"); } br.close(); return sb.toString(); } } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return null; } } Usage Paste this file in your script as INNER CLASS. Like: public class SXSomeScript extends Script { @Override public void onStart() { // Your things here } @Override public int onLoop() { // Your things here } // Copy above class, this is just an example. public class Exchange { ... } } Then use it as follows in your class. Note: Use the onStart() to get the prices, not the onLoop()! Exchange exchange = new Exchange(); exchange.getPrice("dragon chainbody"); // Returns 13735000.0 (a double) Edited May 11, 2013 by SXForce Link to comment Share on other sites More sharing options...
Booch Posted May 11, 2013 Share Posted May 11, 2013 Mind describing what this does? Link to comment Share on other sites More sharing options...
Wizard Posted May 11, 2013 Share Posted May 11, 2013 Good job, will use it. Link to comment Share on other sites More sharing options...
SXForce Posted May 11, 2013 Author Share Posted May 11, 2013 Mind describing what this does? It gets the recent price for an item. For example, if you want the price for an oak log: Exchange exchange = new Exchange(); exchange.getPrice("oak log"); Link to comment Share on other sites More sharing options...
Nicholas Posted May 11, 2013 Share Posted May 11, 2013 sick man thanks Link to comment Share on other sites More sharing options...
Muroth Posted May 11, 2013 Share Posted May 11, 2013 This would be very cool if the developers added this to the client, great job. Link to comment Share on other sites More sharing options...
theearmadyl Posted May 11, 2013 Share Posted May 11, 2013 I believe he means to use it as estimated gp/hr Link to comment Share on other sites More sharing options...
SXForce Posted May 11, 2013 Author Share Posted May 11, 2013 I believe he means to use it as estimated gp/hr Correct. Link to comment Share on other sites More sharing options...
GoldenGates Posted May 11, 2013 Share Posted May 11, 2013 Using this, very helpful. Link to comment Share on other sites More sharing options...