Banker 175 Share Posted October 28, 2018 HI, Since the RSBuddy pricing class that was public broke and I've had to create a new one for BankSeller, I thought I'd share the code so everyone can use Grand Exchange pricing within scripts etc again. Graphs.java import java.io.IOException; public class Graphs extends Market { public Graphs() { super("http://services.runescape.com/m=itemdb_oldschool/api/graph/"); } @Override protected int fetch(int itemID) throws IOException { String data = read(uri + itemID + ".json"); return Integer.parseInt(data.substring(data.lastIndexOf(':') + 1, data.length() - 2)); } } Market.java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; abstract class Market { private final static ExecutorService executorService = Executors.newSingleThreadExecutor(); private boolean isCache, isRefresh; private long duration, lastRefresh = System.currentTimeMillis(); Map<Integer, Integer> cache; String uri; Market(String uri) { this(uri, false, false, 0L); } Market(String uri, boolean isCache) { this(uri, isCache, false, 0L); } Market(String uri, boolean isCache, boolean isRefresh, long duration) { if (isCache) cache = new HashMap<>(); this.isRefresh = isRefresh; this.duration = duration; this.isCache = isCache; this.uri = uri; } public Future<?> getAsync(int itemID, MarketInterface marketInterface) { return executorService.submit(() -> { try { marketInterface.notifyMarket(get(itemID)); } catch (IOException e) { marketInterface.error(e); } }); } public int get(int itemID) throws IOException { if (isCache) { long current = System.currentTimeMillis(); if (isRefresh && current - lastRefresh >= duration) { lastRefresh = current; cache.clear(); } if (cache.containsKey(itemID)) { return cache.get(itemID); } else { int value = fetch(itemID); cache.put(itemID, value); return value; } } else { return fetch(itemID); } } protected abstract int fetch(int itemID) throws IOException; static String read(String uri) throws IOException { return readStream(new URL(uri).openStream()); } private static String readStream(InputStream stream) throws IOException { try (ByteArrayOutputStream result = new ByteArrayOutputStream()) { byte[] buffer = new byte[1024]; int length; while ((length = stream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } } } MarketInterface.java import java.io.IOException; public interface MarketInterface { void notifyMarket(int value); void error(IOException e); } InterpreterException.java public class InterpreterException extends RuntimeException { private String input; private int index; InterpreterException(String input, int index, String message) { super(message); this.input = input; this.index = index; } public String getInput() { return input; } public int getIndex() { return index; } } RSBuddy.java import java.io.IOException; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RSBuddy extends Market { public RSBuddy() throws IOException { super("http://api.rsbuddy.com/grandExchange?a=guidePrice&i="); if (cache == null) cache = new HashMap<>(); String dump = read("https://rsbuddy.com/exchange/summary.json"); dump = dump.substring(1, dump.length() - 1); Pattern pattern = Pattern.compile("(?:.*?,){10}"); Matcher matcher = pattern.matcher(dump); while (matcher.find()) { String result = matcher.group(); String[] temp = result.split(":"); cache.put(Integer.parseInt(temp[0].substring(1, temp[0].length() - 1)), Integer.parseInt(temp[10].substring(0, temp[10].indexOf(',')))); } } @Override protected int fetch(int itemID) { return cache.getOrDefault(itemID,-1); } } Runescape.java import java.io.IOException; public class Runescape extends Market { public Runescape() { super("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item="); } @Override protected int fetch(int itemID) throws IOException { String data = read(uri + itemID); int firstIndex = data.indexOf("price") + 8; return interpret(data.substring(firstIndex, data.indexOf("\"", firstIndex)).trim().replace(",", "")); } private int interpret(String input) throws InterpreterException { int length = input.length() - 1; boolean isNegative = input.charAt(0) == '-'; for (int i = isNegative ? 1 : 0; i < length; i++) { char current = input.charAt(i); if (!Character.isDigit(current) && current != '.') { throw new InterpreterException(input, i, "Invalid symbol at position `" + i + "` in `" + input + "`"); } } double value = Double.parseDouble(input.substring(0, input.length() - (Character.isDigit(input.charAt(input.length() - 1)) ? 0 : 1))); if (input.substring(isNegative ? 1 : 0).matches("\\d+")) return (int) value; char last = input.charAt(length); switch (last) { case 'k': return (int) (value * (isNegative ? -1000 : 1000)); case 'm': return (int) (value * (isNegative ? -1000000 : 1000000)); case 'b': return (int) (value * (isNegative ? -1000000000 : 1000000000)); default: return (int) value; } } } Download ZIP - Banker Link to comment Share on other sites More sharing options...
dQw4w9WgXcQ 184 Share Posted October 28, 2018 pretty gud dude did u actually write this ursefl? Link to comment Share on other sites More sharing options...
Banker 175 Author Share Posted October 28, 2018 34 minutes ago, dQw4w9WgXcQ said: pretty gud dude did u actually write this ursefl? yas Link to comment Share on other sites More sharing options...
Eclipseop 194 Share Posted October 28, 2018 thnk abnkers 4 the lovelers cod one again!!!i tnk u ly impvewd ovr tme nd itc neyce to c!!! God Bless you and your family. Sincerely, Eclipseop Link to comment Share on other sites More sharing options...
Dogerina 330 Share Posted October 29, 2018 10 hours ago, Eclipseop said: thnk abnkers 4 the lovelers cod one again!!!i tnk u ly impvewd ovr tme nd itc neyce to c!!! God Bless you and your family. Sincerely, Eclipseop good shit bro what u on g hit banker up with that shit Link to comment Share on other sites More sharing options...
Articron 738 Share Posted January 6, 2019 On 10/28/2018 at 11:50 AM, Banker said: yas NOPE Link to comment Share on other sites More sharing options...
OSRS42 5 Share Posted January 7, 2019 nice Link to comment Share on other sites More sharing options...
Banker 175 Author Share Posted January 10, 2019 On 1/6/2019 at 9:10 PM, Articron said: NOPE heheheheheheh Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.