Forked from DavidWells/parse-csv-and-iterate-with-delay.js
Created
January 2, 2022 00:18
-
-
Save Uvacoder/91c3162898746719962fb6d94c3e3fd7 to your computer and use it in GitHub Desktop.
Parse CSV file and iterate over values with artificial delay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| "csv-parse": "^4.15.3", | |
| "follow-redirects": "^1.13.3", | |
| */ | |
| const path = require('path') | |
| const fs = require('fs').promises | |
| const parse = require('csv-parse') | |
| const { https } = require('follow-redirects') | |
| const parseFullName = require('./src/utils/parse-name') | |
| async function parseCsv(filePath) { | |
| const fileContents = await fs.readFile(filePath) | |
| const output = [] | |
| return new Promise((resolve, reject) => { | |
| parse(fileContents, { | |
| trim: true, | |
| skip_empty_lines: true | |
| }).on('readable', function () { | |
| let record | |
| while (record = this.read()) { | |
| const { first, last } = parseFullName(record[1]) | |
| // Exclude column headers | |
| if (record[0] !== 'Submitted On') { | |
| output.push({ | |
| date: record[0].replace(/^'/, ''), | |
| email: record[2], | |
| ...(first) ? { firstName: first } : {}, | |
| ...(last) ? { lastName: last } : {}, | |
| ...(record[3]) ? { company: record[3] } : {} | |
| }) | |
| } | |
| } | |
| }) | |
| .on('end', async () => { | |
| resolve(output) | |
| }) | |
| .on('error', (err) => { | |
| reject(err) | |
| }) | |
| }) | |
| } | |
| const waitFor = (ms) => new Promise(r => setTimeout(r, ms)) | |
| async function asyncForEach(array, callback) { | |
| for (let index = 0; index < array.length; index++) { | |
| await callback(array[index], index, array) | |
| } | |
| } | |
| function sendRequest(leadData) { | |
| return new Promise((resolve, reject) => { | |
| const options = { | |
| 'method': 'POST', | |
| 'hostname': 'xyz.execute-api.us-east-1.amazonaws.com', | |
| 'path': '/dev/entry', | |
| 'maxRedirects': 20 | |
| } | |
| const req = https.request(options, (res) => { | |
| const chunks = [] | |
| res.on('data', (chunk) => { | |
| chunks.push(chunk) | |
| }) | |
| res.on('end', (chunk) => { | |
| const body = Buffer.concat(chunks) | |
| console.log(body.toString()) | |
| return resolve(body.toString()) | |
| }) | |
| res.on('error', (error) => { | |
| console.error(error) | |
| return reject(error) | |
| }) | |
| }) | |
| const postData = { | |
| 'formName': 'abc', | |
| 'data': { | |
| 'hsFormId': '123', | |
| 'pageUri': 'https://xyz.com', | |
| 'pageName': 'XYZ', | |
| ...leadData | |
| } | |
| } | |
| console.log('postData', postData) | |
| req.write(JSON.stringify(postData)) | |
| req.end() | |
| }) | |
| } | |
| async function processData() { | |
| const file = path.join(__dirname, 'info.csv') | |
| const data = await parseCsv(file) | |
| await asyncForEach(data, async (lead) => { | |
| // Wait for 2 seconds between requests | |
| await waitFor(2000) | |
| console.log(lead) | |
| await sendRequest(lead) | |
| }) | |
| } | |
| processData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment