The Undefeated Posted April 8, 2017 Share Posted April 8, 2017 (edited) I made a snippet which will cache all item prices and ID's out of an array in a list at the start of the script. If the price or ID isn't cached it will automatically add it. Usage: Add this to onStart: cacheItems(items); // Put in your own array as parameter To get an item price: getPrice(itemID); getPrice(item Name); // Will both return the price as an int; To get an item ID: getID(item name); // This doesn't work if the item isn't cached. Code: package API; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.List; import static java.lang.Integer.parseInt; /** * Created by The Undefeated. */ public class FetchItems { String itemName; public int price; public int ID; public static List<FetchItems> itemList = new ArrayList<>(); public FetchItems(String itemName, int ID, int price) { this.itemName = itemName; this.ID = ID; this.price = price; } public static void cacheItems(String[] array) { for (String i : array) { getInfo(i); } } public static int getPrice(String name) { for (int i = 0; i < itemList.size(); i++) { if (itemList.get(i).itemName.equals(name)) { return itemList.get(i).price; } } getInfo(name); return itemList.get((itemList.size() - 1)).price; } public static int getID(String name) { for (int i = 0; i < itemList.size(); i++) { if (itemList.get(i).itemName.equals(name)) { return itemList.get(i).ID; } } getInfo(name); return itemList.get((itemList.size() - 1)).ID; } private static void getInfo(String item) { String fieldID = "GEDBID"; String priceID = "GEPrice"; int price2 = 0; int ID2 = 0; try { final URL url = new URL("http://2007.runescape.wikia.com/wiki/Exchange:" + item.replaceAll(" ", "_")); BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = file.readLine()) != null) { if (line.contains("id=\"" + fieldID + "\"")) { int beginIndex = line.indexOf(fieldID) + (fieldID.length() + 2); ID2 = parseInt(line.substring(beginIndex, line.indexOf("<", beginIndex))); } if((line.contains("id=\"" + priceID + "\""))) { int beginIndex = line.indexOf(priceID) + (priceID.length() + 2); price2 = parseInt((line.substring(beginIndex, line.indexOf("<", beginIndex))).replace(",","")); } } if (price2 != 0 && ID2 != 0) { itemList.add(new FetchItems(item, ID2, price2)); } file.close(); } catch (IOException e) { } } } If you see any improvements I could make, don't hesitate to comment. Edited April 8, 2017 by The Undefeated 1 Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted April 8, 2017 Share Posted April 8, 2017 Check out mine, it uses the Jagex website. I like your caching idea, but as a pricing api i dont think the caching is necessary, that should be on a script by script basis Quote Link to comment Share on other sites More sharing options...
Diclonius Posted April 8, 2017 Share Posted April 8, 2017 Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case. Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add(). With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list. 1 Quote Link to comment Share on other sites More sharing options...
The Undefeated Posted April 8, 2017 Author Share Posted April 8, 2017 1 hour ago, Polymorphism said: Check out mine, it uses the Jagex website. I like your caching idea, but as a pricing api i dont think the caching is necessary, that should be on a script by script basis The prices of the RS Grand Exchange API are not always right. Take for example the price of a bow string (ID:1777) so wouldn't recommend that. 1 hour ago, Diclonius said: Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case. Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add(). With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list. I'll change that anytime soon. Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted April 8, 2017 Share Posted April 8, 2017 3 hours ago, The Undefeated said: The prices of the RS Grand Exchange API are not always right. Take for example the price of a bow string (ID:1777) so wouldn't recommend that. I've never seen one that wasn't right... Quote Link to comment Share on other sites More sharing options...
The Undefeated Posted April 8, 2017 Author Share Posted April 8, 2017 (edited) 12 minutes ago, Polymorphism said: I've never seen one that wasn't right... OSBuddy price is way lower but their website is down 50% of the time. I checked this afternoon, and people in chat can confirm, the API price of bow strings were 281 each. Edited April 8, 2017 by The Undefeated Quote Link to comment Share on other sites More sharing options...
Trees Posted April 9, 2017 Share Posted April 9, 2017 15 hours ago, Diclonius said: Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case. Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add(). With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list. Yes definitely use map for this. Quote Link to comment Share on other sites More sharing options...
Polymorphism Posted April 9, 2017 Share Posted April 9, 2017 15 hours ago, The Undefeated said: the API price of bow strings were 281 each. OSBuddy price doesn't reflect in game prices i believe. Which is why i never use it Quote Link to comment Share on other sites More sharing options...
The Undefeated Posted April 9, 2017 Author Share Posted April 9, 2017 8 minutes ago, Polymorphism said: OSBuddy price doesn't reflect in game prices i believe. Which is why i never use it But as I said, the official GE API fails many times. I used it in one my scripts and it's horrible to use because of the wrong prices sometimes. That's why I think using the wiki is the way to go. Quote Link to comment Share on other sites More sharing options...