Skip to content

Commit 90b1d3a

Browse files
committed
Work on downloading and reading GTFS
1 parent 03ce7a6 commit 90b1d3a

3 files changed

Lines changed: 67 additions & 7 deletions

File tree

lib/util/api.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var url = require('url')
2+
, exec = require('child_process').exec
3+
, fs = require('fs')
4+
, csv = require('csv');
5+
6+
exports.downloadGTFS = function(req, res){
7+
var DOWNLOAD_DIR = './downloads/' + req.params.agency + '/';
8+
var base_url = 'http://www.gtfs-data-exchange.com/agency/';
9+
var file_url = base_url + req.params.agency + '/latest.zip';
10+
11+
// extract the file name
12+
var file_name = url.parse(file_url).pathname.split('/').pop();
13+
14+
// excute wget using child_process' exec function
15+
var wget = 'wget -N -O ' + DOWNLOAD_DIR + 'latest.zip ' + file_url;
16+
var child = exec(wget, function(err, stdout, stderr) {
17+
if (!err){
18+
//unzip file
19+
var unzip = 'unzip ' + DOWNLOAD_DIR + 'latest.zip -d ' + DOWNLOAD_DIR;
20+
var child = exec(unzip, function(err, stdout, stderr) {
21+
if(!err){
22+
res.contentType('application/json');
23+
res.send({
24+
success: true
25+
});
26+
console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);
27+
28+
}
29+
});
30+
31+
} else {
32+
//something didn't work
33+
res.contentType('application/json');
34+
res.send({
35+
success: false
36+
});
37+
}
38+
});
39+
}
40+
41+
exports.parseGTFS = function(req, res){
42+
43+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
, "mongoose": ">= 1.1.0"
88
, "redis": ">= 0.6.7"
99
, "jade": ">= 0.19.0"
10+
, "csv": ">= 0.0.10"
1011
}
1112
}

routes.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
var errors = require('./lib/util/errors');
1+
var errors = require('./lib/util/errors')
2+
, api = require('./lib/util/api');
23

34
module.exports = function routes(app){
45

5-
app.get('/api', function(req, res){
6+
//Routelist
7+
app.get('/api/routes/:agency', function(req, res){
68

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

1012
res.contentType('application/json');
11-
1213
res.send({
13-
error: 'none'
14+
agency: req.params.agency
15+
, routes: [
16+
{
17+
shortName: '35',
18+
routeName: '35-Eureka',
19+
predictionType: 'schedule'
20+
}
21+
]
1422
});
1523

1624
});
17-
18-
app.all('*', function notFound(req, res, next) {
19-
next(new errors.NotFound);
25+
26+
27+
app.get('/api/routes/download/:agency', api.downloadGTFS);
28+
29+
//Nothing specified
30+
app.all('*', function notFound(req, res) {
31+
32+
res.contentType('application/json');
33+
res.send({
34+
error: 'No API call specified'
35+
});
2036
});
2137

2238
}

0 commit comments

Comments
 (0)