Jump to content

How to get the price of an item in the GE?


Recommended Posts

Posted (edited)

Instead of using rs GE, use rsbuddy GE as its more reflective of actual prices imo...

Following code was found elsewhere and adopted for my usecase (sorry cant remember source)

private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json";

 

 

    public static HashMap<String, Integer> getPriceMap(List<String> items) {
		HashMap<String, Integer> priceMap = new HashMap<>();

		try {
			URL url = new URL(RSBUDDY_URL);
			BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream()));
			JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine());
			Iterator<Member> iterator = priceJSON.iterator();
			
			while (iterator.hasNext()) {
				JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject();
				String itemName = itemJSON.get("name").asString();
				if (items.contains(itemName)) {
					priceMap.put(itemName, itemJSON.get("buy_average").asInt());
				}
			}
		} catch (Exception e) {
		}
		return priceMap;
	}

Then, every time you loot an item or if you already know the item name, add it to a list of strings. You can use it as such....

HashMap<String,Integer> itemPrices = new HashMap();

itemPrices = getPriceMap(items);

For keeping a list of total looted you could (inefficiently, sorry unwilling to share my code)

        for(String item : items){
            itemPrices = getPriceMap(items);
            try {
            total = total + itemPrices.get(item);
            }
            catch (Exception e) {
                log("Empty");
            }
            
        }
        items.clear();
}

 

You can use the following library (literally just copy and paste the .java files into your code, I don't know of a better way of doing this)

https://github.com/ralfstx/minimal-json

Edited by Beenrange
  • Like 1
Posted (edited)

I needed something simpler, something to just indicate the ID, thanks for the code fragment and for the advice.

Price.java

package scripts;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import json.JsonObject;
import json.JsonObject.Member;

public class Price {

	private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json";

	public int getPrice(List<Integer> items) {
		int price = 0;

		try {
			URL url = new URL(RSBUDDY_URL);
			BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream()));
			
			JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine());
			Iterator<Member> iterator = priceJSON.iterator();
			
			while (iterator.hasNext()) {
				JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject();
				int itemID = itemJSON.get("id").asInt();
				if (items.contains(itemID)) {
					price = itemJSON.get("buy_average").asInt();
					break;
				}
			}
		} catch (Exception e) {
		}
		return price;
	}
}

Usage:

final Price price = new Price();
ArrayList<Integer> items = new ArrayList<Integer>(); 
items.add(434);
price.getPrice(items);

 

Edited by trainux
Posted
23 hours ago, Beenrange said:

Instead of using rs GE, use rsbuddy GE as its more reflective of actual prices imo...

Following code was found elsewhere and adopted for my usecase (sorry cant remember source)


private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json";

 

 


    public static HashMap<String, Integer> getPriceMap(List<String> items) {
		HashMap<String, Integer> priceMap = new HashMap<>();

		try {
			URL url = new URL(RSBUDDY_URL);
			BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream()));
			JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine());
			Iterator<Member> iterator = priceJSON.iterator();
			
			while (iterator.hasNext()) {
				JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject();
				String itemName = itemJSON.get("name").asString();
				if (items.contains(itemName)) {
					priceMap.put(itemName, itemJSON.get("buy_average").asInt());
				}
			}
		} catch (Exception e) {
		}
		return priceMap;
	}

Then, every time you loot an item or if you already know the item name, add it to a list of strings. You can use it as such....


HashMap<String,Integer> itemPrices = new HashMap();

itemPrices = getPriceMap(items);

For keeping a list of total looted you could (inefficiently, sorry unwilling to share my code)


        for(String item : items){
            itemPrices = getPriceMap(items);
            try {
            total = total + itemPrices.get(item);
            }
            catch (Exception e) {
                log("Empty");
            }
            
        }
        items.clear();
}

 

You can use the following library (literally just copy and paste the .java files into your code, I don't know of a better way of doing this)

https://github.com/ralfstx/minimal-json

thank you very much brother.
 

  • Like 1

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