-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.js
236 lines (200 loc) · 7.58 KB
/
players.js
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
const axios = require("axios");
const axiosRetry = require("axios-retry");
const cheerio = require("cheerio");
const fs = require("fs");
axiosRetry(axios, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
const startIndex = 6001;
const maxTry = 7050;
const baseUrl = "https://sofifa.com/player/";
fs.readFile("check/missingIds.json", (err, data) => {
if (!err && data) {
const allIds = JSON.parse(data);
const players = [];
const errors = [];
let promises = [];
for (let i = startIndex; i <= maxTry; i++) {
const id = allIds[i];
promises.push(
axios(`${baseUrl}${id}`)
.then((response) => {
try {
const html = response.data;
const $ = cheerio.load(html);
const name = $(".info > h1").text();
const img = $(".bp3-card.player > img").attr("data-src");
const historyTable = $(
".card.double-spacing > table > tbody"
).children();
let history = [];
historyTable.each((i, el) => {
const season = $("td", el).first().text();
const teamId = $("td > a", el).attr("href").split("/")[2];
const teamName = $("td > a", el).first().text().trimStart();
const teamImg = $("td > a > figure > img", el).attr("data-src");
const historyObj = {
season: season,
teamId: teamId,
teamName: teamName,
teamImg: teamImg,
};
history.push(historyObj);
});
const result = {
playerName: name,
playerId: id,
playerImg: img,
history: history,
};
players.push(result);
console.log(`Found ${players.length} players`);
} catch (err) {
console.log(`${id}: Player does not exist`);
errors.push({
playerId: id,
error: "Player does not exist",
});
}
})
.catch((err) => {
let error;
if (err.request) {
const redirectedUrl = err.request?._currentUrl.toString();
if (redirectedUrl) {
promises.push(
axios(redirectedUrl)
.then((response) => {
try {
const html = response.data;
const $ = cheerio.load(html);
const name = $(".info > h1").text();
const img = $(".bp3-card.player > img").attr(
"data-src"
);
const historyTable = $(
".card.double-spacing > table > tbody"
).children();
let history = [];
historyTable.each((i, el) => {
const season = $("td", el).first().text();
const teamId = $("td > a", el)
.attr("href")
.split("/")[2];
const teamName = $("td > a", el)
.first()
.text()
.trimStart();
const teamImg = $("td > a > figure > img", el).attr(
"data-src"
);
const historyObj = {
season: season,
teamId: teamId,
teamName: teamName,
teamImg: teamImg,
};
history.push(historyObj);
});
const result = {
playerName: name,
playerId: id,
playerImg: img,
history: history,
};
players.push(result);
console.log(`Found ${players.length} players`);
} catch (err) {
console.log(`${id}: Player does not exist`);
errors.push({
playerId: id,
error: "Player does not exist",
});
}
})
.catch((err) => {
if (err.code === "ECONNRESET") {
error = "Connection reset by server";
} else if (err.request) {
error = `Tried to redirect to ${err.request._currentUrl.toString()}`;
} else if (err.response) {
error = err.response.status;
} else {
error = "Unknown error";
}
})
);
} else {
error = `Tried to redirect to ${redirectedUrl}`;
}
} else if (err.response) {
error = err.response.status;
} else if (err.code === "ECONNRESET") {
error = "Connection reset by server";
} else {
error = "Unknown error";
}
if (error) {
console.log(`${id}: ${error}`);
errors.push({
playerId: id,
error: error,
});
}
})
);
}
Promise.all(promises)
.then((responses) => {
console.log("All promises resolved");
try {
const subsetLength = 1000;
const subsets = Math.ceil(players.length / subsetLength);
for (let i = 0; i < subsets; i++) {
console.log(`Writing ${i + 1} of ${subsets}`);
const subsetStart = i * subsetLength;
const subsetEnd = subsetStart + subsetLength;
const playersSubset = players.slice(
subsetStart,
subsetEnd > players.length ? players.length : subsetEnd
);
const writeStream = fs.createWriteStream(
`output/missing_${startIndex + subsetStart}-${
startIndex + subsetStart + playersSubset.length - 1
}.json`
);
writeStream.write(JSON.stringify(playersSubset));
}
console.log(
`Found ${players.length}/${maxTry - startIndex + 1} players`
);
} catch (err) {
console.warn(err);
}
})
.catch((err) => {
console.warn(err);
try {
const subsetLength = 1000;
const subsets = Math.ceil(players.length / subsetLength);
for (let i = 0; i < subsets; i++) {
const subsetStart = i * subsetLength;
const subsetEnd = subsetStart + subsetLength;
const playersSubset = players.slice(
subsetStart,
subsetEnd > players.length ? players.length : subsetEnd
);
const writeStream = fs.createWriteStream(
`check/missing_${startIndex + subsetStart}-${
startIndex + subsetStart + playersSubset.length - 1
}.json`
);
writeStream.write(JSON.stringify(playersSubset));
}
console.log(
`Found ${players.length}/${maxTry - startIndex + 1} players`
);
} catch (err) {
console.warn(err);
}
});
}
});