-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (87 loc) · 3.57 KB
/
index.js
File metadata and controls
108 lines (87 loc) · 3.57 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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const readline = require('readline');
const BASE_TEAM_URL = "http://localhost:5000/api/v1/teams";
const BASE_PLAYER_URL = "http://localhost:5000/api/v1/players";
const REGIONS = ["na", "eu", "br", "ap", "kr", "ch", "jp", "lan", "las", "oce", "mn", "gc"];
const OUTPUT_DIR = path.join(__dirname, "dataset");
const TEAM_OUTPUT_FILE = path.join(OUTPUT_DIR, "allteams.json");
const PLAYER_OUTPUT_FILE = path.join(OUTPUT_DIR, "allplayers.json");
const LIMIT = 10;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function fetchAllTeams() {
let allTeams = [];
let skippedRegions = [];
for (const region of REGIONS) {
let page = 1;
while (true) {
const url = `${BASE_TEAM_URL}?page=${page}&limit=${LIMIT}®ion=${region}`;
try {
const response = await axios.get(url);
const data = response.data;
if (!data || !data.data || data.data.length === 0) break;
allTeams.push(...data.data);
console.log(`Fetched ${data.data.length} teams from region ${region}, page ${page}`);
if (!data.pagination.hasNextPage) break;
page++;
} catch (error) {
if (error.response && error.response.status === 400) {
console.warn(`Skipping region ${region} due to bad request (400)`);
skippedRegions.push(region);
break;
}
console.error(`Error fetching data for region ${region}, page ${page}:`, error.message);
break;
}
}
}
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
fs.writeFileSync(TEAM_OUTPUT_FILE, JSON.stringify(allTeams, null, 2));
console.log(`Saved all teams data to ${TEAM_OUTPUT_FILE}`);
if (skippedRegions.length > 0) {
console.log(`Skipped regions due to errors: ${skippedRegions.join(", ")}`);
}
}
async function fetchAllPlayers() {
let allPlayers = [];
let page = 1;
while (true) {
const url = `${BASE_PLAYER_URL}?page=${page}&limit=${LIMIT}`;
try {
const response = await axios.get(url);
const data = response.data;
if (!data || !data.data || data.data.length === 0) break;
allPlayers.push(...data.data);
console.log(`Fetched ${data.data.length} players from page ${page}`);
if (!data.pagination.hasNextPage) break;
page++;
} catch (error) {
console.error(`Error fetching players data, page ${page}:`, error.message);
break;
}
}
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
fs.writeFileSync(PLAYER_OUTPUT_FILE, JSON.stringify(allPlayers, null, 2));
console.log(`Saved all players data to ${PLAYER_OUTPUT_FILE}`);
}
function start() {
rl.question("What operation do you want to perform? (1: Fetch Teams, 2: Fetch Players): ", (answer) => {
if (answer === "1") {
fetchAllTeams().then(() => rl.close());
} else if (answer === "2") {
fetchAllPlayers().then(() => rl.close());
} else {
console.log("Invalid option. Please enter 1 or 2.");
rl.close();
}
});
}
start();