Advertisement
Guest User

Untitled

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