Skip to content

Instantly share code, notes, and snippets.

@zdila
Last active September 7, 2018 11:30
Show Gist options
  • Save zdila/395cecf6dbe169656c43293642a395d2 to your computer and use it in GitHub Desktop.
Save zdila/395cecf6dbe169656c43293642a395d2 to your computer and use it in GitHub Desktop.
earthexplorer.usgs.gov SRTM downloader
const fs = require('fs');
const axios = require('axios');
const parseSetCookie = require('set-cookie-parser');
const querystring = require('querystring');
class Cookies {
constructor() {
this.cookies = {};
}
setCookies(r) {
const setCookie = r.headers['set-cookie'];
if (setCookie) {
parseSetCookie(setCookie).forEach(({ name, value }) => {
this.cookies[name] = value;
});
}
}
getHeader() {
return Object.keys(this.cookies).map(name => `${name}=${this.cookies[name]}`).join('; ');
}
}
async function run() {
const indexResponse = await axios.get('https://ers.cr.usgs.gov/');
const m = indexResponse.data.match(/name="csrf_token" value="([^"]+)"/);
const cookies = new Cookies();
cookies.setCookies(indexResponse);
const loginResponse = await axios.post(
'https://ers.cr.usgs.gov/login/',
querystring.stringify({
username: 'john',
password: 'secret',
csrf_token: m[1],
}),
{
maxRedirects: 0,
headers: {
'content-type': 'application/x-www-form-urlencoded',
cookie: cookies.getHeader(),
},
validateStatus: status => status === 302,
},
);
cookies.setCookies(loginResponse);
const tifResponse = await axios.head(
// far north: https://earthexplorer.usgs.gov/download/4980/GT30W060N90/GEOTIFF/EE
'https://earthexplorer.usgs.gov/download/8360/SRTM1N47E021V3/GEOTIFF/EE',
{
headers: {
cookie: cookies.getHeader(),
},
responseType: 'stream',
},
);
tifResponse.data.pipe(fs.createWriteStream('SRTM1N47E021V3.tif'));
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment