forked from SMarioMan/craig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-convert.js
105 lines (96 loc) · 3 KB
/
json-convert.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
const fs = require("fs");
const sqlite3 = require("better-sqlite3");
const db = new sqlite3("craig.db");
db.pragma("journal_mode = WAL");
/*
// craig-guild-membership-status -> guildMembershipStatus
const gmsLog = JSON.parse("[" + fs.readFileSync("craig-guild-membership-status.json", "utf8") + "]");
const gms = gmsLog[0];
for (var i = 1; i < gmsLog.length; i++) {
var step = gmsLog[i];
if (!("v" in step))
delete gms[step.k];
else
gms[step.k] = step.v;
}
const gmsStmt = db.prepare("INSERT INTO guildMembershipStatus (id, refreshed) VALUES (@id, @refreshed)");
for (var id in gms) {
var r = gms[id];
gmsStmt.run({id: id, refreshed: r});
}
// craig-auto -> auto
const autoLog = JSON.parse("[" + fs.readFileSync("craig-auto.json", "utf8") + "]");
const auto = autoLog[0];
for (var i = 1; i < autoLog.length; i++) {
var step = autoLog[i];
if ("t" in step) {
if (!(step.u in auto))
auto[step.u] = [];
auto[step.u].push(step);
} else {
if (!(step.u in auto))
auto[step.u] = [];
var ua = auto[step.u];
for (var ui = 0; ui < ua.length; ui++) {
var e = ua[ui];
if (e.g === step.g && e.c === step.c) {
ua.splice(ui, 1);
break;
}
}
}
}
const autoStmt = db.prepare("INSERT INTO auto (uid, gid, cid, tids) VALUES (@uid, @gid, @cid, @tids)");
for (var uid in auto) {
auto[uid].forEach((el) => {
var tids = [];
for (var tid in el.t)
tids.push(tid);
tids = tids.join(",");
autoStmt.run({
uid: el.u,
gid: el.g,
cid: el.c,
tids: tids
});
});
}
// craig-bans -> bans
const banLog = JSON.parse("[" + fs.readFileSync("craig-bans.json", "utf8") + "]");
const bans = banLog[0];
for (var i = 1; i < banLog.length; i++) {
var step = banLog[i];
if ("u" in step) {
bans[step.i] = step.u;
} else {
delete bans[step.i];
}
}
const banStmt = db.prepare("INSERT INTO bans (id, name) VALUES (@id, @name)");
for (var id in bans) {
banStmt.run({id:id, name:bans[id]});
}
// craig-bless -> blessings
const blessLog = JSON.parse("[" + fs.readFileSync("craig-bless.json", "utf8") + "]");
const blessings = blessLog[0];
for (var i = 1; i < blessLog.length; i++) {
var step = blessLog[i];
if ("g" in step) {
blessings[step.u] = step.g;
} else {
delete blessings[step.u];
}
}
const blessStmt = db.prepare("INSERT INTO blessings (uid, gid) VALUES (@uid, @gid)");
for (var uid in blessings) {
blessStmt.run({uid:uid, gid:blessings[uid]});
}
*/
// *-credentials.json -> drive
const credFiles = fs.readdirSync("/home/yahweasel/craig-drive");
const credFileRE = /^([0-9]*)-credentials\.json$/;
credFiles.forEach((credFile) => {
var cp = credFileRE.exec(credFile);
if (cp === null) return;
db.prepare("INSERT INTO drive (id, data) VALUES (?, ?)").run(cp[1], fs.readFileSync("/home/yahweasel/craig-drive/" + credFile));
});