Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Simple working price checker with cache


    alotoffluffy

    Recommended Posts

    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

    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

    • 3 weeks later...

    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

    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

     

    Artifact example.png

    Link to comment
    Share on other sites

    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

     

    Artifact example.png

    I believe I have gson 2.8.5 included in the build path on eclipse :P i can check real fast

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.