-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-BirdOfTheDay.js
168 lines (144 loc) · 5.55 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
Module.register("MMM-BirdOfTheDay", {
defaults: {
apiKey: "",
endpoint: "https://nuthatch.lastelm.software/v2/birds",
rotation: "Daily",
updateInterval: 24 * 60 * 60 * 1000,
fadeSpeed: 2000,
imageWidth: "400px",
fontSize: "medium",
showName: true,
showSciName: true,
showRegion: true,
showStatus: true,
maxHistory: 100,
pageSize: 100,
textPosition: "below",
showTitleLine: true
},
bird: null,
history: [],
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 () {
const url = `${this.config.endpoint}?hasImg=true&pageSize=${this.config.pageSize}`;
fetch(url, {
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;
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.");
}
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;
}
const titleContainer = document.createElement("div");
titleContainer.className = "bird-title-container";
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);
if (this.config.showTitleLine) {
const line = document.createElement("hr");
line.className = "bird-title-line";
titleContainer.appendChild(line);
}
const contentWrapper = document.createElement("div");
contentWrapper.className = "bird-content";
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);
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);
}
wrapper.appendChild(titleContainer);
contentWrapper.appendChild(imageContainer);
contentWrapper.appendChild(info);
wrapper.appendChild(contentWrapper);
return wrapper;
}
});