-
Notifications
You must be signed in to change notification settings - Fork 4
/
MMM-RottenTomatoes.js
220 lines (217 loc) · 7.51 KB
/
MMM-RottenTomatoes.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
/* global Module */
/* MMM-RottenTomatoes.js
*
* Magic Mirror
* Module: MMM-RottenTomatoes
*
* Magic Mirror By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*
* Module MMM-RottenTomatoes By Adam Moses http://adammoses.com
* MIT Licensed.
*/
Module.register("MMM-RottenTomatoes", {
// setup the default config options
defaults: {
// optional
showHeader: true,
showOpeningThisWeek: true,
showBoxOffice: true,
showComingSoon: true,
limitOpeningThisWeek: 3,
limitBoxOffice: 3,
limitComingSoon: 3,
boxOfficeAfter: true,
mergeOpeningAndComingSoon: true,
},
// the start function
start: function() {
// log starting
Log.info("Starting module: " + this.name);
// set refresh rate to 6 hours
this.config.refreshRate = 6 * 60 * 60 * 1000;
// set an identier config tag
this.config.identifier = this.identifier;
// set loaded, error, and the update to init values
this.loaded = false;
this.errorMessage = null;
// set the header to this place
if (this.config.showHeader) {
this.data.header = 'Rotten Tomatoes';
}
if (this.config.limitOpeningThisWeek == 0)
this.config.limitOpeningThisWeek = 100;
if (this.config.limitBoxOffice == 0)
this.config.limitBoxOffice = 100;
if (this.config.limitComingSoon == 0)
this.config.limitComingSoon = 100;
// add this config to the helper functions
this.sendSocketNotification('ROTTEN_TOMATOES_REGISTER_CONFIG', this.config);
},
// the socket handler
socketNotificationReceived: function(notification, payload) {
// if an update was received
if (notification === "ROTTEN_TOMATOES_UPDATE") {
// check this is for this module based on the woeid
if (payload.identifier === this.identifier)
{
// set loaded flag, set the update, and call update dom
this.rtData = payload.rtData;
this.loaded = true;
this.updateDom();
}
}
// if sent error notice
if (notification === "ROTTEN_TOMATOES_TOO_MANY_ERRORS") {
this.errorMessage("There was an error.");
if (this.updateTimer !== null)
clearTimeout(this.updateTimer);
this.updateTimer = null;
this.updateDom();
}
},
//
cleanScore: function(theScore) {
if (theScore.indexOf('%') == -1)
return '--';
else
return theScore;
},
//
cleanTitle: function(theTitle) {
if (theTitle.length > 28)
return theTitle.slice(0, 28) + '...';
else
return theTitle;
},
// the get dom handler
getDom: function() {
// if an error, say so
if (this.errorMessage !== null) {
var wrapper = document.createElement("div");
wrapper.className = "small";
wrapper.innerHTML = this.errorMessage;
return wrapper;
}
// if nothing loaded yet, put in placeholder text
if (!this.loaded) {
var wrapper = document.createElement("div");
wrapper.className = "small";
wrapper.innerHTML = "Awaiting Update...";
return wrapper;
}
var titleSize = 'xsmall';
var movieSize = 'xsmall';
var wrapper = document.createElement("table");
// do opening this week
var allOTWandCSRows = [ ];
if (this.config.showOpeningThisWeek) {
var otwData = this.rtData.openingThisWeek;
var otwTitleTR = document.createElement("tr");
otwTitleTR.className = titleSize;
var otwTitleTD = document.createElement("td");
otwTitleTD.innerHTML = "Opening This Week";
if (this.config.mergeOpeningAndComingSoon)
otwTitleTD.innerHTML = "Opening / Coming Soon";
otwTitleTD.colSpan = "3";
otwTitleTR.appendChild(otwTitleTD);
allOTWandCSRows.push(otwTitleTR);
for (var cIndex = 0;
(cIndex < otwData.length) && (cIndex < this.config.limitOpeningThisWeek);
cIndex++) {
var cOTW = otwData[cIndex];
var otwRowTR = document.createElement("tr");
otwRowTR.className = movieSize;
var otwRowMeter = document.createElement("td");
otwRowMeter.innerHTML = this.cleanScore(cOTW.meter) + " ";
otwRowTR.appendChild(otwRowMeter);
var otwRowTitle = document.createElement("td");
otwRowTitle.innerHTML = this.cleanTitle(cOTW.title) + " ";
otwRowTitle.align = 'left';
otwRowTR.appendChild(otwRowTitle);
var otwRowDate = document.createElement("td");
otwRowDate.innerHTML = " " + cOTW.date;
otwRowTR.appendChild(otwRowDate);
allOTWandCSRows.push(otwRowTR);
}
}
// do opening this week
if (this.config.showOpeningThisWeek) {
var csData = this.rtData.comingSoon;
var csTitleTR = document.createElement("tr");
csTitleTR.className = titleSize;
var csTitleTD = document.createElement("td");
csTitleTD.innerHTML = "Coming Soon";
csTitleTD.colSpan = "3";
csTitleTR.appendChild(csTitleTD);
if (!this.config.mergeOpeningAndComingSoon)
allOTWandCSRows.push(csTitleTR);
for (var cIndex = 0;
(cIndex < csData.length) && (cIndex < this.config.limitComingSoon);
cIndex++) {
var ccs = csData[cIndex];
var csRowTR = document.createElement("tr");
csRowTR.className = movieSize;
var csRowMeter = document.createElement("td");
csRowMeter.innerHTML = this.cleanScore(ccs.meter) + " ";
csRowTR.appendChild(csRowMeter);
var csRowTitle = document.createElement("td");
csRowTitle.innerHTML = this.cleanTitle(ccs.title) + " ";
csRowTitle.align = 'left';
csRowTR.appendChild(csRowTitle);
var csRowDate = document.createElement("td");
csRowDate.innerHTML = " " + ccs.date;
csRowTR.appendChild(csRowDate);
allOTWandCSRows.push(csRowTR);
}
}
// do box office
var boRows = [ ];
if (this.config.showBoxOffice) {
var boData = this.rtData.boxOffice;
var boTitleTR = document.createElement("tr");
boTitleTR.className = titleSize;
var boTitleTD = document.createElement("td");
boTitleTD.innerHTML = "Box Office";
boTitleTD.width = "250px";
boTitleTD.colSpan = "3";
boTitleTR.appendChild(boTitleTD);
boRows.push(boTitleTR);
for (var cIndex = 0;
(cIndex < boData.length) && (cIndex < this.config.limitBoxOffice);
cIndex++) {
var cbo = boData[cIndex];
var boRowTR = document.createElement("tr");
boRowTR.className = movieSize;
var boRowMeter = document.createElement("td");
boRowMeter.innerHTML = this.cleanScore(cbo.meter) + " ";
boRowTR.appendChild(boRowMeter);
var boRowTitle = document.createElement("td");
boRowTitle.innerHTML = this.cleanTitle(cbo.title) + " ";
boRowTitle.align = 'left';
boRowTR.appendChild(boRowTitle);
var boRowGross = document.createElement("td");
boRowGross.innerHTML = " " + cbo.gross;
boRowTR.appendChild(boRowGross);
boRows.push(boRowTR);
}
}
// build the table
var allRows = [ ];
// if set to show box office first, do that
if (!this.config.boxOfficeAfter)
allRows = allRows.concat(boRows);
// add opening this week and coming soon rows
allRows = allRows.concat(allOTWandCSRows);
// if set to show box office after, do that
if (this.config.boxOfficeAfter)
allRows = allRows.concat(boRows);
// add all rows to the return table wrapper
for (var cIndex = 0; cIndex < allRows.length; cIndex++) {
wrapper.appendChild(allRows[cIndex]);
}
// return table wrapper
return wrapper;
}
});
// ------------ end -------------