-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (166 loc) · 5.36 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const ejs = require("ejs");
const fetch = require("node-fetch");
require("dotenv").config();
const response = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.set("views", "public/views");
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
const myAPI_KEY = process.env.MY_API_KEY;
const url = "https://api.nasa.gov/planetary/apod?api_key=";
const curl = url + myAPI_KEY;
app.get("/gallery", function (req, res) {
res.render("index4");
});
app.get("/addendum", function (req, res) {
fetch(curl)
.then((response) => response.json())
.then((data) => {
// console.log(data.media_type);
res.render("index3", {
info: {
body: data.explanation,
imgurl: data.url,
heading: data.title,
date: data.date,
type: data.media_type,
},
});
})
.catch((error) => {
console.log(error);
});
});
function generateRandomDate() {
var now = new Date(); //right now
var min = new Date(1995, 5, 16).getTime(); // 1995 June 16 00:00:00, the first APOD
var max = new Date(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
18,
59,
59,
999
).getTime(); // now converted UTC time at 03:59:59.999
//taking off 6 hours because APOD goes by east coast USA time.
//should be enough to keep anyone from landing on future APODs which won't be published yet in their timezone
//unless their computer clock is set way off, then they'll get 404's all the time probably
max = max - 5 * 60 * 60 * 1000;
var randomDate = Math.round(min + Math.random() * (max - min)); //ahh, a random APOD date!
//but wait...
//there's one section of missing APODs in the history of APODs
//that's the first three days after the very first APOD was posted
//June 17th, 18th, & 19th, 1995
var missingMin = new Date(1995, 5, 17).getTime(); //1995 June 17 00:00:00
var missingMax = new Date(1995, 5, 19, 23, 59, 59, 999).getTime(); //1995 June 19 23:59:59.999
//if our random date falls in this range, remake it.
while (randomDate >= missingMin && randomDate <= missingMax) {
randomDate = Math.round(min + Math.random() * (max - min));
}
//convert the timestamp back into a date object
randomDate = new Date(randomDate);
this.random_year = randomDate.getFullYear().toString().slice(); //in the year 2095 we're gonna have problems :)))
this.random_month = (0 + (randomDate.getMonth() + 1).toString()).slice(-2); //zero pad the month
this.random_day = (0 + randomDate.getDate().toString()).slice(-2); //zero pad the day
this.randomApodDate =
this.random_year + "-" + this.random_month + "-" + this.random_day;
return this.randomApodDate;
}
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
today.toDateString();
yesterday.toDateString();
previous_day = yesterday.getDate().toString();
year = yesterday.getFullYear().toString();
month = (yesterday.getMonth() + 1).toString();
previousdate = year + "-" + month + "-" + previous_day;
app.get("/previous", function (req, res) {
var curl2 = url + myAPI_KEY + "&date=" + previousdate;
fetch(curl2)
.then((response) => response.json())
.then((data) => {
res.render("index3", {
info: {
body: data.explanation,
imgurl: data.url,
heading: data.title,
date: data.date,
type: data.media_type,
},
});
})
.catch((error) => {
console.log(error);
});
});
app.get("/random", function (req, res) {
var randomdate = generateRandomDate();
var curl1 = url + myAPI_KEY + "&date=" + randomdate;
fetch(curl1)
.then((response) => response.json())
.then((data) => {
res.render("index3", {
info: {
body: data.explanation,
imgurl: data.url,
heading: data.title,
date: data.date,
type: data.media_type,
},
});
})
.catch((error) => {
console.log(error);
});
});
app.get("/celestial", function (req, res) {
res.sendFile(__dirname + "/public/celestial.html");
});
app.get("/icon", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/celestial/fireball", function (req, res) {
var i;
fetch("https://ssd-api.jpl.nasa.gov/fireball.api?limit=60")
.then((response) => response.json())
.then((data) => {
var data1 = [];
for (i = 0; i < data.data.length; i++) {
data1.push(data.data[i]);
}
res.render("index1", { set: data1 });
})
.catch((error) => {
console.log(error);
});
});
app.get("/celestial/cad", function (req, res) {
var i;
fetch(
"https://ssd-api.jpl.nasa.gov/cad.api?dist-max=10LD&date-min=2018-01-01&sort=dist&limit=60&body=ALL&fullname=true"
)
.then((response) => response.json())
.then((data) => {
var data1 = [];
for (i = 0; i < data.data.length; i++) {
data1.push(data.data[i]);
}
res.render("index2", { set: data1 });
})
.catch((error) => {
console.log(error);
});
});
app.listen(port, function () {
console.log(`Server is running on port ${port}`);
});