December 25, 20196 yr I'm trying to use Explv's script to pull GE data but I can't figure out how to actually paint the data on screen. I've created the two classes "RSExchange" and "ExchangeItem" and then there's this snippet: Quote final RSExchange rsExchange = new RSExchange(); rsExchange.getExchangeItem("Spade").ifPresent(System.out::println); All I want is the sell average from the JSON response but I don't know how to parse/output that info using paint. Edited December 25, 20196 yr by bumzag
December 27, 20196 yr Are you sure that it's working correctly to begin with? Last time I checked, the http://api.rsbuddy.com/grandExchange?a=guidePrice&i=x link doesn't work anymore.
December 27, 20196 yr I wrote up a quick class to get item prices from the runescape's ge services. For some reason I don't have the spoiler button? Anyways here's the class. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; public final class OsRsExchange { private final HashMap<Integer, Long> cachedItems = new HashMap<>(); private static OsRsExchange instance; private OsRsExchange() {} public static OsRsExchange getInstance() { if (instance == null) instance = new OsRsExchange(); return instance; } public void cacheItemPricesFromArray(int[] itemIdsArray) { for (int value : itemIdsArray) { cacheItemPrice(value); } } public boolean cacheItemPrice(int itemId) { String urlLink = "http://services.runescape.com/m=itemdb_oldschool//viewitem?obj=" + itemId; try { URL url = new URL(urlLink); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder sb = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) sb.append(line); int indexOfPrice = sb.indexOf("Current Guide Price"); String priceString = ""; try { priceString = sb.substring(indexOfPrice, sb.indexOf(">", indexOfPrice)); } catch (StringIndexOutOfBoundsException si) { si.printStackTrace(); System.out.println("Invalid Item Id!"); return false; } priceString = priceString.substring(priceString.indexOf("'") + 1, priceString.lastIndexOf("'")); String[] splitPriceString = priceString.split(","); sb = new StringBuilder(); for (String s : splitPriceString) { sb.append(s); } long priceOfItem = -1; try { priceOfItem = Long.parseLong(sb.toString()); } catch (NumberFormatException nfe) { nfe.printStackTrace(); System.out.println("Incorrect string format to be converted to a long!"); } if (cachedItems.containsKey(itemId)) cachedItems.replace(itemId, priceOfItem); else cachedItems.put(itemId, priceOfItem); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public long getPriceOfItem(int itemId) { if (!cachedItems.containsKey(itemId)) { System.out.println("The cached items does not contain this Item Id!"); return -1; } return cachedItems.get(itemId); } public HashMap<Integer, Long> getCachedItems() { return cachedItems; } public void clearCachedItems() { cachedItems.clear(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); if (cachedItems.size() > 0) cachedItems.forEach((id, price) -> sb.append(String.format("%s :: %s\n", id, price))); return sb.toString(); } } To use //Recommended to use an array in the onStart of your script as retrieving the prices is a pretty taxing process. int[] itemIds = new int[]{556, 892, 811, 1436, 2627, 5641}; OsRsExchange.getInstance().cacheItemPricesFromArray(itemIds); System.out.println(OsRsExchange.getInstance()); //If you want to do individual items OsRsExchange.getInstance().cacheItemPrice(3202); System.out.println(OsRsExchange.getInstance()); //Retrieve price of item by calling getPriceOfItem() long price = OsRsExchange.getInstance().getPriceOfItem(2627); System.out.println(price);
January 16, 20206 yr On 12/27/2019 at 10:18 AM, BravoTaco said: I wrote up a quick class to get item prices from the runescape's ge services. For some reason I don't have the spoiler button? Anyways here's the class. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; public final class OsRsExchange { private final HashMap<Integer, Long> cachedItems = new HashMap<>(); private static OsRsExchange instance; private OsRsExchange() {} public static OsRsExchange getInstance() { if (instance == null) instance = new OsRsExchange(); return instance; } public void cacheItemPricesFromArray(int[] itemIdsArray) { for (int value : itemIdsArray) { cacheItemPrice(value); } } public boolean cacheItemPrice(int itemId) { String urlLink = "http://services.runescape.com/m=itemdb_oldschool//viewitem?obj=" + itemId; try { URL url = new URL(urlLink); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder sb = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) sb.append(line); int indexOfPrice = sb.indexOf("Current Guide Price"); String priceString = ""; try { priceString = sb.substring(indexOfPrice, sb.indexOf(">", indexOfPrice)); } catch (StringIndexOutOfBoundsException si) { si.printStackTrace(); System.out.println("Invalid Item Id!"); return false; } priceString = priceString.substring(priceString.indexOf("'") + 1, priceString.lastIndexOf("'")); String[] splitPriceString = priceString.split(","); sb = new StringBuilder(); for (String s : splitPriceString) { sb.append(s); } long priceOfItem = -1; try { priceOfItem = Long.parseLong(sb.toString()); } catch (NumberFormatException nfe) { nfe.printStackTrace(); System.out.println("Incorrect string format to be converted to a long!"); } if (cachedItems.containsKey(itemId)) cachedItems.replace(itemId, priceOfItem); else cachedItems.put(itemId, priceOfItem); return true; } catch (IOException e) { e.printStackTrace(); } return false; } public long getPriceOfItem(int itemId) { if (!cachedItems.containsKey(itemId)) { System.out.println("The cached items does not contain this Item Id!"); return -1; } return cachedItems.get(itemId); } public HashMap<Integer, Long> getCachedItems() { return cachedItems; } public void clearCachedItems() { cachedItems.clear(); } @Override public String toString() { StringBuilder sb = new StringBuilder(); if (cachedItems.size() > 0) cachedItems.forEach((id, price) -> sb.append(String.format("%s :: %s\n", id, price))); return sb.toString(); } } To use //Recommended to use an array in the onStart of your script as retrieving the prices is a pretty taxing process. int[] itemIds = new int[]{556, 892, 811, 1436, 2627, 5641}; OsRsExchange.getInstance().cacheItemPricesFromArray(itemIds); System.out.println(OsRsExchange.getInstance()); //If you want to do individual items OsRsExchange.getInstance().cacheItemPrice(3202); System.out.println(OsRsExchange.getInstance()); //Retrieve price of item by calling getPriceOfItem() long price = OsRsExchange.getInstance().getPriceOfItem(2627); System.out.println(price); Thanks. nice contribution
Create an account or sign in to comment