Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* tel-o-fun provider */
- const request = require('request');
- const station_attributes = ['data-lat','data-lng','data-id','data-name'];
- function getStationsData(lat,lng) {
- let results = [];
- let options = {
- method: 'POST',
- url: 'https://telofunonline.info/en/includes/ajax.php',
- headers:
- {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- form:
- {
- action: 'search',
- lng: lng,
- lat: lat
- }
- };
- request(options, function (error, response, body) {
- if (error) throw new Error(error);
- console.log('Incoming stations string: ' + body);
- let start_index = 0;
- let end_index = 0;
- while(true) {
- start_index = body.indexOf('<a',start_index);
- end_index = body.indexOf('</a>',end_index);
- if(start_index == -1 || end_index == -1) {
- break;
- } else {
- let station_str = body.substring(start_index,end_index);
- let attributes = station_attributes.map(function(attr) {
- let start = station_str.indexOf(attr) + attr.length + 1;
- let end = start;
- let end_counter = 0;
- while(end_counter != 2) {
- if(station_str.charCodeAt(end) == 39) {
- end_counter++;
- }
- end++;
- }
- let value = station_str.substring(start+1,end-1);
- return value;
- });
- results.push(attributes)
- start_index += 2;
- end_index += 4;
- }
- }
- results.forEach(function(station) {
- let url = 'https://www.tel-o-fun.co.il/DesktopModules/Locations/StationData.ashx?portalId=1&sid=' + station[2];
- request(url, (err, res, body) => {
- if (err) { return console.log(err); }
- let data_start = body.indexOf('<b>');
- let data_end = body.indexOf('</b>');
- let data = body.substring(data_start + 3,data_end)
- let tokens = data.split(' ');
- let bikes = tokens[1];
- let stands = tokens[tokens.length-1];
- station.push(bikes);
- station.push(stands);
- console.log(station);
- // Station attributes:
- // offset 0: lat
- // offset 1: lng
- // offset 2: id
- // offset 3: address
- // offset 4: number of available bikes
- // offset 5: number of available stands
- });
- });
- });
- }
- if(process.argv.length < 4){
- console.error('Usage: ' + process.argv[1] + ' lat lng');
- } else {
- process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
- getStationsData(process.argv[2],process.argv[3])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement