Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource OSRS Grand Exchange data lookup API

Primate
Joined
Oct 30, 2014
Messages
3,453
Pretty much stolen, updated, and improved from TopBot, credits to whoever made that.

Code:
package productions.celestial.api.osrs;

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

public class PriceLookup {

    private static InputStream is;
    private static InputStreamReader isr;
    private static BufferedReader br;

    private static String[] getData(int id) {
        try {
            final URL url = new URL(
                    "https://api.rsbuddy.com/grandExchange?a=guidePrice&i="
                            + id);
            final URLConnection con = url.openConnection();
            is = con.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            final String line = br.readLine();
            if (line != null) {
                return line.split(",");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                } else if (isr != null) {
                    isr.close();
                } else if (is != null) {
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static int getAveragePrice(final int id) {
        final String[] data = getData(id);
        if (data != null && data.length == 5) {
            return Integer.parseInt(data[0].replaceAll("\\D", ""));
        }
        return -1;
    }

    public static int getAverageBuyingOffer(final int id) {
        final String[] data = getData(id);
        if (data != null && data.length == 5) {
            return Integer.parseInt(data[1].replaceAll("\\D", ""));
        }
        return -1;
    }

    public static int getAverageBuyingQuantity(final int id) {
        final String[] data = getData(id);
        if (data != null && data.length == 5) {
            return Integer.parseInt(data[2].replaceAll("\\D", ""));
        }
        return -1;
    }

    public static int getAverageSellingOffer(final int id) {
        final String[] data = getData(id);
        if (data != null && data.length == 5) {
            return Integer.parseInt(data[3].replaceAll("\\D", ""));
        }
        return -1;
    }

    public static int getAverageSellingQuantity(final int id) {
        final String[] data = getData(id);
        if (data != null && data.length == 5) {
            return Integer.parseInt(data[4].replaceAll("\\D", ""));
        }
        return -1;
    }

}

Enjoy lads.
 

lad

Joined
Feb 6, 2015
Messages
241
I don't think they have accurate prices on OSBuddy, I noticed it while I was collecting herbs.
 
Top