Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let num_days = cmd_read()
- let counter = 0
- let current_date = new Date(process.argv[2])
- while (counter < num_days) {
- // date format conversion used for query string parameter in api call (yyyymmdd)
- Date.prototype.yyyymmdd = function() {
- var mm = this.getMonth() + 1; // getMonth() is zero-based
- var dd = this.getDate();
- return [this.getFullYear(),
- (mm>9 ? '' : '0') + mm,
- (dd>9 ? '' : '0') + dd
- ].join('');
- };
- // query string necessary in proper format for API call
- querystring = current_date.yyyymmdd();
- console.log(`Fetching data for ${current_date}`)
- api_call(querystring, function(response) {
- console.log(`Data Found: Decompressing zip file...${current_date}`)
- getGzipped(response, function(err, data) {
- const fs = require('fs')
- fs.writeFile("input.txt", data, function(err) {
- if (err) {
- return console.log(err);
- }
- console.log(`Parsing through Speedcurve data for ${current_date}`)
- text_parser("input.txt", current_date, counter)
- console.log(counter)
- })
- });
- })
- current_date.setDate(current_date.getDate() + 1)
- counter ++
- }
- // function parses through raw data for given date and only pulls out Custom Vars via regex
- function text_parser(input_file, current_date, day_num) {
- var fs = require('fs');
- var file = fs.readFile(input_file, 'utf8', function(err, doc) {
- // regex pattern to find custom data
- var invoca = doc.match(/invocaPhoneNumber\|([^,]*)/g);
- var alliance = doc.match(/allianceId\|([^,]*)/g);
- var ehealth = doc.match(/eHealthPhone\|([^,]*)/g);
- var fs1 = require('fs');
- console.log('Generating data to report file - output.csv')
- if (day_num == 1) {
- const writeStream = fs1.createWriteStream('output.csv');
- writeStream.write(`Alliance ID, Invoca Number, eHealth Number, Date\n`);
- alliance.forEach(value => writeStream.write(`${value.slice(11)}, ${invoca[alliance.indexOf(value)].slice(18)}, ${ehealth[alliance.indexOf(value)].slice(13)}, ${current_date}\n`));
- }
- // else condition simply adds onto existing output report file
- else {
- 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`));
- }
- });
- }
- function api_call(querystring, callback) {
- var request = require("request");
- var options = { method: 'GET',
- url: 'https://api.speedcurve.com/v1/lux/export',
- qs: { date: querystring },
- headers:
- {Connection: 'keep-alive',
- Host: 'api.speedcurve.com',
- 'Postman-Token': '9058574c-529e-40ec-9781-0fbfa6720472',
- 'Cache-Control': 'no-cache',
- Accept: '*/*',
- 'User-Agent': 'PostmanRuntime/7.15.2',
- Authorization: 'Basic N3J0cGkwNHo4OXkybW14ajBsY2FhcWt5cjE0bzFzOng='
- }
- };
- process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
- request(options, function (error, response, body) {
- if (error) throw new Error(error);
- const regex = /(?<=:")(.*)(?="})/gm;
- let m;
- while ((m = regex.exec(body)) !== null) {
- // This is necessary to avoid infinite loops with zero-width matches
- if (m.index === regex.lastIndex) {
- regex.lastIndex++;
- }
- var download_link = m[0]
- return callback(download_link);
- }
- });
- }
- function getGzipped(url, callback) {
- // buffer to contain streamed decompression
- var https = require("https"),
- zlib = require("zlib");
- var buffer = [];
- https.get(url, function(res) {
- // pipe the response into the gunzip to decompress
- var gunzip = zlib.createGunzip();
- res.pipe(gunzip);
- gunzip.on('data', function(data) {
- // decompression chunk ready, add it to the buffer
- buffer.push(data.toString())
- }).on("end", function() {
- // response and decompression complete, join the buffer and return
- callback(null, buffer.join(""));
- }).on("error", function(e) {
- callback(e);
- })
- }).on('error', function(e) {
- callback(e)
- });
- }
- // function sees which date(s) user wants to access and acts on it
- function cmd_read() {
- if (process.argv.length < 3) {
- console.log('invalid input')
- process.exit(0)
- }
- if (process.argv.length == 3) { // only one date provided to search
- return 1
- }
- if (process.argv.length == 4) { // find date difference to extract data
- var start = process.argv[2].split('/')
- var end = process.argv[3].split('/')
- var startDate = new Date(start[2], start[0]-1, start[1])
- var endDate = new Date(end[2], end[0]-1, end[1])
- daysbetween = 1 + Math.round((endDate-startDate)/(1000*60*60*24))
- return daysbetween
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement