Jump to content

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


trainux

Recommended Posts

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

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

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