Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
0wwafa authored Aug 9, 2024
0 parents commit 4f70b43
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
123 changes: 123 additions & 0 deletions dl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}`);
}
})();
Binary file added torch-2
Binary file not shown.
81 changes: 81 additions & 0 deletions torch-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <limits.h>
#endif

int main(int argc, char *argv[]) {
// Get the path of the current executable
char exePath[1024];
#ifdef _WIN32
GetModuleFileName(NULL, exePath, sizeof(exePath));
#else
ssize_t count = readlink("/proc/self/exe", exePath, sizeof(exePath) - 1);
if (count != -1) {
exePath[count] = '\0';
} else {
perror("readlink");
return 1;
}
#endif

// Remove the executable name from the path to get the directory
char *lastSlash = strrchr(exePath, '/');
#ifdef _WIN32
if (!lastSlash) {
lastSlash = strrchr(exePath, '\\');
}
#endif
char exeName[256];
if (lastSlash) {
strcpy(exeName, lastSlash + 1);
*lastSlash = '\0';
} else {
strcpy(exeName, exePath);
}

// Remove the extension from the executable name
char *dot = strrchr(exeName, '.');
if (dot) {
*dot = '\0';
}

// Declare the command variable
char command[2048];

// Construct the command string with the correct path separator
#ifdef _WIN32
snprintf(command, sizeof(command), "node %s\\%s.js", exePath, exeName);
#else
snprintf(command, sizeof(command), "node %s/%s.js", exePath, exeName);
#endif
for (int i = 1; i < argc; i++) {
strcat(command, " ");
strcat(command, argv[i]);
}

// Open log.txt in append mode
/*
FILE *logFile = fopen("log.txt", "a");
if (logFile == NULL) {
perror("fopen");
return 1;
}
fprintf(logFile, "%s\n", command);
fclose(logFile);
*/

// Execute the command
int result = system(command);
if (result == -1) {
perror("system");
return 1;
}

return 0;
}
Binary file added torch-2.exe
Binary file not shown.

0 comments on commit 4f70b43

Please sign in to comment.