-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnectDB.js
46 lines (43 loc) · 1.34 KB
/
connectDB.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
const {jianpu_to_pitch} = require('./jianpuAlgo');
const { getAllRecentSongs } = require('./mongo/songs');
require('dotenv').config();
let jianpuDB = {};
let lastDate = new Date(0);
let firstTimeInit = true;
function initDB() {
return getAllRecentSongs(lastDate).then(results => {
results.forEach(row => {
let song = {
name: row.name,
id: row.id,
singer: row.singer,
language: row.language,
jianpu: row.jianpu,
creation_time: row.creation_time,
modify_time: row.modify_time,
rev: row.rev,
};
if (row.jianpu) {
let {pitch, duration} = jianpu_to_pitch(row.jianpu);
song.pitch = pitch;
song.duration = duration;
}
jianpuDB[row.id] = song;
if (song.modify_time > lastDate) {
lastDate = song.modify_time;
}
});
if (firstTimeInit) {
console.log('[initDB] found %d songs in mongodb', results.length);
firstTimeInit = false;
}
}).catch((err) => {
console.log('[initDB]', err);
}).finally(() => {
setTimeout(initDB, Math.random() * 30000 + 30000);
});
}
module.exports = {
jianpuDB: jianpuDB,
initDB
};