geobase/example.mjs

32 lines
784 B
JavaScript
Raw Normal View History

2023-01-01 09:40:19 +00:00
import fs from 'fs';
import { pipeline } from 'stream/promises';
import { join } from 'path';
import unzipper from 'unzipper';
const GEONAMES_DUMP =
'https://download.geonames.org/export/dump/allCountries.zip';
async function workDirectory() {
const path = await fs.promises.mkdtemp(join(process.cwd(), '.gntmp-'));
return {
path,
remove: async () => {
await fs.promises.rm(path, { recursive: true, force: true });
},
};
}
async function downloadGeodump({ path }) {
const outfile = join(path, 'out.txt');
const httpStream = await fetch(GEONAMES_DUMP);
pipeline(httpStream.body, unzipper.Extract({ path }));
return outfile;
}
async function test() {
const wd = await workDirectory();
await downloadGeodump(wd);
}
test().catch(console.error);