Jump to content

Get item ID's and prices by name


Recommended Posts

Posted (edited)

I made a snippet which will cache all item prices and ID's out of an array in a list at the start of the script.

If the price or ID isn't cached it will automatically add it.

 

Usage:

 

Add this to onStart:


cacheItems(items);
// Put in your own array as parameter

To get an item price:


getPrice(itemID);
getPrice(item Name);
// Will both return the price as an int;

To get an item ID: 


getID(item name);
// This doesn't work if the item isn't cached.

Code:

 


package API;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Integer.parseInt;

/**
 * Created by The Undefeated.
 */
public class FetchItems {

    String itemName;
    public int price;
    public int ID;
    public static List<FetchItems> itemList = new ArrayList<>();
    public FetchItems(String itemName, int ID, int price) {
        this.itemName = itemName;
        this.ID = ID;
        this.price = price;
    }
  
    public static void cacheItems(String[] array) {
        for (String i : array) {
            getInfo(i);
        }
    }

    public static int getPrice(String name) {
        for (int i = 0; i < itemList.size(); i++) {
            if (itemList.get(i).itemName.equals(name)) {
                return itemList.get(i).price;
            }
        }
        getInfo(name);
        return itemList.get((itemList.size() - 1)).price;
    }

    public static int getID(String name) {
        for (int i = 0; i < itemList.size(); i++) {
            if (itemList.get(i).itemName.equals(name)) {
                return itemList.get(i).ID;
            }
        }
        getInfo(name);
        return itemList.get((itemList.size() - 1)).ID;
    }

    private static void getInfo(String item) {
        String fieldID = "GEDBID";
        String priceID = "GEPrice";
        int price2 = 0;
        int ID2 = 0;
        try {
            final URL url = new URL("http://2007.runescape.wikia.com/wiki/Exchange:" + item.replaceAll(" ", "_"));
            BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
            String line;

            while ((line = file.readLine()) != null) {
                if (line.contains("id=\"" + fieldID + "\"")) {
                    int beginIndex = line.indexOf(fieldID) + (fieldID.length() + 2);
                    ID2 = parseInt(line.substring(beginIndex, line.indexOf("<", beginIndex)));
                }
                if((line.contains("id=\"" + priceID + "\""))) {
                    int beginIndex = line.indexOf(priceID) + (priceID.length() + 2);
                    price2 = parseInt((line.substring(beginIndex, line.indexOf("<", beginIndex))).replace(",",""));
                }
            }
            if (price2 != 0 && ID2 != 0) {
                itemList.add(new FetchItems(item, ID2, price2));
            }
            file.close();
        } catch (IOException e) {

        }
    }

}

If you see any improvements I could make, don't hesitate to comment.

 

Edited by The Undefeated
  • Like 1
Posted

Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case.

Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add().

With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list.

  • Like 1
Posted
1 hour ago, Polymorphism said:

Check out mine, it uses the Jagex website. I like your caching idea, but as a pricing api i dont think the caching is necessary, that should be on a script by script basis

 

 

The prices of the RS Grand Exchange API are not always right. Take for example the price of a bow string (ID:1777) so wouldn't recommend that.

 

1 hour ago, Diclonius said:

Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case.

Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add().

With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list.

I'll change that anytime soon.

 

Posted
15 hours ago, Diclonius said:

Looks good. Since you asked for improvements, you could change the data structures. I think the best solution would be a two HashMaps, one mapping Names to item IDs and one mapping item IDs to price. The performance increase would be negligible in practice but it's better in terms of time complexity for this use case.

Even if there are many values, it (almost) always takes the same amount of time to execute put(), contains() and get(). The put() method is roughly equivalent to add().

With an ArrayList, it always takes the same time for add(), but the time it takes to execute contains() is proportional to the number of items in the list. This means if the number of items is very large, then it can take very long time to search through the list.

Yes definitely use map for this.

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