-
Notifications
You must be signed in to change notification settings - Fork 4
/
MMM-Events.js
218 lines (175 loc) · 7.14 KB
/
MMM-Events.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
/* Magic Mirror
* Module: MMM-Events
*
* By Mykle1 - Brometheus #1 consultant and all around good dude!
*
*/
Module.register("MMM-Events", {
// Module config defaults.
defaults: {
city: "", // Your City or lat,long ex "40.123456,-74.123456",
radius: "", // search radius
radiusDistance: "", // mi or km
eventType: "", // See Events List in ReadMe
when: "", // "All", "Future", "Past", "Today", "Last Week", "This Week", "Next week", and months by name
mode: "", // Frame or noFrame (around picture)
apikey: "Your FREE API Key Goes Here",
rotateInterval: 5 * 60 * 1000, // New Event Appears
useHeader: false,
header: "",
maxWidth: "195px", // adjust to your liking
animationSpeed: 3000, // Event fades in and out
initialLoadDelay: 3750,
retryDelay: 2500,
updateInterval: 60 * 60 * 1000, // 60 minutes. No need to change!
picture: true, // true, false = no picture
},
getStyles: function() {
return ["MMM-Events.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Set locale.
this.url = this.getEventsUrl();
this.event = {};
this.activeItem = 0;
this.rotateInterval = null;
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Upcoming Events...";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright");
if (this.config.header) header.innerHTML = this.config.header;
else header.innerHTML = "Events for " + this.config.city;
wrapper.appendChild(header);
}
var keys = Object.keys(this.event);
if (keys.length > 0) {
if (this.activeItem >= keys.length) {
this.activeItem = 0;
}
var events = this.event[keys[this.activeItem]];
var top = document.createElement("div");
top.classList.add("list-row");
var eventsDate1 = document.createElement("div");
eventsDate1.classList.add("small", "bright");
eventsDate1.innerHTML = events.title;
wrapper.appendChild(eventsDate1);
if (this.config.picture === true) {
var eventsLogo = document.createElement("div");
var eventsIcon = document.createElement("img");
eventsIcon.classList.add("list-left", "photo");
if (this.config.mode === "noFrame") {
if (events.image != null) {
eventsIcon.src = events.image.perspectivecrop176by124.url;
} else {
eventsIcon.src = "modules/MMM-Events/icons/go.jpg";
}
eventsLogo.appendChild(eventsIcon);
wrapper.appendChild(eventsLogo);
} else {
if (events.image != null) {
eventsIcon.src = events.image.dropshadow170.url;
} else {
eventsIcon.src = "modules/MMM-Events/icons/go.jpg";
}
eventsLogo.appendChild(eventsIcon);
wrapper.appendChild(eventsLogo);
}
}
var eventsDate2 = document.createElement("div");
eventsDate2.classList.add("xsmall", "bright", "list-title");
eventsDate2.innerHTML = events.venue_name;
wrapper.appendChild(eventsDate2);
var eventsDate4 = document.createElement("div");
eventsDate4.classList.add("xsmall", "bright", "list-title");
eventsDate4.innerHTML = events.venue_address;
wrapper.appendChild(eventsDate4);
var now = new Date(events.start_time);
var date = now.toLocaleDateString();
var time = now.toLocaleTimeString(navigator.language, {
hour: '2-digit',
minute: '2-digit'
});
var eventsDate3 = document.createElement("div");
eventsDate3.classList.add("xsmall", "bright", "list-title");
if (time != "12:00 AM") {
eventsDate3.innerHTML = "Date: " + date + "<br> Time: " + time;
} else {
eventsDate3.innerHTML = "Date: " + date + "<br> Time: To Be Determined";
}
wrapper.appendChild(eventsDate3);
}
return wrapper;
},
///// Add this function to the modules you want to control with voice //////
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_EVENTS') {
this.hide(1000);
// this.updateDom(300);
} else if (notification === 'SHOW_EVENTS') {
this.show(1000);
// this.updateDom(300);
}
},
processEvents: function(data) {
this.event = data.event;
this.perform = data.performers;
this.loaded = true;
},
scheduleCarousel: function() {
console.log("Scheduling Events");
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
scheduleUpdate: function() {
setInterval(() => {
this.getEvents();
}, this.config.updateInterval);
this.getEvents(this.config.initialLoadDelay);
var self = this;
},
getEventsUrl: function() {
var url = null;
var mode = this.config.mode;
var today = new Date();
var eventsYear = today.getMonth() + 1;20
var city = this.config.city.toLowerCase();
var apikey = this.config.apikey;
var eventType = this.config.eventType;
var when = this.config.when;
var radius = this.config.radius;
var radiusDistance = this.config.radiusDistance;
if (mode == "Frame") {
url = "http://api.eventful.com/json/events/search?app_key=" + apikey + "&location=" + city + "&date=" + when + "&category=" + eventType + "&within=" + radius + "&units=" + radiusDistance + "&sort_order=popularity&sort_direction=descending&page_size=50&image_sizes=dropshadow170";
} else {
url = "http://api.eventful.com/json/events/search?app_key=" + apikey + "&location=" + city + "&date=" + when + "&category=" + eventType + "&within=" + radius + "&units=" + radiusDistance + "&sort_order=popularity&sort_direction=descending&page_size=50&image_sizes=perspectivecrop176by124";
}
return url;
},
getEvents: function() {
this.sendSocketNotification('GET_EVENTS', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "EVENTS_RESULT") {
this.processEvents(payload);
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});