-
Notifications
You must be signed in to change notification settings - Fork 10
/
chat_importer.js
87 lines (74 loc) · 2.5 KB
/
chat_importer.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
//this is using telegrams export feature
const fs = require('fs');
var history = {};
var history_update = (chat_id, user_id) =>{
if (Object.keys(history).includes(chat_id.toString())){
//check user
if(Object.keys(history[chat_id.toString()]).includes(user_id.toString())){
// increase history count
history[chat_id.toString()][user_id.toString()] = history[chat_id.toString()][user_id.toString()] + 1;
}else{
//new user
history[chat_id.toString()][user_id.toString()] = 0;
}
}else{
//create new group
history[chat_id.toString()] = {};
history[chat_id.toString()][user_id.toString()] = 0
}
};
function read_results(){
try {
const file = fs.readFileSync('result.json');
data = JSON.parse(file.toString());
let chat_id = data.id;
let messages = data.messages;
console.log("chat id = ", chat_id,"\ntitle=",data.name, "\nmessages count =",messages.length);
for(let i=0;i<messages.length;i++){
if(messages[i].type === 'message'){
history_update(chat_id,messages[i].from_id);
}
}
try {
fs.writeFileSync('simple_history.json', JSON.stringify(history));
console.log("JSON data is saved.");
} catch (error) {
console.log("failed to save history");
console.error(error);
}
} catch (error) {
console.log("failed to read history");
console.error(error);
}
}
let sort = true;
function show_history(){
try {
const file = fs.readFileSync('simple_history.json');
history = JSON.parse(file.toString());
console.log("history is read");
console.log("chat id", Object.keys(history));
let chat_id = Object.keys(history)[0];
let messages = history[chat_id.toString()];
if(sort){
var sortable = [];
for (var count in messages) {
//console.log(count, " ", messages[count]);
sortable.push([count, messages[count]]);
}
sortable.sort(function(a, b) {
return b[1] - a[1];
})
sortable.forEach(function(item){
console.log(item);
})
}else{
console.log(JSON.stringify(history, null, 4));
}
} catch (error) {
console.log("failed to read history");
console.error(error);
}
}
//read_results();
show_history();