Jump to content

Get item ID's and prices by name


The Undefeated

Recommended Posts

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

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

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.

 

Link to comment
Share on other sites

12 minutes ago, Polymorphism said:

 

I've never seen one that wasn't right...

6SjjXqV.png  AbKAZKK.png

OSBuddy price is way lower but their website is down 50% of the time.

I checked this afternoon, and people in chat can confirm, the API price of bow strings were 281 each.

 

Edited by The Undefeated
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

8 minutes ago, Polymorphism said:

 

OSBuddy price doesn't reflect in game prices i believe. Which is why i never use it

But as I said, the official GE API fails many times. I used it in one my scripts and it's horrible to use because of the wrong prices sometimes. That's why I think using the wiki is the way to go.

 

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