-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
389 lines (349 loc) · 11.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var request = require("request");
const NodeHelper = require("node_helper");
const Log = require("../../js/logger");
module.exports = NodeHelper.create({
start: function () {
this.debug("Starting node helper for: " + this.name);
this.instances = [];
this.peloton_api_url = "https://api.onepeloton.com/";
},
/**
* This node_helper keeps track of data per instance that
* is loaded on the front end. It does so by keeping track
* of instance data. This function will create a new instance
* keyed by the front end instance_identifier. It will store
* the instance config as part of this instance data along with
* other instance specific data accumulated via API calls.
*
* @param string instance_identifier
* @param object config
*
* @return void
*/
createInstance: function(instance_identifier, config) {
let instance = {};
instance.config = config;
instance.peloton_session_id = null;
instance.peloton_user = null;
instance.peloton_user_id = null;
this.setInstance(instance_identifier, instance);
},
/**
* Returns the data associated to the instance identified by
* instance_identifier.
*
* @param string instance_identifier
*
* @return object
*/
getInstance: function(instance_identifier) {
return this.instances[instance_identifier];
},
/**
* Helper function to relate the instance object to the instance
* identified by instance_identifier.
*
* @param string instance_identifier
* @param object instance
*
* @return void
*/
setInstance: function(instance_identifier, instance) {
this.instances[instance_identifier] = instance;
},
/**
* Helper function to get the config data from the instance
* identified by instance_identifier.
*
* @param string instance_identifier
*
* @return object
*/
getConfiguration: function(instance_identifier) {
return this.getInstance(instance_identifier).config;
},
/**
* Wrapper for Log.log function. This will use Log.log if the debug
* confirguration is true. This will prefix the name of the module
* and the instance identifier to the string_to_log.
*
* This expects string_to_log to be a string. If you have an object
* that you want to log you should try something like:
*
* this.debug(JSON.stringify(object, null, 2));
*
* instance_identifier is used to determine if the instance has been
* configured for debugging. If instance_identifier is not passed in
* then the default is to log the data.
*
* @param string string_to_log
* @param string|null instance_identifier
*
* @return void
*/
debug: function(string_to_log, instance_identifier = null) {
let should_log = instance_identifier === null || this.getConfiguration(instance_identifier).debug === true;
let prefix = "";
if (should_log) {
if (instance_identifier) {
prefix = "[" + this.name + ":" + instance_identifier + "] ";
} else {
prefix = "[" + this.name + "] ";
}
if (typeof Log !== "undefined" && typeof Log.log === "function") {
Log.log(prefix + string_to_log);
} else {
console.log(prefix + string_to_log);
}
}
},
/**
* This function will make it so that the socket notification names
* are guaranteed to be unique for our module. This makes it so
* that we do not have to worry about naming collisions with other
* modules.
*
* @param string notification
*
* @return string
*/
normalizeNotification: function(notification) {
return this.name + "_" + notification;
},
/**
* This function captures requests from the front end and determines
* if the request was meant for this node_helper. If it is then this
* will dispatch the appropriate calls.
*
* @param string notification
* @param object payload
*
* @return void
*/
socketNotificationReceived: function(notification, payload) {
this.debug("Received socket notification: " + notification);
switch (notification) {
case this.normalizeNotification("SET_CONFIG"):
this.createInstance(payload.instance_identifier, payload.config);
break;
case this.normalizeNotification("LOGIN"):
this.login(payload.instance_identifier);
break;
case this.normalizeNotification("REQUEST_USER"):
this.getUserData(payload.instance_identifier);
break;
case this.normalizeNotification("REQUEST_RECENT_WORKOUTS"):
this.getRecentWorkouts(payload.instance_identifier);
break;
case this.normalizeNotification("REQUEST_CHALLENGES"):
this.getChallenges(payload.instance_identifier);
break;
}
},
/**
* This will attempt to log in via the Peloton API with the credentials
* from the module config.
*
* This function doesn't return anything, however, depending on the API
* response it will send an appropriate socket notification to the front
* end.
*
* @param string istance_identifier
*
* @return void
*/
login: function(instance_identifier) {
let self = this;
let instance = this.getInstance(instance_identifier);
this.debug("Logging in as: " + instance.config.username, instance_identifier);
if (instance.peloton_session_id) {
this.debug("Already logged in", instance_identifier);
this.sendSocketNotification("USER_IS_LOGGED_IN", {
instance_identifier: instance_identifier
});
} else {
//User is not already logged in so we need to make an attempt here
instance.peloton_session_id = null;
instance.peloton_user_id = null;
instance.peloton_user = null;
this.setInstance(instance_identifier, instance);
request({
url: this.peloton_api_url + "auth/login",
headers: {
"User-Agent": "curl/7.64.0"
},
method: "POST",
json: true,
body: {
username_or_email: instance.config.username,
password: instance.config.password
}
}, function(error, response, body) {
self.debug("Received response for login request", instance_identifier);
if (error) {
self.debug(error, instance_identifier);
self.sendSocketNotification("FAILED_TO_LOG_IN", {
instance_identifier: instance_identifier
});
} else if (response.statusCode === 200) {
self.debug("Successfully logged in", instance_identifier);
instance.peloton_user_id = body.user_id;
instance.peloton_session_id = body.session_id;
self.setInstance(instance_identifier, instance);
self.sendSocketNotification("USER_IS_LOGGED_IN", {
instance_identifier: instance_identifier
});
} else {
self.debug("Failed to authenticate", instance_identifier);
self.sendSocketNotification("FAILED_TO_LOG_IN", {
instance_identifier: instance_identifier,
body: body
});
}
});
}
},
/**
* This will attempt to retrieve user data via the Peloton API.
*
* This function doesn't return anything, however, depending on the API
* response it will send an appropriate socket notification to the front
* end.
*
* @param string istance_identifier
*
* @return void
*/
getUserData: function(instance_identifier) {
let self = this;
let instance = this.getInstance(instance_identifier);
this.debug("About to fetch /api/me for: " + instance.peloton_session_id, instance_identifier);
request({
headers: {
Cookie: "peloton_session_id=" + instance.peloton_session_id + ";",
"peloton-platform": "web"
},
url: this.peloton_api_url + "api/me",
json: true
}, function(error, response, body) {
self.debug("Received response for api/me request", instance_identifier);
if (error) {
self.debug(error, instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_USER_DATA", {
instance_identifier: instance_identifier,
});
} else if (response.statusCode === 200) {
self.debug("Successfully retrieved user data", instance_identifier);
instance.peloton_user = body;
self.setInstance(instance_identifier, instance);
self.sendSocketNotification("RETRIEVED_USER_DATA", {
instance_identifier: instance_identifier,
peloton_user: instance.peloton_user
});
} else {
self.debug("Failed to receive data from api/me", instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_USER_DATA", {
instance_identifier: instance_identifier,
body: body
});
}
});
},
/**
* This will attempt to retrieve recent workouts via the Peloton API.
*
* This function doesn't return anything, however, depending on the API
* response it will send an appropriate socket notification to the front
* end.
*
* @param string istance_identifier
*
* @return void
*/
getRecentWorkouts: function(instance_identifier) {
let self = this;
let instance = this.getInstance(instance_identifier);
this.debug("About to fetch /api/user/" + instance.peloton_user_id + "/workouts", instance_identifier);
request({
headers: {
Cookie: "peloton_session_id=" + instance.peloton_session_id + ";",
"peloton-platform": "web"
},
url: this.peloton_api_url + "api/user/" + instance.peloton_user_id + "/workouts",
json: true,
qs: {
joins: "ride,ride.instructor",
limit: instance.config.recent_workouts_limit,
sort_by: "-created",
page: 0
}
}, function(error, response, body) {
self.debug("Received response for /api/user/" + instance.peloton_user_id + "/workouts request", instance_identifier);
if (error) {
self.debug(error, instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_RECENT_USER_WORKOUT_DATA", {
instance_identifier: instance_identifier,
});
} else if (response.statusCode === 200) {
self.debug("Successfully retrieved user workout data", instance_identifier);
self.sendSocketNotification("RETRIEVED_RECENT_WORKOUT_DATA", {
instance_identifier: instance_identifier,
body: body
});
} else {
self.debug("Failed to receive data from /api/user/" + instance.peloton_user_id + "/workouts", instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_RECENT_USER_WORKOUT_DATA", {
instance_identifier: instance_identifier,
body: body
});
}
});
},
/**
* This will attempt to retrieve user challenges via the Peloton API.
*
* This function doesn't return anything, however, depending on the API
* response it will send an appropriate socket notification to the front
* end.
*
* @param string istance_identifier
*
* @return void
*/
getChallenges: function(instance_identifier) {
let self = this;
let instance = this.getInstance(instance_identifier);
this.debug("About to fetch /api/user/" + instance.peloton_user_id + "/challenges/current", instance_identifier);
request({
headers: {
Cookie: "peloton_session_id=" + instance.peloton_session_id + ";",
"peloton-platform": "web"
},
url: this.peloton_api_url + "api/user/" + instance.peloton_user_id + "/challenges/current",
json: true,
qs: {
has_joined: true
}
}, function(error, response, body) {
self.debug("Received response for /api/user/" + instance.peloton_user_id + "/challenges/current request", instance_identifier);
if (error) {
self.debug(error, instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_CHALLENGE_DATA", {
instance_identifier: instance_identifier,
});
} else if (response.statusCode === 200) {
self.debug("Successfully retrieved user challenge data", instance_identifier);
self.sendSocketNotification("RETRIEVED_CHALLENGE_DATA", {
instance_identifier: instance_identifier,
body: body
});
} else {
self.debug("Failed to receive data from /api/user/" + instance.peloton_user_id + "/challenges/current", instance_identifier);
self.sendSocketNotification("FAILED_TO_RETRIEVE_CHALLENGE_DATA", {
instance_identifier: instance_identifier,
body: body
});
}
});
}
});