Skip to content

Instantly share code, notes, and snippets.

@ra100
Last active September 9, 2019 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ra100/5cfeccab71e48f9f216d8d26fff444b0 to your computer and use it in GitHub Desktop.
Save ra100/5cfeccab71e48f9f216d8d26fff444b0 to your computer and use it in GitHub Desktop.
Download Astronomy Picture of the Day from NASA for specified dates
#!/usr/bin/env node
/**
* script to get NASA APOD pictures in HD quality for specified period of time
* you need NodeJS v12+ to use shebang, and you need jq and wget
* Get the API key from NASA https://api.nasa.gov/index.html
* usage: NASA_API_KEY='asdg' APOD_FROM=2019-09-09 APOD_TO="2000-01-01" ./apod.js
*/
const {exec} = require('child_process')
const API_KEY = process.env.NASA_API_KEY
const API_URL = `https://api.nasa.gov/planetary/apod?api_key=${API_KEY}`
const getCommand = (path) =>
`curl "${path}" | jq .hdurl | xargs wget -nc | true`
const getAPODdata = (date) =>
new Promise((resolve, reject) => {
const url = `${API_URL}&date=${date}`
exec(getCommand(url), (error, stdout, stderr) => {
if (error) {
console.error(`stderr: ${stderr}`)
return reject(error)
}
return resolve('ok')
})
})
const downloadAPOD = async (startDate, eDate) => {
const now = (startDate && new Date(startDate).getTime()) || Date.now()
const endDate = new Date(eDate || '2018-01-01').getTime()
for (
let timestamp = now;
timestamp >= endDate;
timestamp -= 24 * 60 * 60 * 1000
) {
const date = new Date(timestamp).toISOString().slice(0, 10)
await getAPODdata(date)
console.log('downloaded', date)
}
}
downloadAPOD(
process.env.APOD_FROM || '2017-12-20',
process.env.APOD_TO || '2000-01-01'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment