Jump to content

GE Data


Recommended Posts

Posted (edited)

Seems like other snippets of getting item data didn't work so I just made one myself. Who knows how long this might last.

If you're interested in storing values you wish to lookup instead of having to parse the json data stored, please implement it yourself (hashmap). I left it out on purpose.

How it works:

Spoiler

1. Splits each JSON item into a 'block' (array of strings)

2. Gets each property from the block (property:value)

3. returns the value if it matches the property + name of item

Usage:

Spoiler

All look-ups return an Optional<String>. Be sure to convert to numeric if that's what you're after.


ItemLookup.get("Trout", Property.SELL_AVERAGE); // case sensitive

 

Code:

Spoiler

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

public class ItemLookup {

    private static String json;

    private ItemLookup(){ }

    private static void initData(){
        try {
            URL url = new URL("https://rsbuddy.com/exchange/summary.json");
            URLConnection connect = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            json = in.readLine();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Optional<String> get(String itemName, Property property){

        if(json == null)
        {
            initData();
        }

        Optional<String> itemBlock =  Arrays
                .stream(json.split("\\{"))
                .filter(phrase -> findString(itemName, phrase))
                .findFirst();

        if(itemBlock.isPresent())
        {
            return Arrays
                    .stream(itemBlock.get().split(","))
                    .filter(phrase -> findString(property.getProperty(), phrase))
                    .map(ItemLookup::clean)
                    .findFirst();
        }
        else
        {
            return Optional.empty();
        }
    }

    private static boolean findString(String target, String phrase){
        return phrase.matches(".*\"" + Pattern.quote(target) + "\".*");
    }

    private static String clean(String value){
        value = value.split(":")[1];
        return value.replaceAll("}","").replaceAll("\"","");
    }
}

 

Spoiler

public enum Property {

    ID("id"),
    NAME("name"),
    MEMBERS("members"),
    SP("sp"),
    BUY_AVERAGE("buy_average"),
    BUY_QUANTITY("buy_quantity"),
    SELL_AVERAGE("sell_average"),
    SELL_QUANTITY("sell_quantity"),
    OVERALL_AVERAGE("overall_average"),
    OVERALL_QUANTITY("overall_quantity");

    private String property;

    Property(String property){
        this.property = property;
    }

    public String getProperty() {
        return property;
    }
}

 

 

Edited by dreameo
  • Like 4
  • Heart 3
  • 5 months later...
Posted

Just because I know people will have issues converting this. This is how you do that.

 String Itemname = "Logs";
        Optional<String> Itemprice = ItemLookup.get(Itemname, Property.SELL_AVERAGE);
        int ItemPriceConverted = Integer.valueOf(Itemprice.get());
        log(Itemname + " are worth " + ItemPriceConverted + " gp");

Great stuff dreameo <3

  • Heart 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...