-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
222 lines (189 loc) · 7.89 KB
/
index.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
var request = require('request');
var cheerio = require('cheerio');
var Pact = require('bluebird');
var _ = require('underscore');
var moment = require('moment');
var nodeRuntastic = {
_username: '',
_password: '',
_baseUrl: 'https://www.runtastic.com',
_loginUrl: '/en/d/users/sign_in.json',
_logoutUrl: '/en/d/users/sign_out',
_sessionsApiUrl: '/api/run_sessions/json',
_authenticityToken: '',
_cookieJar: null,
loggedIn: false,
_metaInfo: {
id: null,
username: '',
firstName: '',
lastName: '',
height: null,
weight: null
},
setup: function (username, password) {
var context = this;
return new Pact(function (resolve, reject) {
if (username !== '' && password !== '') {
context._username = username;
context._password = password;
context._login().then(function (metaInfo) {
resolve(metaInfo);
});
} else {
reject('No username or password supplied.')
}
});
},
getMetaInfos: function () {
if (this.loggedIn) {
return this._metaInfo;
} else {
return 'Not logged in, run setup Method.';
}
},
_login: function () {
var context = this;
return new Pact(function (resolve, reject) {
if (context._username !== '' && context._password !== '') {
context._cookieJar = request.jar();
request({
method: 'POST',
uri: context._baseUrl + context._loginUrl,
form: {
'user[email]': context._username,
'user[password]': context._password
},
jar: context._cookieJar
}, function (error, response, body) {
if (error) {
console.log(error);
reject();
}
if (response && response.statusCode === 200) {
body = JSON.parse(body);
var currentUser = body.current_user;
context._metaInfo.id = currentUser.id;
context._metaInfo.username = currentUser.slug;
context._metaInfo.firstName = currentUser.first_name;
context._metaInfo.lastName = currentUser.last_name;
context._metaInfo.height = currentUser.height;
context._metaInfo.weight = currentUser.weight;
var loadedDOM = cheerio.load(body.update);
context._authenticityToken = loadedDOM('input[name=authenticity_token]').val();
context.loggedIn = true;
resolve(context._metaInfo);
}
});
}
});
},
_getAllActivities: function () {
var context = this;
return new Pact(function (resolve, reject) {
if (!context.loggedIn) {
context.login().then(function (metaInfo) {
console.log(metaInfo);
context.getAllActivities();
});
} else {
request({
method: 'GET',
uri: context._baseUrl + '/en/users/' + context._metaInfo.username + '/sport-sessions',
jar: context._cookieJar
}, function (error, response, body) {
if (error) {
console.log(error);
reject();
}
if (response && response.statusCode === 200) {
var loadedDOM = cheerio.load(body);
//search for all scriptTags
var scriptTags = loadedDOM('script');
//search for scriptTag with the designated data
var dataTag = _.filter(scriptTags, function (scriptTag) {
if (scriptTag.children[0]) {
return scriptTag.children[0].data.indexOf('index_data') !== -1;
}
return false;
})[0];
//remove unessesary things and parse as json, so we have arrays
var data = JSON.parse(dataTag.children[0].data.substr(73).split(';')[0]);
//sort data by date asc
data = _.sortBy(data, '1');
resolve(data);
} else {
reject();
}
});
}
});
},
getActivities: function (month, year) {
var context = this;
if (month && month < 10) {
month = '0' + month.toString();
}
if (year && year < 1000) {
return 'Year should be bigger than 1000';
}
return new Pact(function (resolve, reject) {
if (context.loggedIn) {
context._getAllActivities().then(function (activities) {
var filteredActivities = _.filter(activities, function (activity) {
var activityDate = moment(activity[1]).format('YYYY-MM');
if (month && year) { //Use month and year to find entries
return activityDate == year.toString() + '-' + month.toString();
} else if (month) { //Use month in current year to find entries
return activityDate == moment().format('YYYY') + '-' + month.toString();
} else if (year) { //Use current year to find entries
return activityDate.substr(0, 4) == year;
}
});
request({
method: 'POST',
uri: context._baseUrl + context._sessionsApiUrl,
form: {
'user_id': context._metaInfo.id,
'items': filteredActivities.join(','),
'authenticity_token': context._authenticityToken
},
jar: context._cookieJar
}, function (error, response, body) {
if (error) {
console.log(error);
reject();
}
if (response && response.statusCode === 200) {
resolve(JSON.parse(body));
} else {
reject();
}
});
});
} else {
reject('Not logged in, run setup Method.');
}
});
},
getActivitiesWithFormatedDate: function (month, year) {
var context = this;
return new Pact(function (resolve, reject) {
context.getActivities(month, year).each(function (activity) {
var activityDate = activity.date;
activity.date =
activityDate.year + '-'
+ activityDate.month + '-'
+ activityDate.day + ' '
+ (activityDate.hour < 10 ? '0' + activityDate.hour : activityDate.hour) + ':'
+ (activityDate.minutes < 10 ? '0' + activityDate.minutes : activityDate.minutes) + ':'
+ (activityDate.seconds < 10 ? '0' + activityDate.seconds : activityDate.seconds);
}).then(function (activities) {
resolve(_.sortBy(activities, 'date'));
}).catch(function (e) {
reject(e);
});
});
}
};
module.exports = nodeRuntastic;