Skip to content

Commit

Permalink
Work on downloading and reading GTFS
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Dec 7, 2011
1 parent 03ce7a6 commit 90b1d3a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
43 changes: 43 additions & 0 deletions lib/util/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var url = require('url')
, exec = require('child_process').exec
, fs = require('fs')
, csv = require('csv');

exports.downloadGTFS = function(req, res){
var DOWNLOAD_DIR = './downloads/' + req.params.agency + '/';
var base_url = 'http://www.gtfs-data-exchange.com/agency/';
var file_url = base_url + req.params.agency + '/latest.zip';

// extract the file name
var file_name = url.parse(file_url).pathname.split('/').pop();

// excute wget using child_process' exec function
var wget = 'wget -N -O ' + DOWNLOAD_DIR + 'latest.zip ' + file_url;
var child = exec(wget, function(err, stdout, stderr) {
if (!err){
//unzip file
var unzip = 'unzip ' + DOWNLOAD_DIR + 'latest.zip -d ' + DOWNLOAD_DIR;
var child = exec(unzip, function(err, stdout, stderr) {
if(!err){
res.contentType('application/json');
res.send({
success: true
});
console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);

}
});

} else {
//something didn't work
res.contentType('application/json');
res.send({
success: false
});
}
});
}

exports.parseGTFS = function(req, res){

}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
, "mongoose": ">= 1.1.0"
, "redis": ">= 0.6.7"
, "jade": ">= 0.19.0"
, "csv": ">= 0.0.10"
}
}
30 changes: 23 additions & 7 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
var errors = require('./lib/util/errors');
var errors = require('./lib/util/errors')
, api = require('./lib/util/api');

module.exports = function routes(app){

app.get('/api', function(req, res){
//Routelist
app.get('/api/routes/:agency', function(req, res){

//Spit back variables from URL
for(i in req.query){ console.log(i+": "+req.query[i]); }

res.contentType('application/json');

res.send({
error: 'none'
agency: req.params.agency
, routes: [
{
shortName: '35',
routeName: '35-Eureka',
predictionType: 'schedule'
}
]
});

});

app.all('*', function notFound(req, res, next) {
next(new errors.NotFound);


app.get('/api/routes/download/:agency', api.downloadGTFS);

//Nothing specified
app.all('*', function notFound(req, res) {

res.contentType('application/json');
res.send({
error: 'No API call specified'
});
});

}

0 comments on commit 90b1d3a

Please sign in to comment.