forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add launches arrays to ships and payloads
- Loading branch information
1 parent
a302a37
commit d7a7789
Showing
6 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
|
||
const got = require('got'); | ||
const { logger } = require('../middleware/logger'); | ||
|
||
const SPACEX_API = 'https://stage.spacexdata.com/v4'; | ||
const KEY = process.env.SPACEX_KEY; | ||
const HEALTHCHECK = process.env.LAUNCHES_HEALTHCHECK; | ||
|
||
/** | ||
* Update launch arrays | ||
* @return {Promise<void>} | ||
*/ | ||
module.exports = async () => { | ||
const launches = await got.post(`${SPACEX_API}/launches/query`, { | ||
json: { | ||
query: { | ||
upcoming: false, | ||
}, | ||
options: { | ||
sort: { | ||
flight_number: 'asc', | ||
}, | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const results = { | ||
capsule: false, | ||
core: false, | ||
crew: false, | ||
landpad: false, | ||
launchpad: false, | ||
payload: false, | ||
ship: false, | ||
}; | ||
|
||
|
||
// Update capsule launches | ||
const capsules = await got.post(`${SPACEX_API}/capsules/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const capsuleLaunches = capsules.docs.map(async (capsule) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.capsules.includes(capsule.id)) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/capsules/${capsule.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.capsule = true; | ||
}); | ||
await Promise.all(capsuleLaunches); | ||
|
||
|
||
// Update core launches | ||
const cores = await got.post(`${SPACEX_API}/cores/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const coreLaunches = cores.docs.map(async (core) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.cores.filter((c) => c.core === core.id).length > 0) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/crew/${core.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.core = true; | ||
}); | ||
await Promise.all(coreLaunches); | ||
|
||
|
||
// Update crew launches | ||
const crewMembers = await got.post(`${SPACEX_API}/crew/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const crewLaunches = crewMembers.docs.map(async (crew) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.crew.includes(crew.id)) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/crew/${crew.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.crew = true; | ||
}); | ||
await Promise.all(crewLaunches); | ||
|
||
|
||
// Update landpad launches | ||
const landpads = await got.post(`${SPACEX_API}/landpads/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const landpadLaunches = landpads.docs.map(async (landpad) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.cores.filter((c) => c.landpad === landpad.id).length > 0) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/crew/${landpad.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.landpad = true; | ||
}); | ||
await Promise.all(landpadLaunches); | ||
|
||
|
||
// Update launchpad launches | ||
const launchpads = await got.post(`${SPACEX_API}/launchpads/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const launchpadLaunches = launchpads.docs.map(async (launchpad) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.launchpad === launchpad.id) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/launchpads/${launchpad.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.launchpad = true; | ||
}); | ||
await Promise.all(launchpadLaunches); | ||
|
||
|
||
// Update payload launches | ||
const payloads = await got.post(`${SPACEX_API}/payloads/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const payloadLaunches = payloads.docs.map(async (payload) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.payloads.includes(payload.id)) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/payloads/${payload.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.payload = true; | ||
}); | ||
await Promise.all(payloadLaunches); | ||
|
||
|
||
// Update ship launches | ||
const ships = await got.post(`${SPACEX_API}/ships/query`, { | ||
json: { | ||
options: { | ||
pagination: false, | ||
}, | ||
}, | ||
resolveBodyOnly: true, | ||
responseType: 'json', | ||
}); | ||
|
||
const shipLaunches = ships.docs.map(async (ship) => { | ||
const launchIds = launches.docs.filter((launch) => { | ||
if (launch.ships.includes(ship.id)) { | ||
return true; | ||
} | ||
return false; | ||
}).map((launch) => launch.id); | ||
|
||
await got.patch(`${SPACEX_API}/ships/${ship.id}`, { | ||
json: { | ||
launches: launchIds, | ||
}, | ||
headers: { | ||
'spacex-key': KEY, | ||
}, | ||
}); | ||
results.ship = true; | ||
}); | ||
await Promise.all(shipLaunches); | ||
|
||
logger.info(results); | ||
|
||
if (HEALTHCHECK) { | ||
await got(HEALTHCHECK); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
|
||
const { CronJob } = require('cron'); | ||
const webcast = require('./webcast'); | ||
const launches = require('./launches'); | ||
|
||
const webcastJob = new CronJob('*/10 * * * *', webcast); | ||
const launchesJob = new CronJob('*/10 * * * *', launches); | ||
|
||
webcastJob.start(); | ||
launchesJob.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters