-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonScraper.java
More file actions
288 lines (266 loc) · 12.4 KB
/
PokemonScraper.java
File metadata and controls
288 lines (266 loc) · 12.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class PokemonScraper {
public static void main(String args[]) throws IOException, InterruptedException {
//mapDataBases("TCG_DataBase","Pokecardex_DataBase");
//matchCollectionIds("TCG_DataBase");
String database = "Pokecardex_DataBase";
//retrieveCardImages("CRZ", database);
String[] collectionIds = ReadWrite.readText("D:/DataBases/Pokemon/CardNames/"+database+"/collectionIdsMatch.txt").split(";");
for (int i = 0; i < collectionIds.length; i++) {
String collectionId = collectionIds[i].split(":")[0];
System.out.println("Retrieving : "+collectionId+", "+i+"/"+collectionIds.length);
try {
//retrieveCardIds(collectionId);
retrieveCardImages(collectionId, database);
Thread.sleep(500);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//resize("./Data/Pokemon/CardImages/CEL/CEL_11-2.png", "./Data/Pokemon/ResizedCardImages/CEL/CEL_11-2.png", 23, 32);
//resizeAllImages("CEL", 23, 32);
}
public static String formatForEqual(String toFormat) {
return toFormat.replace(" ", "").toLowerCase().replace("&", "&").replace("?", "\'").trim();
}
public static void mapDataBases(String db1, String db2) throws IOException {
String[] matchIds1 = ReadWrite.readText("D:/DataBases/Pokemon/CardNames/"+db1+"/collectionIdsMatch.txt").split(";");
String[] matchIds2 = ReadWrite.readText("D:/DataBases/Pokemon/CardNames/"+db2+"/collectionIdsMatch.txt").split(";");
String match = "";
for (int i = 0; i < matchIds1.length; i++) {
String[] couple1 = matchIds1[i].split(":");
boolean matched = false;
String last = "";
for (int j = 0; j < matchIds2.length; j++) {
String[] couple2 = matchIds2[j].split(":");
if (formatForEqual(couple1[1]).equals(formatForEqual(couple2[1])) || formatForEqual(couple1[0]).equals(formatForEqual(couple2[0]))) {
matched = true;
last = couple1[0]+":"+couple2[0]+";";
match+=last;
}
}
if (!matched)
System.out.println("Unmatched : "+couple1[0]+" - "+couple1[1]);
}
match.substring(0, match.length()-1);
ReadWrite.write("D:/DataBases/Pokemon/CardNames/"+db1+"-"+db2+"_match.txt", match);
}
public static String formatId(String cardId) {
String prefix = "";
String suffix = "";
if (!Character.isDigit(cardId.charAt(cardId.length()-1))) {
if (cardId.length() == 1) {
return cardId;
}
suffix += cardId.charAt(cardId.length()-1);
cardId = cardId.substring(0, cardId.length()-1);
}
for (int i = cardId.length(); i < 3; i++) {
prefix = "0"+prefix;
}
return prefix+cardId+suffix;
}
public static void matchCollectionIds(String database) throws IOException {
String names = "";
String url = "https://limitlesstcg.com/cards/fr";
String filePath = "D:/DataBases/Pokemon/HTML/"+database+"/collectionIdsMatch.txt";
String page = "";
if (!ReadWrite.exists(filePath)) {
page = DataScraper.downloadWebPage(url);
ReadWrite.write(filePath, page);
} else {
page = ReadWrite.readText(filePath);
}
Pattern p = Pattern.compile("<img class=\\\"set\\\" alt=\\\"([^\\\"]+)\\\"[^>]+>([^<]+)<");
Matcher m = p.matcher(page);
while(m.find()) {
String collId = m.group(1);
String collName = m.group(2).replace("&", "&").replace("?", "\'").trim();
System.out.println(collId+" : "+collName);
names += collId+":"+collName+";";
}
if (names.length() == 0)
return;
names = names.substring(0, names.length()-1);
System.out.println(names.split(";").length);
ReadWrite.write("D:/DataBases/Pokemon/CardNames/"+database+"/collectionIdsMatch.txt", names);
}
public static void resizeAllImages(String collectionId, int width, int height) throws IOException {
File file = new File("./Data/Pokemon/ResizedCardImages/"+collectionId);
file.mkdirs();
for (File sourceImageFile : new File("./Data/Pokemon/CardImages/"+collectionId).listFiles()) {
resize(sourceImageFile.getAbsolutePath(), "./Data/Pokemon/ResizedCardImages/"+collectionId+"/"+sourceImageFile.getName(), width, height);
}
}
public static void retrieveCardImages(String collectionId, String database) throws IOException, InterruptedException {
String names = ReadWrite.readText("D:/DataBases/Pokemon/CardNames/"+database+"/"+collectionId+".txt");
String[] ids = names.split(";");
File file = new File("D:/DataBases/Pokemon/CardImages/"+database+"/"+collectionId);
file.mkdirs();
String missing = "";
String replaced = "";
Console.startProgressBar(1, ids.length);
//ExecutorService executorService = Executors.newFixedThreadPool(7);
for (int i = 0; i < ids.length; i++) {
String id = (ids[i]);
String url = "https://www.pokecardex.com/assets/images/sets/"+collectionId.toUpperCase()+"/HD/"+(i+1)+".jpg";
String save_url = "https://www.pokecardex.com/assets/images/sets/"+collectionId.toUpperCase()+"/"+(i+1)+".jpg";
String path = "D:/DataBases/Pokemon/CardImages/"+database+"/"+collectionId+"/"+collectionId+"_"+(i+1)+".jpg";
Console.progress(1);
//DataScraper.Scraper task = new DataScraper.Scraper(url, path);
//executorService.submit(task);
try {
//if (!ReadWrite.exists(path))
ReadWrite.write(path, DataScraper.downloadImage(url));
} catch (IOException e) {
try {
//if (!ReadWrite.exists(path))
ReadWrite.write(path, DataScraper.downloadImage(save_url));
} catch (IOException e2) {
missing += id+";";
}
}
//ReadWrite.write(path, DataScraper.downloadImage(url));
}
//executorService.shutdown();
//executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
Console.end();
if (missing.length() > 0) {
missing = missing.substring(0, missing.length()-1);
ReadWrite.write(file.getAbsolutePath()+"/"+collectionId+"_missing.txt", missing);
System.out.println("Missing "+missing.split(";").length+" cards");
} else {
if (ReadWrite.exists(file.getAbsolutePath()+"/"+collectionId+"_missing.txt")) {
ReadWrite.delete(file.getAbsolutePath()+"/"+collectionId+"_missing.txt");
}
}
if (replaced.length() > 0) {
replaced = replaced.substring(0, replaced.length()-1);
ReadWrite.write("./Data/Pokemon/CardImages/"+collectionId+"/"+collectionId+"_replaced.txt", replaced);
System.out.println("Replaced "+replaced.split(";").length+" cards");
} else {
if (ReadWrite.exists("./Data/Pokemon/CardImages/"+collectionId+"/"+collectionId+"_replaced.txt")) {
ReadWrite.delete("./Data/Pokemon/CardImages/"+collectionId+"/"+collectionId+"_replaced.txt");
}
}
}
public static void retrieveCardIds(String collectionId) throws IOException {
int i = 1;
boolean next = true;
String names = "";
while (next) {
String url = "https://www.pokemon.com/fr/jcc-pokemon/cartes-pokemon/"+i+"?"+collectionId;
//String[] array = url.split("/");
String filePath = "D:/DataBases/Pokemon/HTML/Pokemon_DataBase/"+collectionId+"-"+i+".txt";
String page = "";
if (!ReadWrite.exists(filePath)) {
page = DataScraper.downloadWebPage(url);
ReadWrite.write(filePath, page);
} else {
page = ReadWrite.readText(filePath);
}
if (page.contains("Aucune carte ne correspond à votre recheche.")) {
return;
}
Pattern p = Pattern.compile("(<a href=\")([^\"]*)(\")");
Matcher m = p.matcher(page);
while(m.find()) {
String val = m.group(2);
/*String[] array = val.split(" ");
if (array.length == 0)
continue;
val = array[array.length-1].split("/")[0];
names += val+";";*/
if (val.length() >= 38 && val.substring(0, 38).equals("/fr/jcc-pokemon/cartes-pokemon/series/")) {
String[] array = val.split("/");
val = array[array.length-1];
//System.out.println(val);
if (names.contains(val)) {
next = false;
break;
}
names += val+";";
}
}
i++;
}
if (names.length() == 0)
return;
names = names.substring(0, names.length()-1);
System.out.println(names);
ReadWrite.write("D:/DataBases/Pokemon/CardNames/Pokemon_DataBase/"+collectionId+".txt", names);
}
public static void retrieveCollectionIds() throws IOException {
String url = "https://www.pokecardex.com/series";
String[] array = url.split("/");
String filePath = "./Data/Pokemon/HTML/Pokecardex_DataBase/collectionIds.txt";
String page = "";
if (!ReadWrite.exists(filePath)) {
page = DataScraper.downloadWebPage(url);
ReadWrite.write(filePath, page);
} else {
page = ReadWrite.readText(filePath);
}
Pattern p = Pattern.compile("(href=\")([^\"]*)(\")");
Matcher m = p.matcher(page);
String names = "";
while(m.find()) {
String val = m.group(2);
if (val.contains("/series/")) {
array = val.split("/");
names += array[array.length-1]+";";
System.out.println(array[array.length-1]);
}
}
System.out.println(names.split(";").length);
names = names.substring(0, names.length()-1);
ReadWrite.write("./Data/Pokemon/CardNames/Pokecardex_DataBase/collectionIds.txt", names);
}
public static void resize(String inputImagePath,
String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}
/**
* Resizes an image by a percentage of original size (proportional).
* @param inputImagePath Path of the original image
* @param outputImagePath Path to save the resized image
* @param percent a double number specifies percentage of the output image
* over the input image.
* @throws IOException
*/
public static void resize(String inputImagePath,
String outputImagePath, double percent) throws IOException {
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
int scaledWidth = (int) (inputImage.getWidth() * percent);
int scaledHeight = (int) (inputImage.getHeight() * percent);
resize(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
}
}