azy2 Posted July 28, 2015 Posted July 28, 2015 As part of my paint it would be lovely to include how much money has been made but in order to do that I need to know the prices of the items. Do you guys know of any APIs to query the grand exchange for price data? Jagex provides this for RS3 http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item= But I can't find one for RS07
Bobrocket Posted July 28, 2015 Posted July 28, 2015 (edited) import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class Exchange { public static HashMap<Integer, Integer> cache = new HashMap<Integer, Integer>(); public static String getData(int itemID) { try { StringBuilder sb = new StringBuilder("https://api.rsbuddy.com/grandExchange?a=guidePrice&i="); sb.append(String.valueOf(itemID)); InputStream inputStream = new URL(sb.toString()).openStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { if (line.contains("{")) { sb = new StringBuilder(line); //Remove { and } sb.deleteCharAt(0); //sb.deleteCharAt((line.length() - 1)); return sb.toString(); } } } catch (Exception e) { return e.getMessage(); } return null; } public static String[] parseData(String data) { ArrayList<String> holder = new ArrayList<String>(); String[] parts = data.split(","); //Now we have strings in format "x":y for (String s : parts) { s = s.replace("\"", ""); //Remove " - now in format x:y holder.add(s.split(":")[1]); //Extract y from format x:y } String[] ret = new String[holder.size()]; return holder.toArray(ret); } public static int getPrice(int itemID) { if (itemID == 995) return 1; if (cache.containsKey(itemID)) { return cache.get(itemID); } String[] data = parseData(getData(itemID)); int price = Integer.valueOf(data[0]); cache.put(itemID, price); return price; } } EDIT: Coins are ID 995; 1gp=1gp so we return 1 there. Usage: int price = Exchange.getPrice(item.getId()); Edited July 28, 2015 by Bobrocket 2
Mysteryy Posted July 28, 2015 Posted July 28, 2015 import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class Exchange { public static HashMap<Integer, Integer> cache = new HashMap<Integer, Integer>(); public static String getData(int itemID) { try { StringBuilder sb = new StringBuilder("https://api.rsbuddy.com/grandExchange?a=guidePrice&i="); sb.append(String.valueOf(itemID)); InputStream inputStream = new URL(sb.toString()).openStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { if (line.contains("{")) { sb = new StringBuilder(line); //Remove { and } sb.deleteCharAt(0); //sb.deleteCharAt((line.length() - 1)); return sb.toString(); } } } catch (Exception e) { return e.getMessage(); } return null; } public static String[] parseData(String data) { ArrayList<String> holder = new ArrayList<String>(); String[] parts = data.split(","); //Now we have strings in format "x":y for (String s : parts) { s = s.replace("\"", ""); //Remove " - now in format x:y holder.add(s.split(":")[1]); //Extract y from format x:y } String[] ret = new String[holder.size()]; return holder.toArray(ret); } public static int getPrice(int itemID) { if (itemID == 995) return 1; if (cache.containsKey(itemID)) { return cache.get(itemID); } String[] data = parseData(getData(itemID)); int price = Integer.valueOf(data[0]); cache.put(itemID, price); return price; } } EDIT: Coins are ID 995; 1gp=1gp so we return 1 there. Usage: int price = Exchange.getPrice(item.getId()); Why don't I see a single .close() in this snippet?
azy2 Posted July 28, 2015 Author Posted July 28, 2015 (edited) Wow, you could have just replied with https://api.rsbuddy.com/grandExchange?a=guidePrice&i= that's all I really needed but thanks so much for the work you put into that reply super helpful Edited July 28, 2015 by azy2
Bobrocket Posted July 28, 2015 Posted July 28, 2015 Why don't I see a single .close() in this snippet? To make you cry :^)
Mysteryy Posted July 29, 2015 Posted July 29, 2015 To make you cry :^) Well I hope you fixed it on your code. Not closing streams is a terrible habit and can result in unexpected behavior, especially since the stream isn't being flushed.
Bobrocket Posted July 29, 2015 Posted July 29, 2015 Well I hope you fixed it on your code. Not closing streams is a terrible habit and can result in unexpected behavior, especially since the stream isn't being flushed. Don't worry about me babe