Cyberus Posted August 2, 2013 Share Posted August 2, 2013 (edited) package org.rabrg.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; /** * Grabs prices from the Zybez RuneScape 2007 marketplace. * @author Ryan Greene * */ public final class PriceGrabber { /** * The base of the URL used to grab prices. */ private static final String BASE_URL = "http://forums.zybez.net/runescape-2007-prices/api/"; /** * Gets the price of the item with the specified name. * @param itemName The name of the item. * @return The price of the item. * @throws IOException If an IOException occurs. */ public static Price getPrice(final String itemName) throws IOException { try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(BASE_URL + itemName).openStream()))) { String line = reader.readLine().replaceAll("" + '"', ""); int id = Integer.parseInt(line.split("id:")[1].split(",")[0]); String name = line.split("name:")[1].split(",")[0]; String image = line.split("image:")[1].split(",")[0]; float recentHigh = Float.parseFloat(line.split("recent_high:")[1].split(",")[0]); float recentLow = Float.parseFloat(line.split("recent_low:")[1].split(",")[0]); float average = Float.parseFloat(line.split("average:")[1].split(",")[0]); int highAlch = Integer.parseInt(line.split("high_alch:")[1].split(",")[0]); return new Price(id, name, image, recentHigh, recentLow, average, highAlch); } } /** * Represents a single price of an item. * @author Ryan Greene * */ public static final class Price { /** * The id of the item. */ private final int id; /** * The name of the item. */ private final String name; /** * The image of the item. */ private final String image; /** * The recent high price of the item. */ private final float recentHigh; /** * The recent low price of the item. */ private final float recentLow; /** * The average price of the item. */ private final float average; /** * The high alch value of the item. */ private final int highAlch; /** * Constructs a new price. * @param id * @param name * @param image * @param recentHigh * @param recentLow * @param average * @param highAlch */ private Price(int id, String name, String image, float recentHigh, float recentLow, float average, int highAlch) { this.id = id; this.name = name; this.image = image; this.recentHigh = recentHigh; this.recentLow = recentLow; this.average = average; this.highAlch = highAlch; } /** * Gets the id of the item. * @return The id of the item. */ public int getId() { return id; } /** * Gets the name of the item. * @return The name of the item. */ public String getName() { return name; } /** * Gets the image of the item. * @return The image of the item. */ public String getImage() { return image; } /** * Gets the recent high price of the item. * @return The recent high price of the item. */ public float getRecentHigh() { return recentHigh; } /** * Gets the recent low price of the item. * @return The recent low price of the item. */ public float getRecentLow() { return recentLow; } /** * Gets the average price of the item. * @return The average price of the item. */ public float getAverage() { return average; } /** * Gets the high alch value of the item. * @return The high alch value of the item. */ public int getHighAlch() { return highAlch; } } } Edited August 2, 2013 by Cyberus Link to comment Share on other sites More sharing options...
miami305heat6 Posted August 2, 2013 Share Posted August 2, 2013 what? do we copy nd paste and put it in our script folder? then what will happen? Link to comment Share on other sites More sharing options...
Cyberus Posted August 2, 2013 Author Share Posted August 2, 2013 Price price = PriceGrabber.getPrice("Rune Axe"); Link to comment Share on other sites More sharing options...
Diclonius Posted August 2, 2013 Share Posted August 2, 2013 (edited) Very nice! I was just about to make something like this myself but then I saw then ! edit: I guess you didn't make it? You use it like this: Price price = PriceGrabber.getPrice("rune+axe"); Edited August 2, 2013 by Diclonius Link to comment Share on other sites More sharing options...
Cyberus Posted August 2, 2013 Author Share Posted August 2, 2013 I did make it, and my example works. Link to comment Share on other sites More sharing options...
mike21nolan Posted August 6, 2013 Share Posted August 6, 2013 This is sweet, it should be implemented in client methods. Link to comment Share on other sites More sharing options...