Skip to content

Commit 0e49441

Browse files
committed
init
0 parents  commit 0e49441

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
api: node server.js

bvg.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
this.title = "Welcome to bvg webservice!";
2+
this.name = "bvg api module";
3+
this.version = "0.0.1";
4+
this.endpoint = "http://localhost:8080";
5+
6+
var get = require('get');
7+
var util = require('util'),
8+
jjw = require('jjw');
9+
10+
// you can pass an object -- will return an object with the results matching these keys
11+
var scrapers = {
12+
stations: function($) {
13+
var arr = []
14+
$('select.ivu_selectbox option').each(function() {
15+
arr.push($(this).attr('value'));
16+
});
17+
return arr;
18+
}
19+
};
20+
21+
exports.stations = function(options, callback){
22+
//http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil?input=Stromstr.+%28Berlin%29
23+
24+
//console.log(escape(options.input));
25+
26+
var url = 'http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil?input='+escape(options.input),
27+
dl = new get(url);
28+
29+
dl.asBuffer(function(err, data) {
30+
if (err) throw err;
31+
32+
// convert from ISO-8859-1 to UTF-8
33+
var Iconv = require('iconv').Iconv;
34+
var iconv = new Iconv('ISO-8859-1', 'UTF-8');
35+
var buffer = iconv.convert(data);
36+
37+
// do something useful with the buffer
38+
39+
var scrapers = {
40+
choices: function($) {
41+
var arr = [];
42+
$('select.ivu_selectbox option').each(function() {
43+
arr.push($(this).attr('value'));
44+
});
45+
return arr;
46+
},
47+
departures: function($) {
48+
var obj = {};
49+
obj.results = [];
50+
obj.status = "";
51+
$('.ivu_result_box').each(function(){
52+
if($(this).attr('id')=='ivuStreckeninfos') {
53+
obj.status = $.trim($(this).text());
54+
} else {
55+
$(this).find('.ivu_table').each(function(){
56+
$(this).find('tbody tr').each(function(){
57+
var departure = {};
58+
departure.time = $.trim($(this).find('.ivu_table_c_dep').text());
59+
departure.line = $.trim($(this).find('.ivu_table_c_line').text());
60+
departure.direction = $.trim($(this).find('.catlink').text());
61+
obj.results.push(departure);
62+
});
63+
});
64+
}
65+
});
66+
return obj;
67+
}
68+
};
69+
70+
var page = buffer.toString('utf8', 0, buffer.length);
71+
72+
//console.log(page);
73+
74+
jjw(page, scrapers, function(err, result) {
75+
if (err) throw err;
76+
callback(null, result);
77+
});
78+
});
79+
};
80+
81+
exports.stations.description = "Show list of found stations."
82+
exports.stations.schema = {
83+
input: {
84+
type: 'string',
85+
optional: false
86+
}
87+
}
88+
89+
exports.departures = function(options, callback){
90+
callback(null, options.input);
91+
};
92+
exports.departures.description = "this is the echo method, it echos back your msg";
93+
exports.departures.schema = {
94+
input: {
95+
type: 'string',
96+
optional: false
97+
}
98+
};
99+
100+
exports.ping = function(options, callback){
101+
setTimeout(function(){
102+
callback(null, 'pong');
103+
}, 2000);
104+
}
105+
exports.ping.description = "this is the ping method, it pongs back after a 2 second delay";

crawler.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var get = require('get');
2+
3+
var dl = new get('http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil?input=stral').asBuffer(function(err, data) {
4+
if (err) throw err;
5+
console.log(data);
6+
});
7+
8+
/*
9+
var util = require('util'),
10+
jjw = require('jjw')
11+
12+
// you can pass an object -- will return an object with the results matching these keys
13+
var scrapers = {
14+
scripts: function($) {
15+
var arr = [], src
16+
$('script').each(function() {
17+
src = $(this).attr('src')
18+
if (src) arr.push(src)
19+
})
20+
return arr
21+
}
22+
, thumbs: function($) {
23+
var arr = []
24+
$('.pc_img').each(function() {
25+
arr.push($(this).attr('src'))
26+
})
27+
return arr
28+
}
29+
, results: function($) {
30+
return $('.Results').text()
31+
}
32+
}
33+
34+
jjw('http://www.flickr.com/search/?q=homer+simpson', scrapers, function(err, res) {
35+
if (err) throw err
36+
console.log(res)
37+
});
38+
*/

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "bvg-api",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"webservice": "0.5.x",
6+
"get": "0.4.x",
7+
"jjw": "0.0.4"
8+
}
9+
}

server.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var webservice = require('webservice'),
2+
bvg = require('./bvg'),
3+
port = process.env.PORT || 3000;
4+
5+
webservice.createServer(bvg).listen(port);
6+
console.log('BVG json webservice started on port '+port);

0 commit comments

Comments
 (0)