72 lines
No EOL
2.4 KiB
JavaScript
72 lines
No EOL
2.4 KiB
JavaScript
const fs = require('fs');
|
|
const stream = require('stream');
|
|
const crypto = require('crypto');
|
|
|
|
let outputFolder;
|
|
let stickersJsonUrl;
|
|
let stickerJsonBackupFilename;
|
|
let newUrlBasePath;
|
|
|
|
async function parseArguments() {
|
|
const args = process.argv.slice(2);
|
|
if (args.length != 4) {
|
|
console.log('usage: node index.js output-folder stickers-json-url sticker-json-backup-filename new-url-base-path')
|
|
throw new Error('wrong argument count');
|
|
}
|
|
outputFolder = args[0];
|
|
stickersJsonUrl = args[1];
|
|
stickerJsonBackupFilename = args[2];
|
|
newUrlBasePath = args[3];
|
|
}
|
|
|
|
async function downloadFile(url, path) {
|
|
const resp = await fetch(url);
|
|
return new Promise(resolve => {
|
|
const writer = fs.createWriteStream(path);
|
|
if (resp.ok && resp.body) {
|
|
stream.Readable.fromWeb(resp.body).pipe(writer);
|
|
} else {
|
|
throw new Error('unable to fetch stickers url: ' + stickersJsonUrl);
|
|
}
|
|
writer.on('finish', resolve);
|
|
});
|
|
}
|
|
|
|
async function processSticker(sticker) {
|
|
const sha1sum = crypto.createHash('sha1');
|
|
const filename = sha1sum.update(sticker.url).digest('hex');
|
|
let ending = '';
|
|
let arr = sticker.url.split('/').slice(-1)[0].split('.');
|
|
if (arr.length < 2) {
|
|
const resp = await fetch(sticker.url, {redirect: 'manual', method: 'HEAD'});
|
|
if (resp.headers.has('location')) {
|
|
arr = resp.headers.get('location').split('/').slice(-1)[0].split('.');
|
|
}
|
|
}
|
|
if (arr.length > 1) {
|
|
ending = '.' + arr.slice(-1)[0];
|
|
}
|
|
await downloadFile(sticker.url, outputFolder + '/data/' + filename + ending);
|
|
sticker.url = newUrlBasePath + '/data/' + filename + ending;
|
|
}
|
|
|
|
async function main() {
|
|
await parseArguments();
|
|
if(fs.existsSync(outputFolder)) {
|
|
fs.rmSync(outputFolder, { recursive: true, force: true });
|
|
}
|
|
fs.mkdirSync(outputFolder);
|
|
fs.mkdirSync(outputFolder + '/data');
|
|
await downloadFile(stickersJsonUrl, outputFolder + '/' + stickerJsonBackupFilename)
|
|
|
|
const data = JSON.parse(fs.readFileSync(outputFolder + '/' + stickerJsonBackupFilename));
|
|
|
|
await data.forEach(sticker => {
|
|
processSticker(sticker);
|
|
});
|
|
|
|
const dataString = JSON.stringify(data, null, 2); // todo: remove formatting
|
|
fs.writeFileSync(outputFolder + '/index.json', dataString);
|
|
}
|
|
|
|
main().catch(console.error); |