-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuestionScraper.java
More file actions
229 lines (210 loc) · 6.9 KB
/
QuestionScraper.java
File metadata and controls
229 lines (210 loc) · 6.9 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuestionScraper {
private ArrayList<Game> allGames;
private Set<String> invalidGameIds;
public QuestionScraper() {
allGames = new ArrayList<Game>();
addInvalidGames();
GameUrlScraper scraper = new GameUrlScraper();
String gameBase = "http://www.j-archive.com/showgame.php?game_id=";
for (String s : scraper.getGameIds()) {
if (invalidGameIds.contains(s)) {
continue;
}
System.out.println("Last game Id tried: " + s);
try {
URL url = new URL(gameBase + s);
Game g = createGame(url, s);
g.cleanUp();
allGames.add(g);
g.writeGameToDatabase();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
private void addInvalidGames() {
invalidGameIds = new HashSet<String>();
invalidGameIds.add("4284");
invalidGameIds.add("4281");
invalidGameIds.add("4279");
invalidGameIds.add("4271");
invalidGameIds.add("4273");
invalidGameIds.add("4256");
invalidGameIds.add("4261");
invalidGameIds.add("295");
invalidGameIds.add("3117");
invalidGameIds.add("4246");
invalidGameIds.add("4296");
invalidGameIds.add("3973");
invalidGameIds.add("4226");
invalidGameIds.add("1243");
invalidGameIds.add("2117");
invalidGameIds.add("2356");
invalidGameIds.add("1743");
invalidGameIds.add("1742");
invalidGameIds.add("1744");
invalidGameIds.add("1745");
invalidGameIds.add("1746");
invalidGameIds.add("1747");
invalidGameIds.add("1748");
invalidGameIds.add("1749");
invalidGameIds.add("1750");
invalidGameIds.add("1751");
invalidGameIds.add("1752");
invalidGameIds.add("1753");
invalidGameIds.add("3576");
invalidGameIds.add("3575");
}
public ArrayList<Game> getGames() {
return allGames;
}
private Game createGame(URL url, String stringId) {
String html = "";
BufferedReader incomingHTML;
//Get html
try {
incomingHTML = new BufferedReader(new InputStreamReader(url.openStream()));
String currLine;
currLine = incomingHTML.readLine();
StringBuilder str = new StringBuilder();
while (currLine != null) {
str.append(currLine);
currLine = incomingHTML.readLine();
}
incomingHTML.close();
html = str.toString();
}
catch (IOException e) {
e.printStackTrace();
}
//Get rows and columns for both rounds
Queue<Integer> rows = new LinkedList<Integer>();
Queue<Integer> columns = new LinkedList<Integer>();
Pattern p = Pattern.compile("<td id=\"clue_D?J_(.?)_(.?)_");
Matcher m = p.matcher(html);
while (m.find()) {
columns.offer(Integer.parseInt(m.group(1)));
rows.offer(Integer.parseInt(m.group(2)));
}
//Get categories
ArrayList<Category> round1Categories = new ArrayList<Category>();
ArrayList<Category> round2Categories = new ArrayList<Category>();
p = Pattern.compile("<td class=\"category_name\">(.*?)</td>");
m = p.matcher(html);
Pattern pComment = Pattern.compile("<td class=\"category_comments\">(.*?)</td>");
Matcher mComment = pComment.matcher(html);
Category FJcategory = null;
int count = 0;
while (m.find()) {
String comment = "";
if (mComment.find()) {
comment = mComment.group(1);
}
else {
System.out.println("FLAG: possible mismatched categories and comments");
}
Category c = new Category(m.group(1), comment);
if (count < 6) {
round1Categories.add(c);
}
else if (count < 12) {
round2Categories.add(c);
}
else if (count == 12) {
FJcategory = c;
}
else {
System.out.println("FLAG: extra categories present");
}
count++;
}
//Get questions
p = Pattern.compile("clue_text\">(.*?)</td>");
m = p.matcher(html);
ArrayList<Question> allQuestions = new ArrayList<Question>();
count = 0;
int numQuestionsInRound1 = 0;
int nextRow;
int nextCol;
while (!rows.isEmpty() && !columns.isEmpty()) {
if (m.find()) {
nextRow = rows.poll();
nextCol = columns.poll();
//PROBLEM: Won't work if first question is ever blank
if (count > 0 && nextRow == 1 && nextCol == 1) {
numQuestionsInRound1 = count;
}
Question q = new Question(m.group(1), nextRow, nextCol);
allQuestions.add(q);
count++;
}
else {
System.out.println("FLAG: not enough questions for rows/cols");
}
}
Question FJquestion;
if (m.find()) {
FJquestion = new Question(m.group(1), -1, -1);
FJquestion.setCategory(FJcategory);
}
else {
FJquestion = null;
System.out.println("FLAG: no FJ question");
}
//Get answers
p = Pattern.compile("correct_response">(.*?)</em>");
m = p.matcher(html);
int index = 0;
while (m.find() && index < allQuestions.size()) {
allQuestions.get(index).setAnswer(m.group(1));
index++;
}
/* TODO: Get FJ answer
p = Pattern.compile("correct_response\\">(.*?)</em>");
m = p.matcher(html);
FJquestion.setAnswer(m.group(1));*/
//Split questions by round
ArrayList<Question> round1Questions = new ArrayList<Question>();
ArrayList<Question> round2Questions = new ArrayList<Question>();
for (int i = 0; i < allQuestions.size(); i++) {
if (i < numQuestionsInRound1) {
round1Questions.add(allQuestions.get(i));
}
else {
round2Questions.add(allQuestions.get(i));
}
}
//Set categories and create boards
Gameboard jeopardyBoard = createBoard(round1Questions, round1Categories, false);
Gameboard doubleJeopardyBoard = createBoard(round2Questions, round2Categories, true);
//Create this game with its gameboards
Game g = new Game(jeopardyBoard, doubleJeopardyBoard, Integer.parseInt(stringId), FJquestion);
return g;
}
private Gameboard createBoard(ArrayList<Question> questions, ArrayList<Category> categories,
boolean djBoolean) {
for (Question q : questions) {
q.setRoundAndValue(djBoolean);
Category thisC = categories.get(q.getCol()-1);
q.setCategory(thisC);
thisC.addQuestion(q);
}
Gameboard board = new Gameboard(djBoolean);
board.setCategories(categories);
board.setQuestions(questions);
return board;
}
}