From c6db1c40c41b1e8886fd8cb0c7908a5b4dc03b7b Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 2 Feb 2017 16:47:45 -0500 Subject: [PATCH 1/3] Install unzipper and through2-sink --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 391ab8b3..1541d876 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,9 @@ "pelias-wof-admin-lookup": "2.11.0", "request": "^2.34.0", "through2": "^2.0.1", - "through2-filter": "^2.0.0" + "through2-filter": "^2.0.0", + "through2-sink": "^1.0.0", + "unzipper": "^0.8.3" }, "devDependencies": { "deep-diff": "^0.3.3", From 339aa004ee71b23a31650e43c9df9943600cb39d Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 2 Feb 2017 16:47:57 -0500 Subject: [PATCH 2/3] Expose internal functions to get specific source stream --- lib/tasks/resolvers.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/tasks/resolvers.js b/lib/tasks/resolvers.js index cda3a88e..82b231f8 100644 --- a/lib/tasks/resolvers.js +++ b/lib/tasks/resolvers.js @@ -7,15 +7,29 @@ var fs = require('fs'), var settings = require('pelias-config').generate(); var basepath = settings.imports.geonames.datapath; -module.exports.selectSource = function(filename) { +function getLocalFileStream(filename) { var localFileName = util.format( '%s/%s.zip', basepath, filename ); - var remoteFilePath = util.format( 'http://download.geonames.org/export/dump/%s.zip', filename ); - if( fs.existsSync( localFileName ) ){ logger.info( 'reading datafile from disk at:', localFileName ); return fs.createReadStream( localFileName); } else { - logger.info( 'streaming datafile from:', remoteFilePath ); - return request.get( remoteFilePath ); + return undefined; } +} + +function getRemoteFileStream(filename) { + var remoteFilePath = util.format( 'http://download.geonames.org/export/dump/%s.zip', filename ); + + logger.info( 'streaming datafile from:', remoteFilePath ); + return request.get( remoteFilePath ); +} + +function selectSource(filename) { + return getLocalFileStream(filename) || getRemoteFileStream(filename); +} + +module.exports = { + getLocalFileStream: getLocalFileStream, + getRemoteFileStream: getRemoteFileStream, + selectSource: selectSource }; From 6d96a92f2ab8f908cadbdde7e5d72a92dcada89c Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 2 Feb 2017 16:48:19 -0500 Subject: [PATCH 3/3] Add verify script --- bin/verify.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 bin/verify.js diff --git a/bin/verify.js b/bin/verify.js new file mode 100644 index 00000000..decaeeee --- /dev/null +++ b/bin/verify.js @@ -0,0 +1,25 @@ +const unzipper = require('unzipper'); +const sink = require('through2-sink'); + +const config = require('pelias-config').generate(); +const resolvers = require( '.././lib/tasks/resolvers' ); + +console.log('verifying local file'); + +const isocode = config.imports.geonames.countryCode; +const filename = isocode === 'ALL' ? 'allCountries' : isocode; +const fileStream = resolvers.getLocalFileStream(filename); + +const unzipStream = unzipper.Parse(); + +unzipStream.on('entry', function(entry) { + console.log(entry.props); + + // pipe every file to a stream that does nothing + entry.pipe(sink(function() {})); +}).on('error', function(err) { + console.error(err); + process.exit(1); +}); + +fileStream.pipe(unzipStream);