Jump to content

GE Data using RSBuddy & JSON Simple library


Explv

Recommended Posts

You will need to include the JSON Simple library in your project (this can also be used on the SDN):

https://github.com/fangyidong/json-simple

RSExchange.java

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public final class RSExchange {

    private final JSONObject ITEMS_JSON;

    public RSExchange() throws Exception {
        Optional<JSONObject> itemsJSONOpt = getItemsJson();
        if (!itemsJSONOpt.isPresent()) {
            throw new Exception("Failed to get items JSON");
        }
        JSONObject itemsJSONKeyedByID = itemsJSONOpt.get();

        JSONObject itemsJSONKeyedByName = new JSONObject();
        for (Object itemValue : itemsJSONKeyedByID.values()) {
            JSONObject itemValueJSON = (JSONObject) itemValue;
            itemsJSONKeyedByName.put(itemValueJSON.get("name"), itemValueJSON);
        }
        ITEMS_JSON = itemsJSONKeyedByName;
    }

    private Optional<JSONObject> getItemsJson() {
        try {
            URL url = new URL("https://rsbuddy.com/exchange/summary.json");
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            con.setUseCaches(true);

            try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                JSONParser jsonParser = new JSONParser();
                return Optional.of((JSONObject) jsonParser.parse(bufferedReader));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    public final Map<String, ExchangeItem> getExchangeItems(final String... itemNames) {
        Map<String, ExchangeItem> exchangeItems = new HashMap<>();
        for (final String itemName : itemNames) {
            getExchangeItem(itemName).ifPresent(exchangeItem -> exchangeItems.put(itemName, exchangeItem));
        }
        return exchangeItems;
    }

    public final Optional<ExchangeItem> getExchangeItem(final String itemName) {
        Object itemObj = ITEMS_JSON.get(itemName);
        if (itemObj == null) {
            return Optional.empty();
        }
        JSONObject itemJSON = (JSONObject) itemObj;
        return Optional.of(new ExchangeItem((String) itemJSON.get("name"), (long) itemJSON.get("id")));
    }
}

 

ExchangeItem.java

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public final class ExchangeItem {

    private final String name;
    private final long id;
    private long overallAverage = -1;
    private long buyAverage = -1;
    private long sellAverage = -1;
    private long buyingQuantity;
    private long sellingQuantity;

    public ExchangeItem(final String name, final long id) {
        this.name = name;
        this.id = id;
        updateRSBuddyValues();
    }

    public final String getName() {
        return name;
    }

    public final long getId() {
        return id;
    }

    public final long getBuyAverage() {
        return buyAverage;
    }

    public final long getSellAverage() {
        return sellAverage;
    }

    public final long getBuyingQuantity() {
        return buyingQuantity;
    }

    public final long getSellingQuantity() {
        return sellingQuantity;
    }

    public void updateRSBuddyValues() {
        try {
            URL url = new URL("http://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + id);
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

            try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                JSONParser jsonParser = new JSONParser();
                JSONObject itemJSON = (JSONObject) jsonParser.parse(bufferedReader);

                overallAverage = (long) itemJSON.get("overall");
                sellAverage = (long) itemJSON.get("selling");
                buyAverage = (long) itemJSON.get("buying");
                buyingQuantity = (long) itemJSON.get("buyingQuantity");
                sellingQuantity = (long) itemJSON.get("sellingQuantity");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final String toString() {
        return String.format("Name: %s, ID: %d, Overall AVG: %d gp, Buying AVG: %d gp, Selling AVG: %d gp, Buying Quantity: %d, Selling Quantity:%d",
                name, id, overallAverage, buyAverage, sellAverage, buyingQuantity, sellingQuantity);
    }
}

 

Usage:

RSExchange rsExchange = new RSExchange();
Optional<ExchangeItem> exchangeItem = rsExchange.getExchangeItem("Yew logs");
exchangeItem.ifPresent(System.out::println);

Prints:

Name: Yew logs, ID: 1515, Overall AVG: 355 gp, Buying AVG: 354 gp, Selling AVG: 355 gp, Buying Quantity: 67874, Selling Quantity:72956

 

Or:

Map<String, ExchangeItem> exchangeItems = rsExchange.getExchangeItems("Yew logs", "Iron bar");
exchangeItems.values().forEach(System.out::println);

Prints:

Name: Yew logs, ID: 1515, Overall AVG: 354 gp, Buying AVG: 354 gp, Selling AVG: 354 gp, Buying Quantity: 47056, Selling Quantity:33141
Name: Iron bar, ID: 2351, Overall AVG: 179 gp, Buying AVG: 182 gp, Selling AVG: 177 gp, Buying Quantity: 2765, Selling Quantity:4467

 

Edited by Explv
  • Like 3
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...