-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-horoscope.js
208 lines (182 loc) · 5.39 KB
/
MMM-horoscope.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
Module.register("MMM-horoscope",{
defaults: {
sign: "aries",
maxWidth: "400px", // maximum width of the module in px, %, em
updateInterval: 1 * 60 * 60 * 1000, // updates every hour
timeShift: 5 * 60 * 60 * 1000, // shift clock in milliseconds to start showing next day horoscope at 7pm (24 - 19 = 5)
useTextIcon: true,
initialLoadDelay: 0,
animationSpeed: 2000,
zodiacTable: {
"aries": {
"signId": "ari",
"range": "3/21-4/19",
"unicodeChar": "♈"
},
"taurus": {
"signId": "tau",
"range": "4/20-5/20",
"unicodeChar": "♉"
},
"gemini": {
"signId": "gem",
"range": "5/21-6/21",
"unicodeChar": "♊"
},
"cancer": {
"signId": "can",
"range": "6/22-7/22",
"unicodeChar": "♋"
},
"leo": {
"signId": "leo",
"range": "7/23-8/22",
"unicodeChar": "♌"
},
"virgo": {
"signId": "vir",
"range": "8/23-9/22",
"unicodeChar": "♍"
},
"libra": {
"signId": "lib",
"range": "9/23-10/22",
"unicodeChar": "♎"
},
"scorpio": {
"signId": "sco",
"range": "10/23-11/21",
"unicodeChar": "♏"
},
"sagittarius": {
"signId": "sag",
"range": "11/22-12/21",
"unicodeChar": "♐"
},
"capricorn": {
"signId": "cap",
"range": "12/22-1/19",
"unicodeChar": "♑"
},
"aquarius": {
"signId": "aqu",
"range": "1/20-2/18",
"unicodeChar": "♒"
},
"pisces": {
"signId": "pis",
"range": "2/19-3/20",
"unicodeChar": "♓"
}
},
debug: false
},
getStyles: function() {
return ["MMM-horoscope.css"];
},
getScripts: function() {
return [
"moment.js"
];
},
start: function() {
Log.info("Starting module: " + this.name);
if (this.config.debug) {
this.config.updateInterval = 60 * 1000; // update very 1 minute for debug
}
// just in case someone puts mixed case in their config files
this.config.sign = this.config.sign.toLowerCase();
this.sign = null;
this.signText = null;
this.horoscopeText = null;
this.horoscpeDate = null;
this.scheduleUpdate(this.config.initialLoadDelay);
},
updateHoroscope: function() {
this.date = new Date();
this.sendSocketNotification("GET_HOROSCOPE_DATA", { sign: this.config.sign, date: moment(this.date).add(this.config.timeShift, "milliseconds").format("YYYYMMDD") });
},
// Subclass socketNotificationReceived method.
socketNotificationReceived: function(notification, payload){
if(notification === "HOROSCOPE_DATA" && payload != null){
this.horoscopeData = payload[0];
this.processHoroscope(this.horoscopeData);
} else {
this.processHoroscopeError("Unable to get horoscope from API. Please check the logs.");
}
},
processHoroscope: function(data) {
const regex = /<a.*\/a>/g;
const subst = "";
this.sign = this.config.zodiacTable[this.config.sign]["unicodeChar"];
this.signText = data.sign;
this.horoscopeText = data.overview.replace(regex, subst).trim();
this.horoscopeDate = data.frequencyValue
this.loaded = true;
this.updateDom(this.config.animationSpeed);
this.scheduleUpdate();
},
processHoroscopeError: function(error) {
this.loaded = true;
this.error = true;
this.updateDom(this.config.animationSpeed);
Log.error("Process Horoscope Error: ", error);
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "horoscope-wrapper"
wrapper.style["max-width"] = this.config.maxWidth;
if (this.config.sign === "") {
wrapper.innerHTML = "Please set the correct Zodiac <i>sign</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = "Aligning Stars...";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.error) {
wrapper.innerHTML = this.name + ": Something went wrong. Please check logs.";
wrapper.className = "bright light small";
return wrapper;
}
var horoscopeTop = document.createElement("div");
horoscopeTop.className = "horoscope-top";
var zodiacIcon = document.createElement("div");
if (this.config.useTextIcon) {
zodiacIcon.className = "zodiac-text-icon";
zodiacIcon.innerHTML = this.sign;
} else {
zodiacIcon.className = "zodiac-icon " + this.config.sign;
}
var horoscopeTitle = document.createElement("div");
horoscopeTitle.className = "horoscope-title align-right";
var zodiacSignText = document.createElement("div");
zodiacSignText.className = "zodiac-sign-text medium";
zodiacSignText.innerHTML = this.signText;
var horoscopeDate = document.createElement("div");
horoscopeDate.className = "horoscope-date xsmall";
horoscopeDate.innerHTML = "Horoscope for " + moment(this.horoscopeDate).format("LL");
horoscopeTitle.appendChild(zodiacSignText);
horoscopeTitle.appendChild(horoscopeDate);
horoscopeTop.appendChild(zodiacIcon);
horoscopeTop.appendChild(horoscopeTitle);
wrapper.appendChild(horoscopeTop);
var horoscopeText = document.createElement("div");
horoscopeText.className = "horoscope-text small";
horoscopeText.innerHTML = this.horoscopeText;
wrapper.appendChild(horoscopeText);
return wrapper;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.updateHoroscope();
}, nextLoad);
}
});