-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-BirdOfTheDay.js
183 lines (159 loc) · 6.22 KB
/
MMM-BirdOfTheDay.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
/**
* MagicMirror² Module: MMM-BirdOfTheDay
* Version: 1.1.8
*
* Modifications:
* - Changed bird identification to use `sciName` instead of `id` for preventing duplicates.
* - Annotated code for clarity.
*/
Module.register("MMM-BirdOfTheDay", {
defaults: {
apiKey: "",
endpoint: "https://nuthatch.lastelm.software/v2/birds?hasImg=true",
rotation: "Daily",
updateInterval: 24 * 60 * 60 * 1000,
fadeSpeed: 2000,
imageWidth: "400px",
fontSize: "medium",
showName: true,
showSciName: true,
showRegion: true,
showStatus: true,
maxHistory: 50,
textPosition: "below", // Options: "below", "left", "right"
showTitleLine: true // Toggle to show/hide the line below the title
},
bird: null,
history: [], // Tracks `sciName` instead of `id`
start: function () {
this.updateIntervalFromRotation();
this.getBird();
setInterval(() => {
this.getBird();
}, this.config.updateInterval);
},
updateIntervalFromRotation: function () {
switch (this.config.rotation) {
case "Weekly":
this.config.updateInterval = 7 * 24 * 60 * 60 * 1000;
break;
case "Daily":
this.config.updateInterval = 24 * 60 * 60 * 1000;
break;
case "Hourly":
this.config.updateInterval = 60 * 60 * 1000;
break;
default:
console.warn("Invalid rotation value. Defaulting to 'Daily'.");
this.config.updateInterval = 24 * 60 * 60 * 1000;
}
},
getBird: function () {
fetch(this.config.endpoint, {
headers: { "api-key": this.config.apiKey },
})
.then((response) => {
if (!response.ok) throw new Error("Failed to fetch bird data.");
return response.json();
})
.then((data) => {
const birdsWithImages = data.entities.filter(
(bird) => bird.images && bird.images.length > 0
);
if (birdsWithImages.length > 0) {
let bird;
let attempts = 0;
// Pick a bird with a unique sciName
do {
const randomIndex = Math.floor(Math.random() * birdsWithImages.length);
bird = birdsWithImages[randomIndex];
attempts++;
} while (this.history.includes(bird.sciName) && attempts < 100);
if (attempts >= 100) {
console.warn("Could not find a unique bird, showing the first available.");
}
// Add the bird's sciName to history and maintain maxHistory limit
this.history.push(bird.sciName);
if (this.history.length > this.config.maxHistory) {
this.history.shift();
}
this.bird = bird;
this.updateDom(this.config.fadeSpeed);
}
})
.catch((error) => console.error("Error fetching bird data:", error));
},
getStyles: function () {
return ["MMM-BirdOfTheDay.css"];
},
getDom: function() {
const wrapper = document.createElement("div");
wrapper.className = `bird-wrapper bird-text-${this.config.textPosition}`;
if (!this.bird) {
wrapper.innerHTML = "Loading Bird...";
return wrapper;
}
// Title container
const titleContainer = document.createElement("div");
titleContainer.className = "bird-title-container";
// Title
const title = document.createElement("h2");
title.className = "bird-title";
title.innerHTML = this.config.rotation === "Weekly"
? "Bird of the Week"
: this.config.rotation === "Hourly"
? "Bird of the Hour"
: "Bird of the Day";
titleContainer.appendChild(title);
// Add horizontal line if enabled
if (this.config.showTitleLine) {
const line = document.createElement("hr");
line.className = "bird-title-line";
titleContainer.appendChild(line);
}
// Content wrapper
const contentWrapper = document.createElement("div");
contentWrapper.className = "bird-content";
// Image section
const imageContainer = document.createElement("div");
imageContainer.className = "bird-image-container";
const image = document.createElement("img");
image.className = "bird-image";
image.src = this.bird.images[0];
image.style.maxWidth = this.config.imageWidth;
imageContainer.appendChild(image);
// Info section
const info = document.createElement("div");
info.className = "bird-info";
if (this.config.showName) {
const birdName = document.createElement("h3");
birdName.className = "bird-name";
birdName.innerHTML = this.bird.name;
info.appendChild(birdName);
}
if (this.config.showSciName) {
const sciName = document.createElement("p");
sciName.innerHTML = `<i>${this.bird.sciName}</i>`;
sciName.style.fontSize = this.config.fontSize;
info.appendChild(sciName);
}
if (this.config.showRegion) {
const region = document.createElement("p");
region.innerHTML = `Region: ${this.bird.region.join(", ")}`;
region.style.fontSize = this.config.fontSize;
info.appendChild(region);
}
if (this.config.showStatus) {
const status = document.createElement("p");
status.innerHTML = `Conservation Status: ${this.bird.status}`;
status.style.fontSize = this.config.fontSize;
info.appendChild(status);
}
// Assembly
wrapper.appendChild(titleContainer);
contentWrapper.appendChild(imageContainer);
contentWrapper.appendChild(info);
wrapper.appendChild(contentWrapper);
return wrapper;
}
});