-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
66 lines (55 loc) · 1.89 KB
/
stats.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
const fs = require('fs');
// Path to your Telegram export
// Replace with your actual path
const filePath = '<YourFilePathHere>';
let rawdata = fs.readFileSync(filePath);
let telegramData = JSON.parse(rawdata);
let stats = {
dailyActivity: {},
userActivity: {},
activeHours: {},
activeDays: {},
totalUsers: 0,
};
telegramData.messages.forEach(message => {
let date = new Date(message.date);
let day = `${date.getUTCFullYear()}-${date.getUTCMonth() + 1}-${date.getUTCDate()}`;
let hour = date.getUTCHours();
let weekDay = date.getUTCDay();
// Update daily activity
if (day in stats.dailyActivity) {
stats.dailyActivity[day]++;
} else {
stats.dailyActivity[day] = 1;
}
// Update user activity
if (message.from in stats.userActivity) {
stats.userActivity[message.from]++;
} else {
stats.userActivity[message.from] = 1;
stats.totalUsers++;
}
// Update active hours
if (hour in stats.activeHours) {
stats.activeHours[hour]++;
} else {
stats.activeHours[hour] = 1;
}
// Update active days
if (weekDay in stats.activeDays) {
stats.activeDays[weekDay]++;
} else {
stats.activeDays[weekDay] = 1;
}
});
// Sort users by message count and get the top 20
let sortedUsers = Object.entries(stats.userActivity)
.sort((a, b) => b[1] - a[1])
.slice(0, 20);
stats.userActivity = Object.fromEntries(sortedUsers);
// Get the current date and time to create a timestamp
let timestamp = new Date();
let filename = `stats_${timestamp.getUTCFullYear()}-${timestamp.getUTCMonth() + 1}-${timestamp.getUTCDate()}_${timestamp.getUTCHours()}-${timestamp.getUTCMinutes()}-${timestamp.getUTCSeconds()}.json`;
// Write the stats to a new JSON file with the timestamp in the filename
// Replace with your actual path
fs.writeFileSync(`<YourFilePathHere>/${filename}`, JSON.stringify(stats));