-
Notifications
You must be signed in to change notification settings - Fork 8
/
load-data.js
67 lines (54 loc) · 2.12 KB
/
load-data.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
const {Telegraf} = require("telegraf");
const {Model} = require("./src/model");
const csv = require('csv-parser')
const fs = require('fs')
const {Person} = require("./src/model");
require('dotenv').config();
async function load(path) {
const model = new Model()
const results = [];
return new Promise(resolve => {
fs.createReadStream(path)
.pipe(csv(["name", "roomNum", "intro"]))
.on('data', (data) => {
if (data.name === "Name") return
const newPerson = new Person();
for (const key in data) {
if (!data.hasOwnProperty(key) || key === "_") continue
switch (key) {
case "telegramId":
newPerson[key] = data[key].replace("@", "")
break
default:
newPerson[key] = data[key].trim()
}
}
results.push(newPerson);
})
.on('end', () => {
/* Randomize array in-place using Durstenfeld shuffle algorithm */
// function shuffleArray(array) {
// for (var i = array.length - 1; i > 0; i--) {
// var j = Math.floor(Math.random() * (i + 1));
// var temp = array[i];
// array[i] = array[j];
// array[j] = temp;
// }
// }
// shuffleArray(results)
const msg = results.map(p => p.name)
console.log(msg);
// const bot = new Telegraf(process.env.DEBUG_BOT_TOKEN);
//
// msg.slice(1,4).forEach(m => {
// bot.telegram.sendMessage(process.env.ME, m, {"parse_mode": "HTML"})
// })
results.forEach(p => model.addPerson(p))
model.generateUuids()
model.setupAMRefs()
model.saveToStorage()
resolve()
});
})
}
module.exports = {load}