-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
145 lines (121 loc) · 5.17 KB
/
scrape.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/node --expose-gc
const fetch = require("node-fetch");
const request = require("request");
const fs = require("fs");
const ncp = require("ncp");
const asar = require("asar");
const archiver = require("archiver");
const exec = require("child_process").exec;
const branches = ["canary", "ptb", "stable"];
const platforms = ["win", "linux", "osx"];
console.log("loading config...");
var config = fs.existsSync("config.json")
? JSON.parse(fs.readFileSync("config.json").toString())
: {};
var lastver = {};
function patch(target, resolve) {
console.log("extracting zip...");
if (!fs.existsSync(target)) fs.mkdirSync(target);
console.log(`unzip -o ${target}.orig.zip -d ${target}`);
var child = exec(`unzip -o ${target}.orig.zip -d ${target}`);
child.stdout.pipe(process.stdout);
child.on("exit", () => {
path = target + "/core";
console.log("extracting asar...");
asar.extractAll(path + ".asar", path);
console.log("patching mainScreen.js...");
var mainScreen = fs
.readFileSync(path + "/app/mainScreen.js")
.toString();
mainScreen = mainScreen
.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 }));"
);
fs.writeFileSync(path + "/app/mainScreen.js", mainScreen);
console.log("patching mainScreenPreload.js...");
var mainScreenPreload = fs
.readFileSync(path + "/app/mainScreenPreload.js")
.toString();
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...");
ncp.ncp(__dirname + "/bspwn", path + "/app/bspwn", () => {
console.log("zipping it all up...");
var archive = archiver("zip");
var output = fs.createWriteStream(
(config.target || "./") + target + ".zip"
);
archive.pipe(output);
archive.directory(path, "core");
archive.file(__dirname + "/files/index.js", { name: "index.js" });
archive.file(target + "/package.json", { name: "package.json" });
archive.finalize();
//output.close(resolve);
resolve();
});
});
}
async function scrape() {
console.log("scraping...");
try {
for (b in branches) {
var branch = branches[b];
for (p in platforms) {
await new Promise(async (resolve, reject) => {
var platform = platforms[p];
var ident = `${branch}-${platform}`;
var currentver = await fetch(
`https://discordapp.com/api/updates/${branch}?platform=${platform}`
);
currentver = await currentver.json();
currentver = currentver.name;
var modules = await fetch(
`https://discordapp.com/api/modules/${branch}/versions.json?platform=${platform}&host_version=${currentver}`
);
modules = await modules.json();
var verstring = `${currentver}-${
modules.discord_desktop_core
}`;
console.log(`${ident} ${verstring}`);
if (!lastver[ident] || verstring > lastver[ident]) {
console.log("update available, fetching...");
lastver[ident] = verstring;
var target = `https://discordapp.com/api/modules/${branch}/discord_desktop_core/${
modules.discord_desktop_core
}?platform=${platform}&host_version=${currentver}`;
console.log(target);
request(
{
followAllRedirects: true,
url: target,
encoding: null
},
(err, response, body) => {
fs.writeFile(
"./" + ident + ".orig.zip",
body,
() => {
console.log("done downloading");
patch(ident, resolve);
}
);
}
);
} else resolve();
});
}
}
} catch (ex) {
console.log(ex);
}
console.log("scraping again in 15 minutes");
global.gc();
}
setInterval(scrape, 15 * 60 * 1000);
//setInterval(scrape, 60*1000);
scrape();