This commit is contained in:
Cynthia Foxwell 2018-07-11 18:29:19 -06:00
parent 4fbee777b0
commit 6f7c92df74
1 changed files with 81 additions and 73 deletions

154
scrape.js
View File

@ -1,135 +1,143 @@
#!/usr/bin/node --expose-gc #!/usr/bin/node --expose-gc
const fetch = require('node-fetch'); const fetch = require("node-fetch");
const request = require('request'); const request = require("request");
const fs = require('fs'); const fs = require("fs");
const ncp = require('ncp'); const ncp = require("ncp");
const asar = require('asar'); const asar = require("asar");
const archiver = require('archiver'); const archiver = require("archiver");
const exec = require('child_process').exec; const exec = require("child_process").exec;
const branches = ['canary', 'ptb', 'stable']; const branches = ["canary", "ptb", "stable"];
const platforms = ['win', 'linux', 'osx']; const platforms = ["win", "linux", "osx"];
console.log('loading config...'); console.log("loading config...");
var config = fs.existsSync('config.json') ? var config = fs.existsSync("config.json")
JSON.parse(fs.readFileSync('config.json').toString()) : ? JSON.parse(fs.readFileSync("config.json").toString())
{}; : {};
var lastver = {}; var lastver = {};
function patch(target, resolve) { function patch(target, resolve) {
console.log("extracting zip...");
console.log('extracting zip...');
if (!fs.existsSync(target)) fs.mkdirSync(target); if (!fs.existsSync(target)) fs.mkdirSync(target);
console.log(`unzip -o ${target}.orig.zip -d ${target}`); console.log(`unzip -o ${target}.orig.zip -d ${target}`);
var child = exec(`unzip -o ${target}.orig.zip -d ${target}`); var child = exec(`unzip -o ${target}.orig.zip -d ${target}`);
child.stdout.pipe(process.stdout); child.stdout.pipe(process.stdout);
child.on('exit', () => { child.on("exit", () => {
path = target + "/core";
path = target + '/core'; console.log("extracting asar...");
asar.extractAll(path + ".asar", path);
console.log('extracting asar...'); console.log("patching mainScreen.js...");
asar.extractAll(path + '.asar', path); var mainScreen = fs
.readFileSync(path + "/app/mainScreen.js")
console.log('patching mainScreen.js...'); .toString();
var mainScreen = fs.readFileSync(path + '/app/mainScreen.js').toString();
mainScreen = mainScreen mainScreen = mainScreen
.replace(/nodeIntegration: false,/, 'nodeIntegration: true,') .replace(/nodeIntegration: false,/, "nodeIntegration: true,")
.replace(/mainWindow\.loadURL\(URL_TO_LOAD\);/, "mainWindow.loadURL(_url.format({ pathname: _path2.default.join(__dirname, 'bspwn/updater.html'), protocol: 'file:', slashes: true }));"); .replace(
fs.writeFileSync(path + '/app/mainScreen.js', mainScreen); /mainWindow\.loadURL\(URL_TO_LOAD\);/,
"mainWindow.loadURL(_url.format({ pathname: _path2.default.join(__dirname, 'bspwn/updater.html'), protocol: 'file:', slashes: true }));"
);
fs.writeFileSync(path + "/app/mainScreen.js", mainScreen);
console.log('patching mainScreenPreload.js...'); console.log("patching mainScreenPreload.js...");
var mainScreenPreload = fs.readFileSync(path + '/app/mainScreenPreload.js').toString(); var mainScreenPreload = fs
mainScreenPreload = mainScreenPreload .readFileSync(path + "/app/mainScreenPreload.js")
.replace(/var electron = require\('electron'\);/, "var bspwn = require('./bspwn');\nbspwn.go();\nvar electron = require('electron');"); .toString();
fs.writeFileSync(path + '/app/mainScreenPreload.js', mainScreenPreload); mainScreenPreload = mainScreenPreload.replace(
/var electron = require\('electron'\);/,
"var bspwn = require('./bspwn');\nbspwn.go();\nvar electron = require('electron');"
);
fs.writeFileSync(path + "/app/mainScreenPreload.js", mainScreenPreload);
console.log('copying bspwn files...') console.log("copying bspwn files...");
ncp.ncp(__dirname + '/bspwn', path + '/app/bspwn', () => { ncp.ncp(__dirname + "/bspwn", path + "/app/bspwn", () => {
console.log("zipping it all up...");
var archive = archiver("zip");
console.log('zipping it all up...'); var output = fs.createWriteStream(
var archive = archiver('zip'); (config.target || "./out/") + target + ".zip"
);
var output = fs.createWriteStream((config.target || './') + target + '.zip');
archive.pipe(output); archive.pipe(output);
archive.directory(path, 'core'); archive.directory(path, "core");
archive.file(__dirname + '/files/index.js', { name: 'index.js' }); archive.file(__dirname + "/files/index.js", { name: "index.js" });
archive.file(target + '/package.json', { name: 'package.json' }); archive.file(target + "/package.json", { name: "package.json" });
archive.finalize(); archive.finalize();
//output.close(resolve); //output.close(resolve);
resolve(); resolve();
}); });
}); });
} }
async function scrape() { async function scrape() {
console.log("scraping...");
console.log('scraping...');
try { try {
for (b in branches) { for (b in branches) {
var branch = branches[b]; var branch = branches[b];
for (p in platforms) { for (p in platforms) {
await new Promise(async (resolve, reject) => { await new Promise(async (resolve, reject) => {
var platform = platforms[p]; var platform = platforms[p];
var ident = `${branch}-${platform}`; var ident = `${branch}-${platform}`;
var currentver = await fetch(`https://discordapp.com/api/updates/${branch}?platform=${platform}`); var currentver = await fetch(
`https://discordapp.com/api/updates/${branch}?platform=${platform}`
);
currentver = await currentver.json(); currentver = await currentver.json();
currentver = currentver.name; currentver = currentver.name;
var modules = await fetch(`https://discordapp.com/api/modules/${branch}/versions.json?platform=${platform}&host_version=${currentver}`); var modules = await fetch(
`https://discordapp.com/api/modules/${branch}/versions.json?platform=${platform}&host_version=${currentver}`
);
modules = await modules.json(); modules = await modules.json();
var verstring = `${currentver}-${modules.discord_desktop_core}`; var verstring = `${currentver}-${
modules.discord_desktop_core
}`;
console.log(`${ident} ${verstring}`); console.log(`${ident} ${verstring}`);
if (!lastver[ident] || verstring > lastver[ident]) { if (!lastver[ident] || verstring > lastver[ident]) {
console.log('update available, fetching...'); console.log("update available, fetching...");
lastver[ident] = verstring; lastver[ident] = verstring;
var target = `https://discordapp.com/api/modules/${branch}/discord_desktop_core/${modules.discord_desktop_core}?platform=${platform}&host_version=${currentver}`; var target = `https://discordapp.com/api/modules/${branch}/discord_desktop_core/${
modules.discord_desktop_core
}?platform=${platform}&host_version=${currentver}`;
console.log(target); console.log(target);
request({ request(
followAllRedirects: true, {
url: target, followAllRedirects: true,
encoding: null url: target,
}, (err, response, body) => { encoding: null
fs.writeFile(ident + '.orig.zip', body, () => { },
console.log('done downloading'); (err, response, body) => {
patch(ident, resolve); fs.writeFile(
}) "./out/" + ident + ".orig.zip",
}); body,
} () => {
else resolve(); console.log("done downloading");
patch(ident, resolve);
}
);
}
);
} else resolve();
}); });
} }
} }
} catch (ex) {
}
catch (ex) {
console.log(ex); console.log(ex);
} }
console.log('scraping again in 15 minutes'); console.log("scraping again in 15 minutes");
global.gc(); global.gc();
} }
setInterval(scrape, 15 * 60 * 1000); setInterval(scrape, 15 * 60 * 1000);