Skip to content

Instantly share code, notes, and snippets.

@kbkk
Created November 16, 2017 00:23
Show Gist options
  • Save kbkk/ebabd11ee732703d20d033457b1b9024 to your computer and use it in GitHub Desktop.
Save kbkk/ebabd11ee732703d20d033457b1b9024 to your computer and use it in GitHub Desktop.
process((params, emit) => {
const zip = new JSZip();
const countryBlocksZip = 'GeoLite2-Country-CSV_20171003/GeoLite2-Country-Blocks-IPv4.csv';
const countryLocationsZip = 'GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv';
emit(0);
const parseFile = (zipFile, emitPre, emitPost) => {
return zip
.file(zipFile)
.async('string')
.then(data => {
emit(emitPre);
return new Promise((resolve, reject) => {
Papa.parse(data, {
header: true,
error: reject,
complete: results => {
emit(emitPost);
resolve(results);
}
})
});
})
};
zip
.loadAsync(params.data)
.then(zipContent => Promise.all([
parseFile(countryBlocksZip, 25, 50),
parseFile(countryLocationsZip, 75, 100)
]))
.then(([ipv4, meta]) => {
const dist = {};
meta.data.forEach(geo => {
dict[geo.geoname_id] = geo.country_name
});
return {
ipv4: ipv4.data,
meta: _.sortBy(meta.data, 'country_name'),
dict: dict
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment