Jump to content

Pulling GE price's


bumzag

Recommended Posts

I'm trying to use Explv's script to pull GE data but I can't figure out how to actually paint the data on screen.

I've created the two classes "RSExchange" and "ExchangeItem" and then there's this snippet:

Quote

final RSExchange rsExchange = new RSExchange();
rsExchange.getExchangeItem("Spade").ifPresent(System.out::println);

All I want is the sell average from the JSON response but I don't know how to parse/output that info using paint.

 

Edited by bumzag
Link to comment
Share on other sites

I wrote up a quick class to get item prices from the runescape's ge services.

 

For some reason I don't have the spoiler button? Anyways here's the class.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

public final class OsRsExchange {
    private final HashMap<Integer, Long> cachedItems = new HashMap<>();
    private static OsRsExchange instance;

    private OsRsExchange() {}

    public static OsRsExchange getInstance() {
        if (instance == null)
            instance = new OsRsExchange();

        return instance;
    }

    public void cacheItemPricesFromArray(int[] itemIdsArray) {
        for (int value : itemIdsArray) {
            cacheItemPrice(value);
        }
    }

    public boolean cacheItemPrice(int itemId) {
        String urlLink = "http://services.runescape.com/m=itemdb_oldschool//viewitem?obj=" + itemId;
        try {
            URL url = new URL(urlLink);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            StringBuilder sb = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null)
                sb.append(line);

            int indexOfPrice = sb.indexOf("Current Guide Price");

            String priceString = "";

            try {
                priceString = sb.substring(indexOfPrice, sb.indexOf(">", indexOfPrice));
            } catch (StringIndexOutOfBoundsException si) {
                si.printStackTrace();
                System.out.println("Invalid Item Id!");
                return false;
            }

            priceString = priceString.substring(priceString.indexOf("'") + 1, priceString.lastIndexOf("'"));
            String[] splitPriceString = priceString.split(",");

            sb = new StringBuilder();

            for (String s : splitPriceString) {
                sb.append(s);
            }

            long priceOfItem = -1;

            try {
                priceOfItem = Long.parseLong(sb.toString());
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                System.out.println("Incorrect string format to be converted to a long!");
            }

            if (cachedItems.containsKey(itemId))
                cachedItems.replace(itemId, priceOfItem);
            else
                cachedItems.put(itemId, priceOfItem);

            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public long getPriceOfItem(int itemId) {
        if (!cachedItems.containsKey(itemId)) {
            System.out.println("The cached items does not contain this Item Id!");
            return -1;
        }

        return cachedItems.get(itemId);
    }

    public HashMap<Integer, Long> getCachedItems() {
        return cachedItems;
    }

    public void clearCachedItems() {
        cachedItems.clear();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (cachedItems.size() > 0)
            cachedItems.forEach((id, price) -> sb.append(String.format("%s :: %s\n", id, price)));

        return sb.toString();
    }
}

 

To use

//Recommended to use an array in the onStart of your script as retrieving the prices is a pretty taxing process.
int[] itemIds = new int[]{556, 892, 811, 1436, 2627, 5641};
OsRsExchange.getInstance().cacheItemPricesFromArray(itemIds);
System.out.println(OsRsExchange.getInstance());

//If you want to do individual items
OsRsExchange.getInstance().cacheItemPrice(3202);
System.out.println(OsRsExchange.getInstance());

//Retrieve price of item by calling getPriceOfItem()
long price = OsRsExchange.getInstance().getPriceOfItem(2627);
System.out.println(price);
Link to comment
Share on other sites

  • 3 weeks later...
On 12/27/2019 at 10:18 AM, BravoTaco said:

I wrote up a quick class to get item prices from the runescape's ge services.

 

For some reason I don't have the spoiler button? Anyways here's the class.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

public final class OsRsExchange {
    private final HashMap<Integer, Long> cachedItems = new HashMap<>();
    private static OsRsExchange instance;

    private OsRsExchange() {}

    public static OsRsExchange getInstance() {
        if (instance == null)
            instance = new OsRsExchange();

        return instance;
    }

    public void cacheItemPricesFromArray(int[] itemIdsArray) {
        for (int value : itemIdsArray) {
            cacheItemPrice(value);
        }
    }

    public boolean cacheItemPrice(int itemId) {
        String urlLink = "http://services.runescape.com/m=itemdb_oldschool//viewitem?obj=" + itemId;
        try {
            URL url = new URL(urlLink);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            StringBuilder sb = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null)
                sb.append(line);

            int indexOfPrice = sb.indexOf("Current Guide Price");

            String priceString = "";

            try {
                priceString = sb.substring(indexOfPrice, sb.indexOf(">", indexOfPrice));
            } catch (StringIndexOutOfBoundsException si) {
                si.printStackTrace();
                System.out.println("Invalid Item Id!");
                return false;
            }

            priceString = priceString.substring(priceString.indexOf("'") + 1, priceString.lastIndexOf("'"));
            String[] splitPriceString = priceString.split(",");

            sb = new StringBuilder();

            for (String s : splitPriceString) {
                sb.append(s);
            }

            long priceOfItem = -1;

            try {
                priceOfItem = Long.parseLong(sb.toString());
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                System.out.println("Incorrect string format to be converted to a long!");
            }

            if (cachedItems.containsKey(itemId))
                cachedItems.replace(itemId, priceOfItem);
            else
                cachedItems.put(itemId, priceOfItem);

            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public long getPriceOfItem(int itemId) {
        if (!cachedItems.containsKey(itemId)) {
            System.out.println("The cached items does not contain this Item Id!");
            return -1;
        }

        return cachedItems.get(itemId);
    }

    public HashMap<Integer, Long> getCachedItems() {
        return cachedItems;
    }

    public void clearCachedItems() {
        cachedItems.clear();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (cachedItems.size() > 0)
            cachedItems.forEach((id, price) -> sb.append(String.format("%s :: %s\n", id, price)));

        return sb.toString();
    }
}

 

To use


//Recommended to use an array in the onStart of your script as retrieving the prices is a pretty taxing process.
int[] itemIds = new int[]{556, 892, 811, 1436, 2627, 5641};
OsRsExchange.getInstance().cacheItemPricesFromArray(itemIds);
System.out.println(OsRsExchange.getInstance());

//If you want to do individual items
OsRsExchange.getInstance().cacheItemPrice(3202);
System.out.println(OsRsExchange.getInstance());

//Retrieve price of item by calling getPriceOfItem()
long price = OsRsExchange.getInstance().getPriceOfItem(2627);
System.out.println(price);

Thanks. nice contribution

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...