-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuneScrape.java
More file actions
292 lines (248 loc) · 10.4 KB
/
RuneScrape.java
File metadata and controls
292 lines (248 loc) · 10.4 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
import java.util.regex.PatternSyntaxException;
import java.io.UnsupportedEncodingException;
public class RuneScrape {
public static void main(String[] args) {
System.out.println("Running");
String code = "";
String name = "";
String[] websites = {
"http://services.runescape.com/m=itemdb_oldschool/Ranarr+seed/viewitem?obj=5295",
"http://services.runescape.com/m=itemdb_oldschool/Grimy+ranarr+weed/viewitem?obj=207",
"http://services.runescape.com/m=itemdb_oldschool/Snapdragon+seed/viewitem?obj=5300",
"http://services.runescape.com/m=itemdb_oldschool/Grimy+snapdragon/viewitem?obj=3051",
"http://services.runescape.com/m=itemdb_oldschool/Torstol+seed/viewitem?obj=5304",
"http://services.runescape.com/m=itemdb_oldschool/Grimy+torstol/viewitem?obj=219",
"http://services.runescape.com/m=itemdb_oldschool/Dragonfruit+tree+seed/viewitem?obj=22877",
"http://services.runescape.com/m=itemdb_oldschool/Maple+seed/viewitem?obj=5314",
"http://services.runescape.com/m=itemdb_oldschool/Palm+tree+seed/viewitem?obj=5289",
"http://services.runescape.com/m=itemdb_oldschool/Redwood+tree+seed/viewitem?obj=22871",
"http://services.runescape.com/m=itemdb_oldschool/Yew+seed/viewitem?obj=5315",
"http://services.runescape.com/m=itemdb_oldschool/Old+school+bond/viewitem?obj=13190",
"http://services.runescape.com/m=itemdb_oldschool/Ahrim%27s+hood/viewitem?obj=4708",
"http://services.runescape.com/m=itemdb_oldschool/Ahrim%27s+robeskirt/viewitem?obj=4714",
"http://services.runescape.com/m=itemdb_oldschool/Ahrim%27s+robetop/viewitem?obj=4712",
"http://services.runescape.com/m=itemdb_oldschool/Ahrim%27s+staff/viewitem?obj=4710",
"http://services.runescape.com/m=itemdb_oldschool/Torag%27s+hammers/viewitem?obj=4747",
"http://services.runescape.com/m=itemdb_oldschool/Torag%27s+helm/viewitem?obj=4745",
"http://services.runescape.com/m=itemdb_oldschool/Torag%27s+platebody/viewitem?obj=4749",
"http://services.runescape.com/m=itemdb_oldschool/Torag%27s+platelegs/viewitem?obj=4751",
"http://services.runescape.com/m=itemdb_oldschool/Dharok%27s+greataxe/viewitem?obj=4718",
"http://services.runescape.com/m=itemdb_oldschool/Dharok%27s+helm/viewitem?obj=4716",
"http://services.runescape.com/m=itemdb_oldschool/Dharok%27s+platebody/viewitem?obj=4720",
"http://services.runescape.com/m=itemdb_oldschool/Dharok%27s+platelegs/viewitem?obj=4722",
"http://services.runescape.com/m=itemdb_oldschool/Guthan%27s+chainskirt/viewitem?obj=4730",
"http://services.runescape.com/m=itemdb_oldschool/Guthan%27s+helm/viewitem?obj=4724",
"http://services.runescape.com/m=itemdb_oldschool/Guthan%27s+platebody/viewitem?obj=4728",
"http://services.runescape.com/m=itemdb_oldschool/Guthan%27s+warspear/viewitem?obj=4726",
"http://services.runescape.com/m=itemdb_oldschool/Karil%27s+coif/viewitem?obj=4732",
"http://services.runescape.com/m=itemdb_oldschool/Karil%27s+crossbow/viewitem?obj=4734",
"http://services.runescape.com/m=itemdb_oldschool/Karil%27s+leatherskirt/viewitem?obj=4738",
"http://services.runescape.com/m=itemdb_oldschool/Karil%27s+leathertop/viewitem?obj=4736",
"http://services.runescape.com/m=itemdb_oldschool/Verac%27s+brassard/viewitem?obj=4757",
"http://services.runescape.com/m=itemdb_oldschool/Verac%27s+flail/viewitem?obj=4755",
"http://services.runescape.com/m=itemdb_oldschool/Verac%27s+helm/viewitem?obj=4753",
"http://services.runescape.com/m=itemdb_oldschool/Verac%27s+plateskirt/viewitem?obj=4759",
"http://services.runescape.com/m=itemdb_oldschool/Mithril+bar/viewitem?obj=2359",
"http://services.runescape.com/m=itemdb_oldschool/Iron+bar/viewitem?obj=2351",
"http://services.runescape.com/m=itemdb_oldschool/Steel+bar/viewitem?obj=2353",
"http://services.runescape.com/m=itemdb_oldschool/Rune+platebody+%28g%29/viewitem?obj=2615",
"http://services.runescape.com/m=itemdb_oldschool/Rune+full+helm+%28g%29/viewitem?obj=2619",
"http://services.runescape.com/m=itemdb_oldschool/Ring+of+dueling%288%29/viewitem?obj=2552",
"http://services.runescape.com/m=itemdb_oldschool/Amulet+of+glory/viewitem?obj=1704"
};
for (int i = 0; i < websites.length; i++) {
code = getURLSource(websites[i]);
name = setName(code);
code = clean(code);
String[] pricesStringArray = code.split("\\r?\\n");
int[] prices = new int[pricesStringArray.length];
for (int j = 0; j < pricesStringArray.length; j++) {
prices[j] = Integer.parseInt(pricesStringArray[j]);
}
System.out.println(name);
DecimalFormat formatter = new DecimalFormat("#,###");
int currentPrice = prices[prices.length - 1];
System.out.println("Current Price: $" + formatter.format(currentPrice));
System.out.println("Oldest Price: $" + formatter.format(prices[0]));
int middlePrice = (prices[prices.length - 1] + prices[0]) / 2;
System.out.println("Middle Price: $" + formatter.format(middlePrice));
if (currentPrice < middlePrice) {
System.out.println("Current Price < Middle Price.");
System.out.println("Buy Now\n");
}
else {
System.out.println("Current Price > Middle Price.");
System.out.println("DO NOT BUY\n");
}
}
}
public static String getURLSource(String url) {
URL urlObject = null;
URLConnection urlConnection = null;
try {
urlObject = new URL(url);
}
catch (MalformedURLException e) {
System.err.println("MalformedURLException occured.");
System.err.println("Printing Error.");
System.err.println(e);
}
try {
urlConnection = urlObject.openConnection();
}
catch (IOException e) {
System.err.println("IOException occured.");
System.err.println("Printing Error.");
System.err.println(e);
}
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
try {
return toString(urlConnection.getInputStream());
}
catch (IOException e) {
System.err.println("IOException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "getURLSource Failed.";
}
}
private static String toString(InputStream inputStream) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
}
catch (UnsupportedEncodingException e) {
System.err.println("UnsupportedEncodingException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "toString() Failed.";
}
String inputLine;
StringBuilder stringBuilder = new StringBuilder();
try {
while ((inputLine = bufferedReader.readLine()) != null) {
stringBuilder.append(inputLine);
}
}
catch (IOException e) {
System.err.println("IOException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "toString() Failed.";
}
return stringBuilder.toString();
}
public static String clean(String code) {
/* Remove code before first data instance */
String clean = code.substring(code.indexOf("average180.push"));
clean.trim();
/* Remove code after last data instance */
clean = clean.substring(0, clean.indexOf("</script>"));
/* Print all data instances on a new line */
clean = clean.replaceAll(";", ";\n");
/* Remove all tabs */
clean = clean.replaceAll(" ", "");
/* Remove all multi-spaces */
clean = clean.replaceAll(" ", "");
clean = removeDateTrade(clean);
clean = removePriceAverage(clean);
return clean;
}
public static String removeDateTrade(String clean) {
/* Count occurrences of trade data instances */
int fromIndex = 0;
int count = 0;
while ((fromIndex = clean.indexOf("tr", fromIndex)) != -1) {
count++;
fromIndex++;
}
/* Remove occurrences of trade data instances */
for (int i = 0; i < count; i++) {
int startIndexTr = clean.indexOf("tr");
int endIndexTr = clean.indexOf(";", startIndexTr);
String replaceTr = clean.substring(startIndexTr, endIndexTr + 2);
clean = clean.replaceFirst(Pattern.quote(replaceTr), "");
}
/* Count unnecessary parts of average data occurrences */
if (count == 0) {
while ((fromIndex = clean.indexOf("av", fromIndex)) != -1) {
count++;
fromIndex++;
}
}
/* Remove unnecessary parts of average data occurrences */
/*
This will always run.
If first count succeeds, the count transfers over to second for loop.
If first count fails, count is recounted, then second for loop runs.
*/
for (int i = 0; i < count; i++) {
int startIndexAv = clean.indexOf("av");
int endIndexAv = clean.indexOf(", ", startIndexAv);
String replaceAv = clean.substring(startIndexAv, endIndexAv + 2);
clean = clean.replaceFirst(Pattern.quote(replaceAv), "");
}
return clean;
}
public static String removePriceAverage(String clean) {
int fromIndex = 0;
int count = 0;
String replaceCo = null;
while ((fromIndex = clean.indexOf(", ", fromIndex)) != -1) {
count++;
fromIndex++;
}
/* Remove all occurrences of price averages */
for (int i = 0; i < count; i++) {
int startIndexCo = clean.indexOf(", ", 0);
int endIndexCo = clean.indexOf(");", 0);
int fromIndexTest = 0;
int countTest = 0;
try {
replaceCo = clean.substring(startIndexCo, endIndexCo + 2);
}
catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "removePriceAverage() Failed.";
}
try {
clean = clean.replaceFirst(Pattern.quote(replaceCo), "");
}
catch (PatternSyntaxException e) {
System.err.println("PatternSyntaxException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "removePriceAverage() Failed.";
}
}
return clean;
}
/* Sets the item name */
public static String setName(String code) {
String name = code.substring(code.indexOf("<h2>") + 4);
name.trim();
try {
name = name.substring(0, name.indexOf("</h2>"));
}
catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException occured.");
System.err.println("Printing Error.");
System.err.println(e);
return "Unknown Item Name";
}
return name;
}
}