Skip to content
Permalink
Browse files
Work on downloading and reading GTFS
  • Loading branch information
brendannee committed Dec 7, 2011
1 parent 03ce7a6 commit 90b1d3a0e93972b0e8754ca1103b25a8cebdfd7e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
@@ -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){

}
@@ -7,5 +7,6 @@
, "mongoose": ">= 1.1.0"
, "redis": ">= 0.6.7"
, "jade": ">= 0.19.0"
, "csv": ">= 0.0.10"
}
}
@@ -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.