Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let num_days = cmd_read()
  2.  
  3. let counter = 0
  4. let current_date = new Date(process.argv[2])
  5.  
  6.  
  7.  
  8. while (counter < num_days) {
  9.     // date format conversion used for query string parameter in api call (yyyymmdd)
  10.     Date.prototype.yyyymmdd = function() {
  11.         var mm = this.getMonth() + 1; // getMonth() is zero-based
  12.         var dd = this.getDate();
  13.      
  14.         return [this.getFullYear(),
  15.                 (mm>9 ? '' : '0') + mm,
  16.                 (dd>9 ? '' : '0') + dd
  17.                ].join('');
  18.     };
  19.  
  20.         // query string necessary in proper format for API call
  21.     querystring = current_date.yyyymmdd();
  22.     console.log(`Fetching data for ${current_date}`)
  23.     api_call(querystring, function(response) {
  24.         console.log(`Data Found: Decompressing zip file...${current_date}`)
  25.         getGzipped(response, function(err, data) {
  26.  
  27.             const fs = require('fs')
  28.             fs.writeFile("input.txt", data, function(err) {
  29.                 if (err) {
  30.                     return console.log(err);
  31.                 }
  32.                 console.log(`Parsing through Speedcurve data for ${current_date}`)
  33.                 text_parser("input.txt", current_date, counter)
  34.                 console.log(counter)
  35.             })
  36.         });
  37.  
  38.     })
  39.  
  40.     current_date.setDate(current_date.getDate() + 1)
  41.     counter ++
  42. }
  43.  
  44.  
  45. // function parses through raw data for given date and only pulls out Custom Vars via regex
  46. function text_parser(input_file, current_date, day_num) {
  47.  
  48.     var fs = require('fs');
  49.  
  50.     var file = fs.readFile(input_file, 'utf8', function(err, doc) {
  51.         // regex pattern to find custom data
  52.         var invoca = doc.match(/invocaPhoneNumber\|([^,]*)/g);
  53.         var alliance = doc.match(/allianceId\|([^,]*)/g);
  54.         var ehealth = doc.match(/eHealthPhone\|([^,]*)/g);
  55.         var fs1 = require('fs');
  56.         console.log('Generating data to report file - output.csv')
  57.         if (day_num == 1) {
  58.             const writeStream = fs1.createWriteStream('output.csv');
  59.             writeStream.write(`Alliance ID, Invoca Number, eHealth Number, Date\n`);
  60.             alliance.forEach(value => writeStream.write(`${value.slice(11)}, ${invoca[alliance.indexOf(value)].slice(18)}, ${ehealth[alliance.indexOf(value)].slice(13)}, ${current_date}\n`));
  61.            
  62.         }
  63.         // else condition simply adds onto existing output report file
  64.         else {            
  65.             alliance.forEach(value => fs.appendFile("output.csv",`${value.slice(11)}, ${invoca[alliance.indexOf(value)].slice(18)}, ${ehealth[alliance.indexOf(value)].slice(13)}, ${current_date}\n`));
  66.        
  67.         }
  68.        
  69.     });
  70. }
  71.  
  72.  
  73. function api_call(querystring, callback) {
  74.     var request = require("request");
  75.  
  76.     var options = { method: 'GET',
  77.         url: 'https://api.speedcurve.com/v1/lux/export',
  78.         qs: { date:   querystring },
  79.         headers:
  80.             {Connection: 'keep-alive',
  81.  
  82.             Host: 'api.speedcurve.com',
  83.             'Postman-Token': '9058574c-529e-40ec-9781-0fbfa6720472',
  84.             'Cache-Control': 'no-cache',
  85.             Accept: '*/*',
  86.             'User-Agent': 'PostmanRuntime/7.15.2',
  87.      
  88.  
  89.             Authorization: 'Basic N3J0cGkwNHo4OXkybW14ajBsY2FhcWt5cjE0bzFzOng='  
  90.         }
  91.     };
  92.  
  93.     process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  94.  
  95.     request(options, function (error, response, body) {
  96.         if (error) throw new Error(error);
  97.  
  98.         const regex = /(?<=:")(.*)(?="})/gm;
  99.         let m;
  100.  
  101.         while ((m = regex.exec(body)) !== null) {
  102.         // This is necessary to avoid infinite loops with zero-width matches
  103.             if (m.index === regex.lastIndex) {
  104.                 regex.lastIndex++;
  105.             }
  106.             var download_link = m[0]
  107.             return callback(download_link);
  108.         }
  109.  
  110.     });
  111.  
  112. }
  113.  
  114. function getGzipped(url, callback) {
  115.     // buffer to contain streamed decompression
  116.     var https = require("https"),
  117.     zlib = require("zlib");
  118.     var buffer = [];
  119.  
  120.     https.get(url, function(res) {
  121.         // pipe the response into the gunzip to decompress
  122.         var gunzip = zlib.createGunzip();            
  123.         res.pipe(gunzip);
  124.  
  125.         gunzip.on('data', function(data) {
  126.          // decompression chunk ready, add it to the buffer
  127.             buffer.push(data.toString())
  128.  
  129.         }).on("end", function() {
  130.          // response and decompression complete, join the buffer and return
  131.          callback(null, buffer.join(""));
  132.  
  133.         }).on("error", function(e) {
  134.          callback(e);
  135.         })
  136.     }).on('error', function(e) {
  137.      callback(e)
  138.     });
  139. }
  140.  
  141.  
  142.  
  143. // function sees which date(s) user wants to access and acts on it
  144. function cmd_read() {
  145.     if (process.argv.length < 3) {
  146.         console.log('invalid input')
  147.         process.exit(0)
  148.        
  149.     }
  150.     if (process.argv.length == 3) { // only one date provided to search
  151.         return 1
  152.  
  153.     }
  154.     if (process.argv.length == 4) { // find date difference to extract data
  155.         var start = process.argv[2].split('/')
  156.         var end = process.argv[3].split('/')
  157.         var startDate = new Date(start[2], start[0]-1, start[1])
  158.         var endDate = new Date(end[2], end[0]-1, end[1])
  159.         daysbetween = 1 + Math.round((endDate-startDate)/(1000*60*60*24))
  160.         return daysbetween  
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement