Deploy APOD Button

Co-authored-by: Panos Triantafyllidis <panostriantafyllidis@users.noreply.github.com>
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2024-04-14 11:58:00 +01:00
parent aa376ecfd6
commit cbdd838693
2 changed files with 55 additions and 2 deletions

View File

@@ -187,6 +187,35 @@ const main = function () {
player.addEventListener("ended", playNextTrack);
playNextTrack();
// APOD
document.getElementById("apodButton").addEventListener("click", function () {
window.open("https://apod.nasa.gov/apod/astropix.html", "_blank");
});
document.getElementById("downloadApodButton").addEventListener("click", async function () {
const apiKey = process.env.NASA_API_KEY;
const apiUrl = `https://api.nasa.gov/planetary/apod?api_key=${apiKey}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data.media_type === "image") {
const a = document.createElement("a");
a.href = data.hdurl || data.url;
a.download = data.title.replace(/[^a-zA-Z0-9]/g, "_") + ".jpg";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
alert("The APOD for today is not an image.");
}
} catch (error) {
console.error("Failed to fetch APOD data:", error);
alert("Failed to download the APOD image. Please try again later.");
}
});
// Animation
let simulationSpeed = 1;