-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMMM-APOD.js
245 lines (198 loc) · 6.5 KB
/
MMM-APOD.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
/* MagicMirror²
* Module: MMM-APOD
*
* MagicMirror² By Michael Teeuw https://magicmirror.builders
* MIT Licensed.
*
* Module MMM-APOD By Grena https://github.com/grenagit
* MIT Licensed.
*/
Module.register("MMM-APOD",{
// Default module config
defaults: {
appid: "",
updateInterval: 6 * 60 * 60 * 1000, // every 6 hours
animationSpeed: 1000, // 1 second
maxMediaWidth: 0,
maxMediaHeight: 0,
maxDescriptionLength: 200,
backgroundSize: "cover",
backgroundPosition: "center",
backgroundOverlay: "linear-gradient(to bottom, rgba(0, 0, 0, 0.75) 0%, rgba(0, 0, 0, 0) 40%, rgba(0, 0, 0, 0) 80%, rgba(0, 0, 0, 0.75) 100%)",
showTitle: true,
showDescription: false,
useShortDescription: true,
initialLoadDelay: 0, // 0 seconds delay
retryDelay: 2500, // 2,5 seconds
apiBase: "https://api.nasa.gov/",
apodEndpoint: "planetary/apod",
},
// Define required scripts
getStyles: function() {
return ["MMM-APOD.css"];
},
// Define start sequence
start: function() {
Log.info("Starting module: " + this.name);
this.title = null;
this.description = null;
this.copyright = null;
this.type = null;
this.url = null;
if(this.data.position === 'fullscreen_below') {
this.backgrounded = true;
this.data.header = null;
} else {
this.backgrounded = false;
}
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
},
// Override dom generator
getDom: function() {
var wrapper = document.createElement("div");
if(this.config.appid === "") {
wrapper.innerHTML = "Please set the correct NASA <i>appid</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if(!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
return wrapper;
}
if(this.backgrounded) {
var apodBackground = document.createElement('div');
apodBackground.className = "background";
apodBackground.style.backgroundSize = this.config.backgroundSize;
apodBackground.style.backgroundPosition = this.config.backgroundPosition;
apodBackground.style.backgroundImage = 'url("' + this.url + '")';
wrapper.appendChild(apodBackground);
if(this.config.backgroundOverlay != "") {
var apodBackgroundOverlay = document.createElement('div');
apodBackgroundOverlay.className = "overlay";
apodBackgroundOverlay.style.background = this.config.backgroundOverlay;
wrapper.appendChild(apodBackgroundOverlay);
}
} else {
if(this.config.showTitle) {
var apodTitle = document.createElement('div');
apodTitle.className = "dimmed light small";
apodTitle.innerHTML = this.title;
wrapper.appendChild(apodTitle);
}
var apodImage = document.createElement('img');
if(this.config.maxMediaWidth != 0) {
apodImage.style.maxWidth = this.config.maxMediaWidth + 'px';
}
if(this.config.maxMediaHeight != 0) {
apodImage.style.maxHeight = this.config.maxMediaHeight + 'px';
}
apodImage.src = this.url;
apodImage.alt = this.title;
wrapper.appendChild(apodImage);
if(this.copyright != "" && typeof this.copyright !== "undefined") {
var apodCopyright = document.createElement('div');
apodCopyright.className = "dimmed thin xsmall";
apodCopyright.innerHTML = "© " + this.copyright;
wrapper.appendChild(apodCopyright);
}
if(this.config.showDescription) {
var apodDescription = document.createElement('div');
apodDescription.className = "dimmed light xsmall description";
if(this.config.maxMediaWidth != 0) {
apodDescription.style.maxWidth = this.config.maxMediaWidth + 'px';
} else if(this.type === "video") {
apodDescription.style.maxWidth = '960px';
}
if(this.config.useShortDescription) {
apodDescription.innerHTML = this.shortText(this.description, this.config.maxDescriptionLength);
} else {
apodDescription.innerHTML = this.description;
}
wrapper.appendChild(apodDescription);
}
}
return wrapper;
},
// Request new data from api.nasa.gov
updateAPOD: async function() {
if(this.config.appid === "") {
Log.error(this.name + ": APPID not set.");
return;
}
var url = this.config.apiBase + this.config.apodEndpoint + "?api_key=" + this.config.appid;
var self = this;
var retry = true;
try {
const response = await fetch(url);
if(response.status === 200) {
const data = await response.json();
self.processAPOD(data);
} else if(response.status === 403) {
self.updateDom(self.config.animationSpeed);
retry = false;
throw new Error(self.name + ": Incorrect APPID.");
} else if(response.status === 429) {
self.updateDom(self.config.animationSpeed);
retry = false;
throw new Error(self.name + ": Rate limit exceeded.");
} else {
throw new Error(self.name + ": Could not load APOD.");
}
} catch(error) {
Log.error(error);
} finally {
if(retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
},
// Use the received data to set the various values before update DOM
processAPOD: function(data) {
if(!data || typeof data.url === "undefined") {
Log.error(this.name + ": Do not receive usable data.");
return;
}
this.title = data.title;
this.description = data.explanation;
this.copyright = data.copyright;
this.type = data.media_type;
if(this.type === "image") {
if(typeof data.hdurl !== "undefined") {
this.url = data.hdurl;
} else {
this.url = data.url;
}
} else if(this.type === "video") {
let id = data.url.match(/(?:[?&]vi?=|\/embed\/|\/\d\d?\/|\/vi?\/|https?:\/\/(?:www\.)?youtu\.be\/)([^&\n?#]+)/)[1];
this.url = "https://img.youtube.com/vi/" + id + "/maxresdefault.jpg";
} else {
Log.error(this.name + ": Type of media unknown (not image or video).");
return;
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
// Schedule next update
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if(typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.updateAPOD();
}, nextLoad);
},
// Short text without cutting sentences and words
shortText: function (text, maxLenght) {
if(text.lastIndexOf(".", maxLenght) !== -1) {
return text.substr(0, text.lastIndexOf(".", maxLenght)) + ".";
} else if(text.lastIndexOf(" ", maxLenght) !== -1) {
return text.substr(0, text.lastIndexOf(" ", maxLenght)) + "…";
} else {
return text.substr(0, maxLenght) + "…";
}
}
});