Advertisement
balderman

Untitled

Mar 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* tel-o-fun provider */
  2.  
  3. const request = require('request');
  4. const station_attributes = ['data-lat','data-lng','data-id','data-name'];
  5.  
  6. function getStationsData(lat,lng) {
  7.     let results = [];
  8.     let options = {
  9.         method: 'POST',
  10.         url: 'https://telofunonline.info/en/includes/ajax.php',
  11.         headers:
  12.             {
  13.                 'content-type': 'application/x-www-form-urlencoded'
  14.             },
  15.         form:
  16.             {
  17.                 action: 'search',
  18.                 lng: lng,
  19.                 lat: lat
  20.             }
  21.     };
  22.     request(options, function (error, response, body) {
  23.         if (error) throw new Error(error);
  24.         console.log('Incoming stations string: ' + body);
  25.         let start_index = 0;
  26.         let end_index = 0;
  27.         while(true) {
  28.             start_index = body.indexOf('<a',start_index);
  29.             end_index = body.indexOf('</a>',end_index);
  30.             if(start_index == -1 || end_index == -1) {
  31.                 break;
  32.             } else {
  33.                 let station_str = body.substring(start_index,end_index);
  34.                 let attributes = station_attributes.map(function(attr) {
  35.                     let start = station_str.indexOf(attr) + attr.length + 1;
  36.                     let end = start;
  37.                     let end_counter = 0;
  38.                     while(end_counter != 2) {
  39.                         if(station_str.charCodeAt(end) == 39) {
  40.                             end_counter++;
  41.                         }
  42.                         end++;
  43.                     }
  44.                     let value = station_str.substring(start+1,end-1);
  45.                     return value;
  46.                 });
  47.                 results.push(attributes)
  48.                 start_index += 2;
  49.                 end_index += 4;
  50.             }
  51.         }
  52.         results.forEach(function(station) {
  53.             let url = 'https://www.tel-o-fun.co.il/DesktopModules/Locations/StationData.ashx?portalId=1&sid=' + station[2];
  54.             request(url, (err, res, body) => {
  55.                 if (err) { return console.log(err); }
  56.                 let data_start = body.indexOf('<b>');
  57.                 let data_end = body.indexOf('</b>');
  58.                 let data = body.substring(data_start + 3,data_end)
  59.                 let tokens = data.split('&nbsp;');
  60.                 let bikes = tokens[1];
  61.                 let stands = tokens[tokens.length-1];
  62.                 station.push(bikes);
  63.                 station.push(stands);
  64.                 console.log(station);
  65.                 // Station attributes:
  66.                 //   offset 0: lat
  67.                 //   offset 1: lng
  68.                 //   offset 2: id
  69.                 //   offset 3: address
  70.                 //   offset 4: number of available bikes
  71.                 //   offset 5: number of available stands
  72.             });
  73.         });
  74.     });
  75.  
  76. }
  77.  
  78.  
  79.  
  80. if(process.argv.length < 4){
  81.     console.error('Usage: ' + process.argv[1] +  ' lat lng');
  82. } else {
  83.     process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  84.     getStationsData(process.argv[2],process.argv[3])
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement