forked from inevitable-team/compiled-mcr-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataGather.js
69 lines (59 loc) · 2.68 KB
/
dataGather.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
let meetup = require("./sources/meetup"),
eventbrite = require("./sources/eventbrite"),
googleCalendar = require("./sources/googleCalendar"),
googleCalendarKey = "AIzaSyCR3-ptjHE-_douJsn8o20oRwkxt-zHStY"; // Calendar API Key gained from the Google Developer Console, this key is shown in many examples
class dataGather {
constructor() {
this.eventbrite = new eventbrite();
this.meetup = new meetup();
this.googleCalendar = new googleCalendar("[email protected]", "https://technw.uk/calendar", "TechNW", googleCalendarKey);
// this.sources = [this.eventbrite, this.meetup, this.googleCalendar];
this.sources = [this.eventbrite, this.meetup];
}
async getData() {
return new Promise(resolve => {
Promise.all(this.sources.map(api => api.getData())).then(data => {
console.table(data)
// Joining data from all sources
let groupsData = data.reduce((total, curr) => total.concat(curr[0]), []);
let eventsData = data.reduce((total, curr) => total.concat(curr[1]), []);
// Sorting / Filtering
let groups = this.sortNames(groupsData);
let events = this.sortTime(this.filterEvents(eventsData));
resolve([groups, events]);
})
});
}
sortTime(items) {
return items.sort((a, b) => new Date(a.startTimeISO) - new Date(b.startTimeISO));
}
sortNames(items) {
return items.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
return 0;
});
}
// https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript
filterEvents(events) {
return events.filter((event,index) => {
return index === events.findIndex(obj => {
return this.rtnLowercaseAlpha(event.name) == this.rtnLowercaseAlpha(obj.name) && this.formatDate(event.startTimeISO) == this.formatDate(obj.startTimeISO);
});
});
}
rtnLowercaseAlpha(string) {
return string.toLowerCase().replace(/[^a-z0-9]/gi,'');
}
// https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd
formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [day, month, year].join('-');
}
}
module.exports = dataGather;