-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fetch.js
554 lines (527 loc) · 23.8 KB
/
Fetch.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
const request = require('request');
const htmlToText = require('html-to-text');
// These are constant, since they are required and unchanging properties of the client itself.
const CLIENT_ID = 'PLEASE PROVIDE -- SEE README FOR CLIENT SETUP INSTRUCTIONS';
const CLIENT_SECRET = 'PLEASE PROVIDE -- SEE README FOR CLIENT SETUP INSTRUCTIONS ';
const MAX = 1000;
class Fetch {
/**
* Creates fetcher object, generates access token options
* @param username login credential email id
* @param password login credential password
* @return none
*/
constructor(username, password) {
if (!username || !password) {
throw ('Invalid object initialization - must provide username and password');
}
this._username = username; //set immediately, since required for data fetching
this._password = password;
this._tokenOptions = null;
this._generateTokenOptions();
}
get username() { return this._username; }
get password() { return this._password; }
get tokenOptions() { return this._tokenOptions; }
set username(value) {
this._username = value;
}
set password(value) {
this._password = value;
}
/**
* Helper function for constructor that generates token options based on login credentials
* @param none
* @return none
*/
_generateTokenOptions() {
this._tokenOptions = {
url: 'https://login.windows.net/common/oauth2/token',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: `resource=https%3A%2F%2Fgraph.microsoft.com%2F&grant_type=password&username=${this.username}&password=${this.password}&audience=https://someapi.com/api&scope=openid&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`,
json: true };
}
/**
* Retrieves access token and fetches data from Outlook API
* @param callback callback function for desired data manipulation
* @return none
*/
logInCreateData(callback) {
let data = {};
let messages = [];
request(this.tokenOptions, function (error, response, tokenObj) {
if (tokenObj.error) {
console.log(tokenObj);
throw ('Error - Invalid login credentials');
}
var fetchOptions = { method: 'GET',
url: `https://outlook.office.com/api/v2.0/me/messages?$top=${MAX}`,
headers:
{ authorization: `Bearer ${tokenObj.access_token}`,
'content-type': 'application/json' },
json: true
};
request(fetchOptions, function (error, response, mailObj) {
if (error) throw new Error(error);
for (let i = (mailObj.value.length-1); i >= 0; i--) {
let message = {};
message['emailNumber'] = i+1;
message['folderId'] = mailObj.value[i].ParentFolderId;
message['timestamp'] = mailObj.value[i].ReceivedDateTime;
// handle to field
var to = [];
for (let x = 0; x < mailObj.value[i].ToRecipients.length; x++) {
to.push(mailObj.value[i].ToRecipients[x].EmailAddress.Address);
}
message['to'] = to;
// handle from and subject fields
if (mailObj.value[i].From !== undefined) {
message['from'] = mailObj.value[i].From.EmailAddress.Address;
} else {
message['from'] = 'draft';
}
var subject = mailObj.value[i].Subject;
if (subject === '') {subject = 'None';}
message['subject'] = subject;
// handle body field
var bodyhtml = mailObj.value[i].Body.Content;
var bodytext = htmlToText.fromString(bodyhtml, {
wordwrap: 130
});
bodytext = bodytext.split('From')[0];
if (bodytext === '') {bodytext = 'None';}
message['body'] = bodytext;
messages.push(message);
}
data.messages = messages;
fetchOptions['url'] = 'https://outlook.office.com/api/v2.0/me/MailFolders';
request(fetchOptions, function (error, response, folderObj) {
if (error) throw new Error(error);
let folders = [];
for (let i = 0; i < folderObj.value.length; i++) {
let folder = {};
folder.name = folderObj.value[i].DisplayName;
folder.id = folderObj.value[i].Id;
folders.push(folder);
}
data.folders = folders;
fetchOptions['url'] = 'https://outlook.office.com/api/v2.0/me';
request(fetchOptions, function (error, response, profileObj) {
if (error) throw new Error(error);
let profile = {};
profile['Display Name'] = profileObj.DisplayName;
profile['Email Address'] = profileObj.EmailAddress;
data.profile = profile;
callback(data);
});
});
});
});
}
/**
* @param data dictionary object created by logInCreateData
* @return array of folders in Outlook account
*/
folderList(data) {
return data.folders;
}
/**
* @param data dictionary object created by logInCreateData
* @return dictionary with name and email address associated with account
*/
profileInfo(data) {
return data.profile;
}
/**
* @param data dictionary object created by logInCreateData
* @returns int total amount of emails in account OR 1000 if API is maxed out
*/
totalQuantity(data) {
if (data.messages.length < MAX) {
return data.messages.length;
}
else {
console.log(`WARNING: totalQuantity is maxed out at ${MAX} messages`);
return MAX;
}
}
/**
* @param data dictionary object created by logInCreateData
* @return array of all mail in every folder
*/
allMail(data) {
return data.messages;
}
/**
* @param data dictionary object created by logInCreateData
* @param desiredFolder string containing folder to search in
* @return boolean if desired folder exists in Outlook folders list
*/
folderExists(data, desiredFolder) {
let folderIndex = -1;
for(let i = 0; i < data.folders.length; i++) {
if (desiredFolder === data.folders[i].name) {
folderIndex = i;
}
}
if (folderIndex === -1) {
throw ('Error - Invalid Folder Name');
}
return folderIndex;
}
/**
* @param data dictionary object created by logInCreateData
* @param desiredFolder string containing folder to search in
* @return int total count of folders associated with account
*/
folderQuantity(data, desiredFolder) {
let result = 0;
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
result++;
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param desiredFolder string containing folder to search in
* @return array of all mail that exists in specified folder
*/
folderMail(data, desiredFolder) {
let result = [];
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
result.push(data.messages[j]);
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param to string specifying email address in to field
* @param desiredFolder string containing folder to search in
* @return int of all mail to specific email in desired search folder
*/
toQuantity(data, to, desiredFolder) {
let result = 0;
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
for (let k = 0; k < data.messages[j].to.length; k++) {
if (data.messages[j].to[k].toUpperCase().trim() === to.toUpperCase().trim()) {
result++;
}
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param to string specifying email address in to field
* @param desiredFolder string containing folder to search in
* @return array of all mail objects to specific email in desired search folder
*/
toMail(data, to, desiredFolder) {
let result = [];
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
for (let k = 0; k < data.messages[j].to.length; k++) {
if (data.messages[j].to[k].toUpperCase().trim() === to.toUpperCase().trim()) {
result.push(data.messages[j]);
}
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param from string specifying email address in from field
* @param desiredFolder string containing folder to search in
* @return int of all mail from specific email in desired search folder
*/
fromQuantity(data, from, desiredFolder) {
let result = 0;
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
if (data.messages[j].from.toUpperCase().trim() === from.toUpperCase().trim()) {
result++;
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param from string specifying email address in from field
* @param desiredFolder string containing folder to search in
* @return array of all mail from specific email in desired search folder
*/
fromMail(data, from, desiredFolder) {
let result = [];
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
if (data.messages[j].from.toUpperCase().trim() === from.toUpperCase().trim()) {
result.push(data.messages[j]);
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param subject string specifying subject match; is not case or empty space sensitive
* @param desiredFolder string containing folder to search in
* @return int of all mail with specific subject name in desired search folder
*/
subjectQuantity(data, subject, desiredFolder) {
let result = 0;
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
if (data.messages[j].subject.toUpperCase().trim() === subject.toUpperCase().trim()) {
result++;
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param subject string specifying subject match; is not case or empty space sensitive
* @param desiredFolder string containing folder to search in
* @return array of all mail with specific subject name in desired search folder
*/
subjectMail(data, subject, desiredFolder) {
let result = [];
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
if (data.messages[j].subject.toUpperCase().trim() === subject.toUpperCase().trim()) {
result.push(data.messages[i]);
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param startDate string "YYYY-MM-DD"
* @param startTime string "HH:MM:SS"
* @param finishDate string "YYYY-MM-DD"
* @param finishTime string "HH:MM:SS"
* @param desiredFolder string containing folder to search in
* @return int total of emails received between desired time frames in desired search folder
*/
dateTimeQuantity(data, startDate, startTime, finishDate, finishTime, desiredFolder) {
if (!startDate || !startTime || !finishDate || !finishTime) {
throw ('Error - Invalid Date-Time Range');
}
let next = true;
let nexter = 0;
let question, middledate;
let start = `${startDate}T${startTime}Z`.split(/[-T:Z]/);
let finish = `${finishDate}T${finishTime}Z`.split(/[-T:Z]/);
let folderIndex = this.folderExists(data, desiredFolder);
for(let z = 0; z < data.messages.length; z++) {
if (data.messages[z].folderId === data.folders[folderIndex].id) {
next = false;
question = data.messages[z].timestamp.split(/[-T:Z]/);
middledate = false;
for (let x = 0; x < 3; x++) {
if(Number(question[x]) >= Number(start[x]) && Number(question[x]) <= Number(finish[x])){
middledate = true;
}
}
if (middledate = true) {
for (let j = 0; j < 3; j++){
if(Number(question[j]) > Number(start[j]) && Number(question[j]) < Number(finish[j])){
next = true;
}
else if(Number(question[j]) === Number(start[j]) && Number(question[j]) === Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
else if (Number(question[j]) === Number(start[j]) && Number(question[j]) < Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
else if (Number(question[j]) > Number(start[j]) && Number(question[j]) === Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
}
}
if(next){
nexter++;
}
}
}
return nexter;
}
/**
* @param data dictionary object created by logInCreateData
* @param startDate string "YYYY-MM-DD"
* @param startTime string "HH:MM:SS"
* @param finishDate string "YYYY-MM-DD"
* @param finishTime string "HH:MM:SS"
* @param desiredFolder string containing folder to search in
* @return array of emails received between desired time frames in desired search folder
*/
dateTimeMail(data, startDate, startTime, finishDate, finishTime, desiredFolder) {
if (!startDate || !startTime || !finishDate || !finishTime) {
throw ('Error - Invalid Date-Time Range');
}
let next = true;
let result = [];
let question, middledate;
let start = `${startDate}T${startTime}Z`.split(/[-T:Z]/);
let finish = `${finishDate}T${finishTime}Z`.split(/[-T:Z]/);
let folderIndex = this.folderExists(data, desiredFolder);
for(let z = 0; z < data.messages.length; z++) {
if (data.messages[z].folderId === data.folders[folderIndex].id) {
next = false;
question = data.messages[z].timestamp.split(/[-T:Z]/);
middledate = false;
for (let x = 0; x < 3; x++) {
if(Number(question[x]) >= Number(start[x]) && Number(question[x]) <= Number(finish[x])){
middledate = true;
}
}
if (middledate = true) {
for (let j = 0; j < 3; j++){
if(Number(question[j]) > Number(start[j]) && Number(question[j]) < Number(finish[j])){
next = true;
}
else if(Number(question[j]) === Number(start[j]) && Number(question[j]) === Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
else if (Number(question[j]) === Number(start[j]) && Number(question[j]) < Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
else if (Number(question[j]) > Number(start[j]) && Number(question[j]) === Number(finish[j])){
if(Number(question[3]) < Number(finish[3])){
next = true;
break;
}
else if(Number(question[3]) === Number(finish[3])){
if(Number(question[4]) < Number(finish[4])){
next = true;
break;
}
else if(Number(question[4]) === Number(finish[4])){
if(Number(question[5]) < Number(finish[5])){
next = true;
break;
}
}
}
}
}
}
if(next){
result.push(data.messages[z]);
}
}
}
return result;
}
/**
* @param data dictionary object created by logInCreateData
* @param str string containing search string
* @param desiredFolder string containing folder to search in
* @return array containing all emails with bodies that have the desired search string in them
*/
bodyContainsString(data, str, desiredFolder) {
let result = [];
let folderIndex = this.folderExists(data, desiredFolder);
for(let j = 0; j < data.messages.length; j++) {
if (data.messages[j].folderId === data.folders[folderIndex].id) {
if (data.messages[j].body.toUpperCase().trim().indexOf(str.toUpperCase().trim()) > -1) {
result.push(data.messages[j]);
}
}
}
return result;
}
}
module.exports = Fetch;