Jump to content

GE Data


dreameo

Recommended Posts

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
Link to comment
Share on other sites

  • 5 months later...

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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