-
[Java] '07 Price Checker
Wrote a small Rs client for 07 for playing when extremely bored and needed prices.
Finds average price off of the Zybez '07 market.
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class sevenPrice {
private int price;
public static void main(String[] arg) {
System.out.println(new sevenPrice("nature rune").getPrice());
}
private sevenPrice(String name) {
String info = getInfo(name);
info = info.substring(info.toLowerCase().indexOf(":\"" + name.toLowerCase() + "\""));
info = info.substring(info.indexOf("\"offers\":[") + 13);
String[] itemArray = info.split("\\}");
ArrayList<Item> iz = new ArrayList<Item>();
long priceaverage = 0;
long amount = 0;
if (itemArray.length >= 25) {
for (int i = 0; i < itemArray.length; i++) {
String s = itemArray[i];
if (s.contains("price")) {
i++;
String price = s.substring(s.indexOf("\"price\":\"") + "\"price\":\"".length());
price = price.replace(price.substring(price.indexOf("\",\"date\":\"")), "");
String amountOf = s.substring(s.indexOf("\"quantity\":") + "\"quantity\":\"".length());
amountOf = amountOf.replace(amountOf.substring(amountOf.indexOf("\",\"price\":\"")), "");
long a = Long.parseLong(amountOf);
long p = Long.parseLong(price);
if (a > 1000000 || p > 20000000) {
i--;
continue;
}
iz.add(new Item((int) a, (int) p));
}
}
}
Collections.sort(iz, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.getPrice() - o1.getPrice();
}
});
for (int c = 0; c < 3; c++) {
iz.remove(0);
iz.remove(iz.size() - 1);
}
for (Item p : iz) {
priceaverage += p.getAmount() * p.getPrice();
amount += p.getAmount();
}
price = (int) (priceaverage / (amount * 1.0));
if (price > 500000) {
price = ((int) (price * .001)) * 1000;
}
}
private int getPrice() {
return this.price;
}
private String getInfo(String name) {
name = name.replaceAll(" ", "+");
try {
String text = "http://forums.zybez.net/runescape-2007-prices/api/" + name;
URL url = new URL(text);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0"); //Zybez throwing 403's at me :(
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String f = in.readLine();
in.close();
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//by wyn
private class Item {
private final int amount;
private final int price;
public Item(int amount, int price) {
this.amount = amount;
this.price = price;
}
public int getAmount() {
return this.amount;
}
public int getPrice() {
return this.price;
}
}
}