-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapom.js
189 lines (164 loc) · 5.17 KB
/
apom.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
184
185
186
187
188
189
var counter = 0;
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('mode')) {
var mode_param = urlParams.get('mode');
} else {
var mode_param = 'default';
}
if (urlParams.has('image')) {
var image_param = urlParams.get('image');
} else {
var image_param = null;
}
if (urlParams.has('max')) {
var max_param = urlParams.get('max');
} else {
var max_param = 60;
}
if (urlParams.has('timer')) {
var timer_param = urlParams.get('timer');
} else {
var timer_param = 60;
}
if (urlParams.has('nasakey')) {
var nasakey_param = urlParams.get('nasakey');
} else {
// Replace DEMO_KEY with your https://api.nasa.gov/ key
var nasakey_param = "DEMO_KEY";
}
var img_url = "images/";
// Overrides for demo mode
console.log(window.location.href);
if (window.location.href == "https://apom.mellican.com/") {
mode_param = "demo";
}
if (mode_param == "demo") {
timer_param = 10;
img_url = "https://astro.mellican.com/apom/images/";
mode_param = "random";
image_param = null;
}
// Overrides for NASA APOD mode
if (mode_param == "nasa") {
timer_param = 86400;
};
function change_apom() {
const uniq = Date.now();
let minutes = counter++;
if (counter == max_param) {
counter = 0;
}
// Load new image. The parameter forces CDNs for the non-cached object
var reimgapom
reimgapom=document.getElementById('live-apom')
if (mode_param == "random") {
minutes = Math.floor(Math.random() * max_param);
}
if (typeof image_param === 'undefined' || image_param === null) {
img_src = img_url + minutes + ".jpg?" + uniq;
} else {
img_src = img_url + image_param + "?" + uniq;
}
if (mode_param == "nasa") {
nasarequested(nasakey_param);
} else if (existsFile(img_src)) {
reimgapom.src=img_src;
} else {
change_apom();
}
// Load new image caption. The parameter forces CDNs for the non-cached object
const caption_url = img_src.replace('jpg','txt');
if (existsFile(caption_url)) {
apom_caption(caption_url);
} else {
document.getElementById("title").innerHTML = "";
}
// Replace the APOM title with the minutes ordinal
apom_title()
}
(function countdown(remaining) {
if(remaining === 0) {
change_apom();
// Reset the timer
remaining = timer_param;
}
if(remaining > 0) {
setTimeout(function(){ countdown(remaining - 1); }, 1000);
}
})(timer_param);
function apom_caption(url) {
$('#caption').load(url);
}
function apom_title() {
if (mode_param == "random") {
apoth = "My Random Astronomy Picture of the Moment"
} else {
apoth = "My Astronomy Picture of the Moment"
}
document.getElementById("moment").innerHTML = apoth;
}
function existsFile(url) {
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 404) {
console.log("404 detected");
return false;
}
}
}
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
function nasarequested(nasaKey){
const baseUrl = 'https://api.nasa.gov/planetary/apod?api_key=';
const title = document.querySelector("#caption");
const copyright = document.querySelector("#copyright");
const mediaSection = document.querySelector("#media-section");
const information = document.querySelector("#description");
const currentDate =new Date().toISOString().slice(0, 10);
const imageSection =`<a id="hdimg" href="" target="-blank"></a>`
const videoSection=`<div class="video-div"> <iframe id="videoLink" src="" frameborder="0"></iframe></div>`
let newDate = "";
function fetchData(){
try{
fetch(baseUrl+nasaKey+newDate)
.then(response=> response.json())
.then(json=>{
diplaydata(json)
})
}catch(error){
console.log(error)
}
}
function diplaydata(data){
document.getElementById("moment").innerHTML = "NASA Astronomy Picture of the Day";
title.innerHTML=data.title;
if(data.hasOwnProperty("copyright")){
copyright.innerHTML=data.copyright;
} else{
copyright.innerHTML=""
}
if(data.media_type=="video"){
mediaSection.innerHTML=videoSection;
document.getElementById("videoLink").src=data.url;
}else{
document.getElementById("live-apom").src=data.url;
}
if (apiKey == "DEMO_KEY"){
document.getElementById("live-apom").src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/NASA_logo.svg/574px-NASA_logo.svg.png";
title.innerHTML="Click here for a → <a href='https://api.nasa.gov/' target='_blank'>NASA API</a> key";
copyright.innerHTML="Then edit apom.js and replace DEMO_KEY"
}
}
fetchData();
}
window.onload = function() {
if (mode_param == "nasa") {
nasarequested(nasakey_param);
} else {
change_apom();
}
}