Jump to content

Price grabber


Cyberus

Recommended Posts


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

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...