<commit_before>(function () {
    "use strict";

    const Promise = require('bluebird');
    const apiUsername = require('./apiKeys.js').neverbounce.username;
    const apiSecretKey = require('./apiKeys.js').neverbounce.key;
    const csv = require('fast-csv');
    const fs = require('fs');
    const json2csv = Promise.promisify(require('json2csv'));
    const mailboxLayerAccessKey = require('./apiKeys.js').mailboxLayer.key;
    const NeverBounce = require('neverbounce')({
        apiKey: apiUsername,
        apiSecret: apiSecretKey
    });
    const os = require('os');
    const path = require('path');
    const request = Promise.promisify(require('request'));
    const writeFile = Promise.promisify(fs.writeFile);
    const desktop = path.join(os.homedir(), "Desktop");
    const fileName = process.argv.slice(2).shift();
    const filePath = path.join(desktop, fileName);

    const neverBounceValidate = function (email) {
        return NeverBounce.single.verify(email).then(
            function (result) {
                return {
                    email: email,
                    status: result.getResultTextCode(),
                    statusCode: result.getResult()
                };
            },
            function (error) {
                console.error("errors", error);
            }
        );
    };

    const mailboxLayerValidate = function (email) {
        const mailboxLayerUrl = `http://apilayer.net/api/check`;
        const url = `${mailboxLayerUrl}?access_key=${mailboxLayerAccessKey}&email=${email}`;

        return request({
            url: url,
            json: true
        })
            .then(function (response) {
                return {
                    email: response.body.email,
                    did_you_mean: response.body.did_you_mean,
                    user: response.body.user,
                    domain: response.body.domain,
                    format_valid: response.body.format_valid,
                    mx_found: response.body.mx_found,
                    smtp_check: response.body.smtp_check,
                    catch_all: response.body.catch_all,
                    role: response.body.role,
                    disposable: response.body.disposable,
                    free: response.body.free,
                    score: response.body.score
                };
            });
    };

    const formatToCsv = function (data, fields) {
        return json2csv({
            data,
            fields
        });
    };

    const writeCsvFile = function (fileName, csvData) {
        const fileOutput = path.join(desktop, fileName);

        writeFile(fileOutput, csvData)
            .then(function () {
                console.log(`The ${fileName} was created ${fileOutput}`);
            })
            .catch(function (error) {
                console.error(`There was an error with creating the ${fileName}`, error);
            });
    };

    const validateEmails = function (filePath) {
        const stream = fs.createReadStream(filePath);
        let emails = [];

        csv
            .fromStream(stream)
            .on("data", function (data) {
                if (data.length > 0) {
                    emails.push(data.shift());
                }
            })
            .on("end", function () {
                console.log("Checking Emails", emails);
                console.log("Total emails to check", emails.length);
                console.log("Checking emails....");

                let emailLength = emails.length;

                return Promise.mapSeries(emails, function (email) {
                    console.log('Emails remaining', emailLength);
                    emailLength = emailLength - 1;

                    // return neverBounceValidate(email);
                    return mailboxLayerValidate(email);
                }).then(function (results) {
                    // const csvFieldsNEverBounce = ["email", "status", "statusCode"];
                    const csvFieldsMailboxLayer = ["email", "did_you_mean", "user", "domain", "format_valid", "mx_found", "smtp_check", "catch_all", "role", "disposable", "free", "score"];


                    console.log("DONE!");
                    return formatToCsv(results, csvFieldsMailboxLayer);
                }).then(function (csv) {
                    const resultFileName = fileName.replace('.csv', 'Results.csv');

                    writeCsvFile(resultFileName, csv);
                });
            });
    };

    validateEmails(filePath);
}());<commit_msg>Update strcuture to allow for user to decide which validation service they want to use<commit_after>(function () {
    "use strict";

    const Promise = require('bluebird');
    const csv = require('fast-csv');
    const json2csv = Promise.promisify(require('json2csv'));
    const fs = require('fs');
    const os = require('os');
    const path = require('path');
    const writeFile = Promise.promisify(fs.writeFile);
    const Validators = require('./Validators.js');

    const desktop = path.join(os.homedir(), "Desktop");
    const fileName = process.argv.slice(2).shift();
    const filePath = path.join(desktop, fileName);
    const validationServiceInput = process.argv.slice(3).shift();

    const compileListOfEmailsToValidate = () => {
        const emailFileStream = fs.createReadStream(filePath);
        let emails = [];

        return new Promise(function(resolve, reject) {
            csv
                .fromStream(emailFileStream)
                .on('data', function(data) {
                    if (data.length > 0) {
                        emails.push(data.shift());
                    }
                })
                .on('end', function() {
                    resolve(emails);
                });
            });
    };

    const validationServiceLookup = (validationServiceInput) => {
        if (validationServiceInput == "nb") {
            return Validators.neverBounceValidator;
        }

        if (validationServiceInput == "mbl") {
            return Validators.mailboxLayerValidator;
        }

        throw new Error(`Can't find a validation service for ${validationServiceInput}`);
    };


    const formatToCsv = (data, fields) => json2csv({data, fields});

    const writeCsvFile = function (fileName, csvData) {
        const fileOutput = path.join(desktop, fileName);

        return writeFile(fileOutput, csvData)
            .then(function () {
                console.log(`The ${fileName} was created ${fileOutput}`);
            })
            .catch(function (error) {
                console.error(`There was an error with creating the ${fileName}`, error);
            });
    };

    const sendEmailsToValidationService = function (emails, validator) {

        console.log("Checking Emails", emails);
        console.log("Total emails to check", emails.length);
        console.log("Checking emails....");

        let totalEmails = emails.length;

        return Promise.mapSeries(emails, function (email) {
            console.log('Emails remaining', totalEmails);
            totalEmails = totalEmails - 1;

            return validator.validate(email);
        });
    };


    const validateEmails = function (filePath, validationService) {
        const validator = validationServiceLookup(validationService);

        return compileListOfEmailsToValidate(filePath)
            .then(function (emails){
                if (emails.length < 1) {
                    throw `Can't find any emails in ${filePath}`;
                }

                return sendEmailsToValidationService(emails, validator);
            })
            .then(function (results) {

                console.log("Done checking emails.");
                return formatToCsv(results, validator.csvFields);
            })
            .then(function (csv) {
                const resultFileName = fileName.replace('.csv', 'Results.csv');

                writeCsvFile(resultFileName, csv);
            })
            .catch(console.error)
    };


    validateEmails(filePath, validationServiceInput);
}());
{'fraction_non_alphanumeric': 0.12015839623808934, 'fraction_numerical': 0.001732458854102215, 'mean_word_length': 1.861898016997167, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3029348', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2025, 'n_words': 442}