-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from brechtvdv/master
Improvements
- Loading branch information
Showing
13 changed files
with
249 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,45 @@ | ||
var ReadStream = require('./lib/ReadStream.js'), | ||
ArrDep2Connections = require('./lib/arrdep2connections.js'), | ||
var ArrDep2Connections = require('./lib/arrdep2connections.js'), | ||
stringify = require('JSONStream').stringify(false), | ||
jsonldstream = require('jsonld-stream'), | ||
param = require('param'), | ||
MongoClient = require('mongodb').MongoClient, | ||
StreamToMongo = require('./lib/StreamToMongo.js'), | ||
fs = require('fs'); | ||
|
||
// Read filename arrivals.json and departure.json from parameters | ||
var arrivalsFilename = './' + process.argv[2]; | ||
var arrivalData = require(arrivalsFilename); | ||
var departuresFilename = './' + process.argv[3]; | ||
var departureData = require(departuresFilename); | ||
// Read filename arrivals.jsonldstream and departure.jsonldstream from parameters | ||
var arrivals = param('arrivals'); | ||
var departures = param('departures'); | ||
|
||
// Initialise streams | ||
var arrivalStream = new ReadStream(arrivalData); | ||
var departureStream = new ReadStream(departureData); | ||
var arrdep2connections = new ArrDep2Connections(arrivalStream); // our transform stream | ||
// Read extra configuration parameters | ||
var options = {}; | ||
options['mongoDb'] = param('mongodb'); | ||
options['mongoDbConfig'] = param('mongoDbConfig'); | ||
options['inbound'] = param('inbound'); | ||
options['outbound'] = param('outbound'); | ||
|
||
departureStream.pipe(arrdep2connections).pipe(stringify).pipe(process.stdout); | ||
// Initialise streams | ||
var arrivalStream = fs.createReadStream(arrivals, {encoding: 'utf8'}).pipe(new jsonldstream.Deserializer()); | ||
var departureStream = fs.createReadStream(departures, {encoding: 'utf8'}).pipe(new jsonldstream.Deserializer()); | ||
var arrdep2connections = new ArrDep2Connections(arrivalStream, options); // our transform stream | ||
|
||
// Check parameter 4 for optional inbound or outbound | ||
if (process.argv[4] && process.argv[4] === '-i') { | ||
var inbound = process.argv[5]; | ||
} else if (process.argv[4] && process.argv[4] === '-o') { | ||
var outbound = process.argv[5]; | ||
} | ||
// Load in MongoDB | ||
if (options.mongoDb === true ) { | ||
var url = 'mongodb://' + options.mongoDbConfig.host + ':' + options.mongoDbConfig.port + '/' + options.mongoDbConfig.database; | ||
|
||
// Check parameter 6 for optional inbound or outbound | ||
if (process.argv[6] && process.argv[6] === '-i') { | ||
var inbound = process.argv[7]; | ||
} else if (process.argv[6] && process.argv[6] === '-o') { | ||
var outbound = process.argv[7]; | ||
} | ||
// First empty collection | ||
MongoClient.connect(url, function(err, db) { | ||
if (err) { | ||
die("Wasn't able to connect to MongoDB server. Check if your server is running.", url); | ||
} | ||
|
||
// TODO: taking care of in/outbound | ||
var collection = db.collection(options.mongoDbConfig.collection); | ||
collection.remove(); // empty the collection | ||
var streamToMongo = new StreamToMongo(collection); | ||
var stream = departureStream.pipe(arrdep2connections).pipe(streamToMongo).on('finish', function () { | ||
db.close(); // close connection | ||
}); | ||
}); | ||
} else { | ||
// Write to stdout | ||
departureStream.pipe(arrdep2connections).pipe(stringify).pipe(process.stdout); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
{ | ||
"@context" : { | ||
"st" : "http://semweb.mmlab.be/ns/stoptimes#", | ||
"Arrival" : "st:Arrival", | ||
"Departure" : "st:Departure", | ||
"gtfs" : "http://vocab.gtfs.org/terms#", | ||
"arrivalTime" : "gtfs:arrivalTime", | ||
"departureTime" : "gtfs:departureTime", | ||
"date" : "http://purl.org/dc/terms/date", | ||
"stop" : { | ||
"@type" : "@id", | ||
"@id" : "gtfs:stop" | ||
} | ||
}, | ||
"trip" : { | ||
"@type" : "@id", | ||
"@id" : "gtfs:trip" | ||
}, | ||
"route" : { | ||
"@type" : "@id", | ||
"@id" : "gtfs:route" | ||
}, | ||
"stopSequence" : "gtfs:stopSequence" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"mongoDbConfig": { | ||
"host": "localhost", | ||
"port": "27017", | ||
"database": "gtfs", | ||
"collection": "connections" | ||
}, | ||
"mongodb": false, | ||
"inbound": "arrdepcontext.json", | ||
"outbound": "connectionscontext.json", | ||
"arrivals": "./arrivals.jsonldstream", | ||
"departures": "./departures.jsonldstream", | ||
"self": "{./development.json}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var Writable = require('stream').Writable; | ||
var util = require('util'); | ||
|
||
function StreamToMongo(collection) { | ||
Writable.call(this, { objectMode: true }); | ||
|
||
this._collection = collection; | ||
} | ||
|
||
util.inherits(StreamToMongo, Writable); | ||
|
||
StreamToMongo.prototype._write = function (obj, encoding, done) { | ||
this._collection.insert(obj, function(err, result) { | ||
if (!err) { | ||
//console.log(result); | ||
done(); | ||
} else { | ||
done(err); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = StreamToMongo; |
Oops, something went wrong.