-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zybez.java
60 lines (50 loc) · 1.7 KB
/
Zybez.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package scripts;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Zybez {
private static String zybezString = "";
public static int getPrice(String item) {
int averagePrice = 0;
openZybezItemData(item);
averagePrice = getAveragePrice();
return averagePrice;
}
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
URLConnection uc = null;
try {
URL url = new URL(urlString);
uc = url.openConnection();
uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
uc.connect();
reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
private static void openZybezItemData(String ItemName){
try {
zybezString = readUrl("http://forums.zybez.net/runescape-2007-prices/api/"+ItemName.replaceAll("\\s","+"));
} catch (Exception e) {
e.printStackTrace();
}
}
private static int getAveragePrice() {
Pattern pattern = Pattern.compile("(?<=\"average\":\")[0-9]+");
Matcher matcher = pattern.matcher(zybezString);
while (matcher.find())
return(Integer.parseInt(matcher.group()));
return 0;
}
}