-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (51 loc) · 2.07 KB
/
script.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
(async function() {
const url = "https://api.nasa.gov/planetary/apod?api_key=xstEYQCA6wByrwDBI51liejfp0HOQyF8gzqqXjcI";
const wall = document.querySelector('.image-wall');
// Loop over past images
let date = new Date();
let count = 10;
while (count--) {
// Get date
let dateString = date.toISOString().substr(0, 10);
// Do API call
let response = await fetch(url + '&date=' + dateString);
// Check if response is valid
if (response.status !== 200) {
continue;
}
// Get JSON data
let data = await response.json();
// Make nodes
let li = document.createElement('li');
let a = document.createElement('a');
let figure = document.createElement('figure');
let img = document.createElement('img');
let figcaption = document.createElement('figcaption');
// Append nodes
let captionText = null;
if (data.media_type === 'video') {
// Youtube video thumbnail
let videoId = data.url.split('/')[4].split('?')[0];
img.setAttribute('src', 'https://img.youtube.com/vi/' + videoId + '/default.jpg');
a.setAttribute('href', data.url);
captionText = document.createTextNode('[Video] ' + data.title + ' (' + data.date + ')');
} else {
// Regular image
img.setAttribute('src', data.url);
a.setAttribute('href', data.hdurl);
captionText = document.createTextNode(data.title + ' (' + data.date + ')');
}
figcaption.appendChild(captionText);
img.setAttribute('alt', data.title);
img.setAttribute('class', 'center-cropped');
figure.appendChild(img);
figure.appendChild(figcaption);
a.setAttribute('target', '_blank');
a.appendChild(figure);
li.setAttribute('class', 'image-item');
li.appendChild(a);
wall.append(li);
// Set date to previous date
date.setDate(date.getDate() - 1)
}
})();