-
Notifications
You must be signed in to change notification settings - Fork 0
/
epic.html
64 lines (63 loc) · 2.52 KB
/
epic.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kimeiga/bahunya/dist/bahunya.min.css">
<title>Space Hub</title>
</head>
<body onload="fetchData()">
<header>
<nav>
<ul>
<li><strong>Space Hub</strong></li>
</ul>
<ul>
<li><a href="index.html" class="contrast">APOD</a></li>
<li><a href="epic.html" class="contrast">EPIC</a></li>
<li><a href="iss.html" class="contrast">ISS Tracker</a></li>
</ul>
</nav>
</header>
<main>
<article>
<header>
<h2>Earth Polychromatic Imaging Camera</h2>
<b id="epic-date"></b>
</header>
<img src="" id="epic-image" style="display: none;"/>
<p id="epic-description"></p>
</article>
</main>
<footer>
<p>Data from <a href="https://api.nasa.gov">NASA Open API</a></p>
<br />
<p><a href="https://github.com/vachan-maker/space-hub">Source Code</a></p>
</footer>
<script>
async function fetchData() {
try {
const api_key = '4nin6GMZtTmHZc0sJKECqidNSfQBwvqdy33ezKL0';
const response = await fetch(`https://api.nasa.gov/EPIC/api/natural?api_key=${api_key}`);
if (!response.ok) {
throw new Error("Could not fetch resource");
}
const data = await response.json();
var epicDate = new Date(data[5].date);
const epicDateElement = document.getElementById('epic-date');
epicDateElement.innerText = epicDate;
epicImageURL = data[5].image;
const epicImage = `https://api.nasa.gov/EPIC/archive/natural/${epicDate.getFullYear()}/${('0'+ (epicDate.getMonth()+1)).slice(-2)}/${epicDate.getDate()}/png/${epicImageURL}.png?api_key=${api_key}` ;
const imageElement = document.getElementById("epic-image");
imageElement.src = epicImage;
imageElement.style.display = "block";
const descriptionElement = document.getElementById("epic-description");
descriptionElement.innerText = data[5].caption;
console.log(epicImage);
} catch (error) {
console.error(error);
}
}
</script>
</body>
</html>