-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_db_init.js
More file actions
77 lines (60 loc) · 2.07 KB
/
make_db_init.js
File metadata and controls
77 lines (60 loc) · 2.07 KB
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
import Loki from 'lokijs';
import fs from 'fs'
var db = new Loki('db.lokidb', {
autoload: true,
autoloadCallback: databaseInitialize,
});
function databaseInitialize() {
collectionInitialize("talentsIcon");
collectionInitialize("platforms").insert([{ "id": 0, "name": "Twitch" }, { "id": 1, "name": "Youtube" }]);
// kick off any program logic or start listening to external events
runProgramLogic();
}
function collectionInitialize(name, propObj) {
var entries = db.getCollection(name);
if (entries === null) {
entries = db.addCollection(name, propObj);
}
return entries;
}
function runProgramLogic() {
// Use fs to read '1.json' and parse it
fs.readFile('1.json', 'utf8', function (err, data) { // 1.json from https://schedule.hololive.tv/api/list/1
if (err) {
return console.log(err);
}
let json = JSON.parse(data);
// Get the 'talentsIcon' collection to add or update data
let talentsIconCollection = db.getCollection('talentsIcon');
json['dateGroupList'].forEach(element => {
let videoList = element['videoList'];
// Iterate through videoList and add or update name and talent.iconImageUrl to the talentsIcon collection
videoList.forEach(video => {
let existingEntry = talentsIconCollection.findOne({ name: video.name });
if (existingEntry) {
// Update the existing entry
if (existingEntry.iconImageUrl !== video.talent.iconImageUrl) {
existingEntry.iconImageUrl = video.talent.iconImageUrl;
talentsIconCollection.update(existingEntry);
console.log("updated", video.name)
}
} else {
// Insert a new entry
talentsIconCollection.insert({
name: video.name,
iconImageUrl: video.talent.iconImageUrl
});
console.log("inserted", video.name)
}
});
});
// Save the database
db.saveDatabase(err => {
if (err) {
console.log('Database save failed: ' + err);
} else {
console.log('Database saved.');
}
});
});
}