Jump to content

Get item price from osrs url?


Recommended Posts

Posted
package com.loudpacks.rs;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.loudpacks.util.IOUtils;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ItemLookup
{
	private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" +
		"/api/catalogue/detail.json?item=%d";

	private static final long TEN_MINUTES = 600000;
	private static final Map<Integer, LookupResult> cache = new HashMap<>();
	private static Map<String, Integer> itemMap;
	private static long startTime;

	static
	{
		startTime = System.currentTimeMillis();
		itemMap = IOUtils.loadItemMap(); //I dumped the cache and generated a map <itemName, itemID> so I can lookup items by name instead of id
	}

	public static int getIdForName(String name)
	{
		if(!itemMap.containsKey(name))
			return -1;
		return itemMap.get(name);
	}

	private static LookupResult parse(int itemId, String json)
	{

		Gson gson = new Gson();
		JsonObject body = gson.fromJson(json, JsonObject.class);
		JsonObject item = body.getAsJsonObject("item");
		JsonObject current = item.getAsJsonObject("current");

		return new LookupResult(
			item.get("icon").getAsString(),
			item.get("icon_large").getAsString(),
			item.get("type").getAsString(),
			item.get("typeIcon").getAsString(),
			item.get("name").getAsString(),
			item.get("description").getAsString(),
			itemId,
			current.get("price").getAsString()
		);
	}

	private static void flushCache()
	{
		if (cache != null)
		{
			cache.clear();
		}
	}

	public static LookupResult find(int itemId)
	{
		if ((System.currentTimeMillis() - TEN_MINUTES) > startTime)
		{
			flushCache();
			startTime = System.currentTimeMillis();
		}

		if (cache != null && !cache.isEmpty())
		{
			LookupResult result = cache.get(itemId);
			if (result != null)
			{
				return result;
			}
		}

		String json;
		try
		{
			URL url = new URL(String.format(API_LOCATION, itemId));
			Scanner scan = new Scanner(url.openStream()).useDelimiter("\\A");
			json = scan.next();
			scan.close();
		}
		catch (IOException e)
		{
			return null;
		}

		LookupResult result = parse(itemId, json);

		if (cache != null)
		{
			cache.put(itemId, result);
		}

		return result;
	}
}
package com.loudpacks.rs;

import lombok.Getter;

public class LookupResult
{
	@Getter
	private final String smallIconUrl, largeIconUrl, type, typeIcon, name, itemDescription;
	@Getter
	private final int id;
	@Getter
	private final String price;

	protected LookupResult(String smallIconUrl,
						   String largeIconUrl,
						   String type,
						   String typeIcon,
						   String name,
						   String itemDescription,
						   int id,
						   String price)
	{

		this.smallIconUrl = smallIconUrl;
		this.largeIconUrl = largeIconUrl;
		this.type = type;
		this.typeIcon = typeIcon;
		this.name = name;
		this.itemDescription = itemDescription;
		this.id = id;
		this.price = price;
	}
}

Some code from a project I was working on. Should be enough to get you started.

  • Like 1
Posted
52 minutes ago, R I F T said:

3 years ago but I believe nothings changed -> https://www.reddit.com/r/2007scape/comments/3g06rq/guide_using_the_old_school_ge_page_api/

Should be able to get it working with a bit of Googling on how to send the requests / parse JSON responses

ummm well,that reddit post is.. cant really say worthless,but what i think is:

you define url as a string

String url = "google.com";

then somehow get the text from the url,and thats it ?

Posted (edited)
package osbot_scripts.framework;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class GEPrice {

	private static final String BASE = "https://api.rsbuddy.com/grandExchange?a=guidePrice&i=";

	/**
	 * Default Constructor
	 * 
	 */
	public GEPrice() {

	}

	/**
	 * Gets the overall price of an item.
	 * 
	 * @param itemID
	 * @return itemPrice
	 * @throws IOException
	 */
	public int getOverallPrice(final int itemID) throws IOException {
		return parse(itemID, "overall");
	}

	/**
	 * Gets the buying price of an item.
	 * 
	 * @param itemID
	 * @return itemPrice
	 * @throws IOException
	 */
	public int getBuyingPrice(final int itemID) throws IOException {
		return parse(itemID, "buying");
	}

	/**
	 * Gets the selling price of an item.
	 * 
	 * @param itemID
	 * @return itemPrice
	 * @throws IOException
	 */
	public int getSellingPrice(final int itemID) throws IOException {
		return parse(itemID, "selling");
	}

	/**
	 * Retrieves the price of an item.
	 * 
	 * @param itemID
	 * @return itemPrice
	 * @throws IOException
	 */
	private int parse(final int itemID, String choice) throws IOException {
		final URL url = new URL(BASE + itemID);
		BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
		String line;
		String price = null;
		while ((line = file.readLine()) != null) {
			if (line.contains("{")) {
				price = (line).trim();
			}
		}
		if (choice.equals("buying")) {
			price = price.substring(price.indexOf(",") + 10, nthOccurrence(price, ',', 1)).trim();
		} else if (choice.equals("selling")) {
			price = price.substring(nthOccurrence(price, ',', 2) + 11, price.indexOf("sellingQuantity") - 2).trim();
		} else {
			price = price.substring(price.indexOf(":") + 1, price.indexOf(",")).trim();
		}
		file.close();
		return Integer.parseInt(price);
	}

	private int nthOccurrence(String str, char c, int n) {
		int pos = str.indexOf(c, 0);
		while (n-- > 0 && pos != -1)
			pos = str.indexOf(c, pos + 1);
		return pos;
	}
}

From DoricsQuester, not sure who made it but seems pretty clean. To use it you can use the following:

GEPrice grandExchangePrices = new GEPrice();

int price = grandExchangePrices.getBuyingPrice(1028);

log(price);

 

Edited by dormic

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