-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiss.js
More file actions
77 lines (67 loc) · 2.05 KB
/
Copy pathiss.js
File metadata and controls
77 lines (67 loc) · 2.05 KB
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
const request = require('request');
const fetchMyIP = function(callback) {
const url = `https://api.ipify.org?format=json`;
request(url, (error, response, body) => {
const ip = JSON.parse(body).ip;
if (error) {
return callback(error, null);
}
if (response.statusCode !== 200) {
const msg = `Status Code ${response.statusCode} when fetching IP. Response: ${body}`;
return callback(Error(msg), null);
}
callback(null, ip);
});
};
const fetchCoordsByIP = function(ip, callback) {
const url = `http://ipwho.is/${ip}`;
request(url, (error, response, body) => {
const coordinates = JSON.parse(body);
if (error) {
return callback(error, null);
}
if (!coordinates.success) {
const msg = `Status Code ${coordinates.success} when fetching IP ${coordinates.ip}. Response: ${coordinates.message}`;
return callback(Error(msg), null);
}
const { latitude, longitude } = coordinates;
callback(null, { latitude, longitude });
});
};
const fetchISSFlyOverTimes = function(coords, callback) {
const url = `https://iss-flyover.herokuapp.com/json/?lat=${coords.latitude}&lon=${coords.longitude}`;
request(url, (error, response, body) => {
if (error) {
return callback(error, null);
}
if (response.statusCode !== 200) {
return callback(Error(`Status Code ${response.statusCode} when fetching ISS pass times: ${body}`), null);
}
const passes = JSON.parse(body).response;
callback(null, passes);
});
};
const nextISSTimesForMyLocation = function(callback) {
fetchMyIP((error, ip) => {
if (error) {
return callback(error, null);
}
fetchCoordsByIP(ip, (error, coordinates) => {
if (error) {
return callback(error, null);
}
fetchISSFlyOverTimes(coordinates, (error, passTimes) => {
if (error) {
return callback(error, null);
}
callback(null, passTimes);
});
});
});
};
module.exports = {
// fetchMyIP,
// fetchCoordsByIP,
// fetchISSFlyOverTimes,
nextISSTimesForMyLocation
};