alotoffluffy 3 Share Posted December 11, 2018 I was searching for an easy way to get the price of items and found that most of the existing snippets no longer worked. So i have created a solution with caching that is very basic using the open source google gson library feel free to @ me and request more features or expand on it yourself. Example usage: PriceFetcher.getPricedItem(itemID).getOverallAverage(); Source: import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class PriceFetcher { private static final long HALF_HOUR = 1800000; private static final String url = "https://rsbuddy.com/exchange/summary.json"; private static long cacheTime; private static String cache = null; public static PricedItem getPricedItem(int id){ if (shouldUpdate()) fetchJSON(); JsonObject query = new JsonParser().parse(cache).getAsJsonObject().getAsJsonObject(Integer.toString(id)); Gson gson = new Gson(); return gson.fromJson(query, PricedItem.class); } private static boolean shouldUpdate() { if (cache == null) return true; else if (System.currentTimeMillis() - cacheTime > HALF_HOUR) return true; return false; } private static void fetchJSON() { try (BufferedReader rd = new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()))) { StringBuilder totalString = new StringBuilder(); String line; while( (line = rd.readLine()) != null) { totalString.append(line); } cacheTime = System.currentTimeMillis(); cache = totalString.toString(); } catch (IOException e) { e.printStackTrace(); } } public class PricedItem { private int id, sp, buy_average, buy_quantity, sell_average, sell_quantity, overall_average, overall_quantity; private String name; private boolean members; public PricedItem() {} public int getShopPrice() { return sp; } public void setShopPrice(int sp) { this.sp = sp; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getBuyAverage() { return buy_average; } public void setBuyAverage(int buyAverage) { this.buy_average = buyAverage; } public int getBuyQuantity() { return buy_quantity; } public void setBuyQuantity(int buy_quantity) { this.buy_quantity = buy_quantity; } public int getSellAverage() { return sell_average; } public void setSellAverage(int sellAverage) { this.sell_average = sellAverage; } public int getSellQuantity() { return sell_quantity; } public void setSellQuantity(int sellQuantity) { this.sell_quantity = sellQuantity; } public int getOverallAverage() { return overall_average; } public void setOverallAverage(int overallAverage) { this.overall_average = overallAverage; } public int getOverallQuantity() { return overall_quantity; } public void setOverallQuantity(int overallQuantity) { this.overall_quantity = overallQuantity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isMembers() { return members; } public void setMembers(boolean members) { this.members = members; } } } Link to comment Share on other sites More sharing options...
Articron 738 Share Posted December 11, 2018 Thanks for the snippet! This should really not be an object though, considering that it's a utility class. Just some feedback on your design choice Link to comment Share on other sites More sharing options...
alotoffluffy 3 Author Share Posted December 12, 2018 13 hours ago, Articron said: Thanks for the snippet! This should really not be an object though, considering that it's a utility class. Just some feedback on your design choice @Articron thanks for the advice, based upon what you have said and some other feedback I received I have rewritten it to be a static class with a nested object which represents all the values for the queried item. Should you think of any other changes please let me know I am keen to improve and write more snippets for the community. Link to comment Share on other sites More sharing options...
Astrate 1 Share Posted December 28, 2018 Awesome snippet, Except for when I try to use it and try to use i believe it returns null :P? Idk, It's not working for me bud Doesn't return anything String ItemName = PriceFetcher.getPricedItem(RandomID).getName(); log("Item: " + ItemName"); Link to comment Share on other sites More sharing options...
alotoffluffy 3 Author Share Posted December 29, 2018 On 12/28/2018 at 12:07 PM, Astrate said: Awesome snippet, Except for when I try to use it and try to use i believe it returns null :P? Idk, It's not working for me bud Doesn't return anything String ItemName = PriceFetcher.getPricedItem(RandomID).getName(); log("Item: " + ItemName"); Hi, i just wanted to check if you have either added the maven dependencies or the jar file to your project and following that please ensure that the gson library is built into the output jar. I have added an image below that demonstrates what the artifact build should look like. Here is the link to the GSON github : https://github.com/google/gson Link to comment Share on other sites More sharing options...
Astrate 1 Share Posted December 29, 2018 20 minutes ago, alotoffluffy said: Hi, i just wanted to check if you have either added the maven dependencies or the jar file to your project and following that please ensure that the gson library is built into the output jar. I have added an image below that demonstrates what the artifact build should look like. Here is the link to the GSON github : https://github.com/google/gson I believe I have gson 2.8.5 included in the build path on eclipse i can check real fast Link to comment Share on other sites More sharing options...
Astrate 1 Share Posted December 29, 2018 If anything I can try loading this up on Intellij I usually use eclipse because that's where I started out on Link to comment Share on other sites More sharing options...
Astrate 1 Share Posted December 29, 2018 Wow... Hmmm maybe an outdated gson jar might be the problem 2.6.2 .... Link to comment Share on other sites More sharing options...
Astrate 1 Share Posted December 29, 2018 Still same issue and I have no clue of why Link to comment Share on other sites More sharing options...
alotoffluffy 3 Author Share Posted December 29, 2018 Works fine for me, looked up your item and second value is sale price Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.