Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verify script #171

Merged
merged 3 commits into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bin/verify.js
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 19 additions & 5 deletions lib/tasks/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down