-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Thingiverse.js
executable file
·206 lines (168 loc) · 5.37 KB
/
MMM-Thingiverse.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
/* global Module */
/* Magic Mirror
* Module: MMM-Thingiverse
*
* By Lucas Bock
* MIT Licensed.
*/
Module.register('MMM-Thingiverse', {
defaults: {
appToken: '',
retryDelay: 5000,
startAtRandom: false,
thingCount: 100,
updateInterval: 60000,
numThingsDisplayed: 1,
searchBy: 'popular',
isFeatured: false,
category: '',
},
requiresVersion: '2.1.0',
start: function () {
var self = this;
var dataRequest = null;
this.currentPage = 1;
this.currentThingId = -1;
this.iterations = 0;
this.loaded = false;
this.maxPages = 11;
this.newRequestTimeout = 10000;
this.overrideUrl = null;
this.things = { hits: [] };
if (!['popular', 'newest'].includes(self.config.searchBy)) {
self.config.searchBy = 'popular';
}
if (![1, 3, 5].includes(self.config.numThingsDisplayed)) {
self.config.numThingsDisplayed = 3;
}
this.getData();
setInterval(function () {
self.updateDom();
}, this.config.updateInterval);
},
getData: function () {
var self = this;
var apiBase = 'https://api.thingiverse.com/';
var apiConfig = `access_token=${this.config.appToken}&per_page=${this.config.thingCount}&page=${self.currentPage}`;
var popularConfig = `search/?sort=${this.config.searchBy}&is_featured=${
this.config.isFeatured ? 1 : 0
}`;
var categoryConfig = `categories/${this.config.category}/things`;
var urlApi =
apiBase +
(this.config.category ? categoryConfig + '?' : popularConfig + '&') +
apiConfig;
var fallbackUrl = apiBase + popularConfig + '&' + apiConfig;
var retry = true;
if (self.overrideUrl != null && self.overrideUrl !== urlApi) {
urlApi = self.overrideUrl;
}
var dataRequest = new XMLHttpRequest();
dataRequest.open('GET', urlApi, true);
dataRequest.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
self.iterations = 0;
var parsedResponse = JSON.parse(this.response);
if (
(parsedResponse && Array.isArray(parsedResponse)) ||
(parsedResponse.hits && Array.isArray(parsedResponse.hits))
) {
self.things =
self.config.category && self.overrideUrl === null
? parsedResponse
: parsedResponse.hits;
self.currentThingId = self.config.startAtRandom
? Math.floor(Math.random() * self.things.length)
: 0;
self.currentPage = Math.max(
(self.currentPage + 1) % self.maxPages,
1,
);
self.processData(self.things);
}
} else if (this.status === 401) {
self.updateDom(self.config.animationSpeed);
Log.error(self.name, this.status);
self.overrideUrl = fallbackUrl;
self.scheduleUpdate(self.loaded ? -1 : self.config.retryDelay);
} else {
Log.error(self.name, 'Could not load data.');
}
}
};
dataRequest.send();
},
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== 'undefined' && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad;
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
},
createThingElement: function (thing) {
var thingCard = document.createElement('div');
thingCard.classList.add('MMM-Thingiverse-card');
if (thing) {
var thingCreator = document.createElement('div');
thingCreator.classList.add('MMM-Thingiverse-creator');
thingCreator.innerHTML = `<i>${thing.creator.name}</i>`;
var thingName = document.createElement('div');
thingName.classList.add('MMM-Thingiverse-name');
thingName.innerHTML = `<b>${thing.name}</b>`;
var thingThumbnail = document.createElement('img');
thingThumbnail.classList.add('MMM-Thingiverse-thumbnail');
thingThumbnail.src = thing.preview_image;
thingThumbnail.alt = thing.name;
thingCard.appendChild(thingName);
thingCard.appendChild(thingThumbnail);
thingCard.appendChild(thingCreator);
}
return thingCard;
},
getDom: function () {
var self = this;
var wrapper = document.createElement('div');
wrapper.classList.add('MMM-Thingiverse-wrapper');
if (this.dataRequest) {
for (var i = 0; i < self.config.numThingsDisplayed; i++) {
var thing = self.things[self.currentThingId % self.things.length];
if (thing) {
self.iterations = self.iterations + 1;
if (self.iterations >= self.things.length) {
self.scheduleUpdate(self.newRequestTimeout);
}
self.currentThingId = self.currentThingId + 1;
wrapper.appendChild(self.createThingElement(thing));
}
}
}
return wrapper;
},
getScripts: function () {
return [];
},
getStyles: function () {
return ['MMM-Thingiverse.css'];
},
getTranslations: function () {
return {
en: 'translations/en.json',
};
},
processData: function (data) {
var self = this;
if (Array.isArray(data)) {
this.dataRequest = data;
if (this.loaded === false) {
self.updateDom(self.config.animationSpeed);
}
this.loaded = true;
}
},
socketNotificationReceived: function (notification, payload) {},
});