-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
225 lines (186 loc) · 6.29 KB
/
node_helper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const NodeHelper = require("node_helper");
const moment = require("moment");
const path = require("path");
const fs = require("fs/promises");
module.exports = NodeHelper.create({
configured: false,
start: function() {},
socketNotificationReceived: async function(notification, payload) {
let self = this;
if (notification == "CONFIG") {
let dataFolder = path.resolve(__dirname, "data", payload.steamId);
if (!self.configured) {
try {
await fs.mkdir(dataFolder, { recursive: true });
} catch (e) {
console.log("Failed to create directory " + dataFolder);
}
let callback = async function() {
await self.updateData(dataFolder, payload.apiKey, payload.steamId);
let data = await self.loadCachedData(dataFolder, payload.excludeGames);
self.sendResult(data, payload.steamId, payload.daysCount, payload.gamesCount);
};
self.scheduleNextUpdate(payload.updateTime, callback);
self.configured = true;
}
let data = await self.loadCachedData(dataFolder, payload.excludeGames);
self.sendResult(data, payload.steamId, payload.daysCount, payload.gamesCount);
}
},
scheduleNextUpdate: function(updateTime, callback) {
var self = this;
var date = self.calculateNextUpdate(updateTime);
var timeout = date.valueOf() - moment().valueOf();
console.log("Next update at " + date.format("YYYY-MM-DD HH:mm:ss"));
setTimeout(async function() {
await callback();
self.scheduleNextUpdate(updateTime, callback);
}, timeout);
},
loadCachedData: async function(dataFolder, excludeAppIds) {
var data = {};
let files = await fs.readdir(dataFolder);
for (let file of files) {
try {
var json = JSON.parse(await fs.readFile(path.resolve(dataFolder, file)));
json.response.games.forEach(function(game) {
if (excludeAppIds.indexOf(game.appid) >= 0) {
return;
}
if (!(game.appid in data)) {
data[game.appid] = {
icon: "http://media.steampowered.com/steamcommunity/public/images/apps/" + game.appid + "/" + game.img_icon_url + ".jpg",
total: {}
}
}
data[game.appid].total[json.date] = game.playtime_forever;
});
} catch (e) {
console.log("Could not load data from " + file + ", error: " + e);
}
};
return data;
},
sendResult: function(data, steamId, daysCount, gamesCount) {
let self = this;
let calculator = new PlaytimeCalculator(data, self.key);
let result = self.buildResult(calculator, daysCount, gamesCount);
self.sendSocketNotification("PLAYTIME", {
playtime : result,
steamId : steamId,
days : calculator.getUniqueDaysCount(),
games : calculator.getUniqueGamesCount()
});
},
buildResult: function(calculator, daysCount, gamesCount) {
let self = this;
let result = {};
var date = moment().subtract(1, "days").startOf("day");
for (let i = 0; i < daysCount; i++) {
let previousDate = date.clone().subtract(1, 'days');
result[self.key(date)] = calculator.getAllPlaytime(date, gamesCount);
date = previousDate;
}
return result;
},
updateData: async function(dataFolder, apiKey, steamId) {
let self = this;
let data = await self.fetchData(apiKey, steamId);
let forDate = self.key(moment().subtract(1, 'days'));
let fileName = forDate + ".json";
let filePath = path.resolve(dataFolder, fileName);
data.date = forDate;
try {
await fs.writeFile(filePath, JSON.stringify(data));
console.log(filePath + " written");
} catch (err) {
self.sendSocketNotification("PLAYTIME_UPDATE_ERROR", "Could not write file " + filePath);
};
},
fetchData: async function(apiKey, steamId) {
let self = this;
try {
let response = await fetch("http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=" + apiKey + "&steamid=" + steamId + "&format=json",
{
method : "GET",
headers : {
"User-Agent" : "MagicMirror/MMM-SteamPlaytime/1.0; (https://github.com/buxxi/MMM-SteamPlaytime)"
}
}
);
if (response.status != 200) {
throw new Error(response.status + ": " + response.statusText);
}
return await response.json();
} catch(err) {
self.sendSocketNotification("PLAYTIME_UPDATE_ERROR", "Got " + err.message + " from API");
};
},
key: function(date) {
return date.format("YYYY-MM-DD");
},
calculateNextUpdate(updateTime) {
let date = moment();
updateTime = updateTime.split(":").map(function(e) { return parseInt(e)});
date.set({
hour: updateTime[0],
minute: updateTime[1],
second: 0
});
if (date.isSameOrBefore(moment())) {
date.add(1, 'day');
}
return date;
}
});
class PlaytimeCalculator {
constructor(data, dateKeyFormatter) {
this.data = data;
this.dateKeyFormatter = dateKeyFormatter;
this.firstDate = this.getFirstDate(data);
}
getAllPlaytime(date, gamesCount) {
var result = [];
for (let appid in this.data) {
//Find the playtime for that current date, if not found the json-file is probably missing for that day, no need to backtrack further
let dateTotalTime = this.getGameTotalTime(appid, date, date);
if (dateTotalTime > 0) {
//If the current day has any playtime backtrack possibly all the way to the first day that has the data set to calculate the delta
let previousDateTotalTime = this.getGameTotalTime(appid, date.clone().subtract(1, 'days'), this.firstDate);
let time = dateTotalTime - previousDateTotalTime;
if (time !== 0) {
result.push({
appid : appid,
icon: this.data[appid].icon,
time: time
})
}
}
}
return result.sort((a, b) => b.time - a.time).slice(0, gamesCount);
}
getGameTotalTime(appid, date, stopAtDate) {
let key = this.dateKeyFormatter(date);
if (key in this.data[appid].total) {
return this.data[appid].total[key];
}
if (stopAtDate.isBefore(date)) {
return this.getGameTotalTime(appid, date.clone().subtract(1, 'days'), stopAtDate);
} else {
return 0;
}
}
getFirstDate(data) {
return moment(Object.values(data).flatMap(e => Object.keys(e.total)).reduce((a, b) => a < b ? a : b, moment()));
}
getUniqueDaysCount() {
return new Set(Object.values(this.data).flatMap(e => Object.keys(e.total))).size;
}
getUniqueGamesCount() {
return Object.values(this.data).filter(game => {
let min = Math.min(...Object.values(game.total));
let max = Math.max(...Object.values(game.total));
return min != max;
}).length;
}
}