-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl.js
123 lines (110 loc) · 3.83 KB
/
dl.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
const https = require('https');
const fs = require('fs');
const path = require('path');
// Function to download a file
function download(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
console.log("Downloading:",dest);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close(resolve);
});
}).on('error', (err) => {
fs.unlink(dest, () => reject(err.message));
});
});
}
// Function to fetch the torch URL
function fetchTorchUrl() {
return new Promise((resolve, reject) => {
https.get('https://www.chess.com/analysis?tab=analysis', (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
const torchUrlMatch = data.match(/([^']*torch-2[^']*)/);
if (torchUrlMatch) {
resolve(`https://www.chess.com${torchUrlMatch[0]}`);
} else {
reject('Torch URL not found.');
}
});
}).on('error', (err) => {
reject(err.message);
});
});
}
// Function to fetch the number of parts
function fetchPartsCount(fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
reject(err.message);
return;
}
const partsMatch = data.match(/enginePartsCount\s*=\s*(\d+)/);
if (partsMatch) {
// Skip the first 3 lines
data = data.split('\n').slice(3).join('\n');
// Save the modified content back to the file
fs.writeFile(fileName, data, 'utf8', (err) => {
if (err) {
reject(err.message);
return;
}
resolve(parseInt(partsMatch[1]) - 1);
});
} else {
reject('Parts count not found.');
}
});
});
}
// Function to combine parts into a single file
function combineParts(parts, outputFileName) {
const writeStream = fs.createWriteStream(outputFileName);
parts.forEach((partFileName) => {
const data = fs.readFileSync(partFileName);
writeStream.write(data);
});
writeStream.end();
}
// Function to delete part files
function deleteParts(parts) {
parts.forEach((partFileName) => {
fs.unlinkSync(partFileName);
});
}
// Main logic
(async () => {
try {
const torchUrl = await fetchTorchUrl();
const fileName = path.basename(torchUrl);
// Download the torch file
await download(torchUrl, "torch-2.js");
// Fetch the number of parts
const parts = await fetchPartsCount("torch-2.js");
// Download each part in parallel
const downloadPromises = [];
const partFileNames = [];
for (let i = 0; i <= parts; i++) {
const partUrl = `${torchUrl.slice(0, -3)}-part-${i}.wasm`;
const partFileName = path.basename(partUrl);
partFileNames.push(partFileName);
downloadPromises.push(download(partUrl, partFileName));
}
// Wait for all downloads to complete
await Promise.all(downloadPromises);
console.log('All parts downloaded successfully.');
// Combine parts into a single file
combineParts(partFileNames, 'torch-2.wasm');
console.log('Parts combined into torch-2.wasm successfully.');
deleteParts(partFileNames);
console.log('Part files deleted successfully.');
} catch (err) {
console.error(`Error: ${err}`);
}
})();