|
| 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"; |
0 commit comments