-
Notifications
You must be signed in to change notification settings - Fork 12
/
update-database.js
58 lines (56 loc) · 1.92 KB
/
update-database.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
const pokaLog = require("./log"); // 可愛控制台輸出
const Pin = require('./db/pin')
const Playlist = require('./db/playlist')
const Record = require('./db/record')
const encodeBase64 = require('./dataModule/cryptoUtils').encodeBase64
function deReq(x) {
const b2a = x => Buffer.from(x, "base64").toString("utf8");
const decode = x => /(.{5})(.+)3C4C7CB3(.+)/.exec(x);
let [_, rand, link, checkSum] = decode(x);
[_, rand, link, checkSum] = [_, rand, b2a(link), b2a(checkSum)];
if (!Number.isInteger(Math.log10(rand.charCodeAt(0) + checkSum.charCodeAt(0)))) {
return false;
}
return link;
}
async function updateDatabase() {
Pin.model.find({}).then(async pins => {
let count = 0
for (let pin of pins) {
if (pin.type == 'album' && pin.source == 'DSM' && !pin.id.match(/^Wy/)) {
pin.id = encodeBase64(JSON.stringify(Object.values(JSON.parse(pin.id))))
await pin.save()
count++
}
}
if (count)
pokaLog.logDB('UPDATE', `${count} pins updated`)
})
Playlist.model.find({}).then(async playlists => {
let count = 0
for (let playlist of playlists) {
for (let song of playlist.songs) {
if (song.source == 'DSM' && song.albumId && !song.albumId.match(/^Wy/)) {
song.albumId = encodeBase64(JSON.stringify(Object.values(JSON.parse(song.albumId))))
count++
}
}
await playlist.save()
}
if (count)
pokaLog.logDB('UPDATE', `${count} playlist songs updated`)
})
Record.model.find({}).then(async records => {
let count = 0
for (let record of records) {
if (record.source == 'DSM' && record.albumId && !record.albumId.match(/^Wy/)) {
record.albumId = encodeBase64(JSON.stringify(Object.values(JSON.parse(record.albumId))))
await record.save()
count++
}
}
if (count)
pokaLog.logDB('UPDATE', `${count} records updated`)
})
}
module.exports = updateDatabase