<commit_before>/**
 * Created by arminhammer on 10/29/15.
 */

'use strict';

var AWS = require('aws-sdk');
var request = require('request');
//var bluebird = require('bluebird');

var s3 = new AWS.S3();

var env_params = {
    Bucket: 'env.dcfahrt.com',
    Key: '<KEY>'
};

exports.handler = function(event, context) {

    s3.getObject(env_params, function(err, envData) {
        if(err) context.fail(err);
        else {
            console.log('Received env data.');
            console.log(envData);
            var WMATA_API_KEY = JSON.parse(envData.Body.toString()).WMATA_API_KEY;
            console.log(WMATA_API_KEY);

            var options = {
                url: 'https://api.wmata.com/StationPrediction.svc/json/GetPrediction/All',
                headers: {
                    'api_key': WMATA_API_KEY
                }
            };

            request(options, function(error, response, body) {

                if(error) {
                    console.log('There was an error: ' + error);
                    context.fail(error)
                } else {
                    console.log('Received response ' + response.statusCode);
                    console.log(body);
                    context.done("Finished.");
                }

            });


            //context.done();
        }
    });

    /*
    if (!event.cmd) {
        context.fail('Please specify a command to run as event.cmd');
        return;
    }
    child = exec(event.cmd, function(error) {
        // Resolve with result of process
        context.done(error, 'Process complete!');
    });

    // Log process stdout and stderr
    child.stdout.on('data', console.log);
    child.stderr.on('data', console.error);
    */

};

// Lambda Handler
/*module.exports.handler = function(event, context) {



    https.get(options, function(res) {
        var body = '';
        res.on('data', function(chunk) {
            body += chunk;
        });
        res.on('end', function() {
            var s3 = new AWS.S3();

            var params = {
                Bucket: process.env['S3_CACHE_BUCKET'],
                Key: 'dcrailprediction.json',
                ACL: 'public-read',
                Body: body
            };

            s3.putObject(params, function(err, s3Data) {
                if(err) context.fail(err);
                else {
                    context.succeed('Uploaded to S3.');
                }
            });
        });
    }).on('error', function(e) {
        return context.fail(e);
    });
};
    */<commit_msg>Working on promisifying and looping the server-side requests.<commit_after>/**
 * Created by arminhammer on 10/29/15.
 */
'use strict';

var AWS = require('aws-sdk');
var P = require('bluebird');
var s3 = P.promisifyAll(new AWS.S3());
var request = P.promisifyAll(require('request'));

var env_params = {
    Bucket: 'env.dcfahrt.com',
    Key: '<KEY>'
};

exports.handler = function(event, context) {
    var scope = {};
    s3
        .getObjectAsync(env_params)
        .then(function(envData) {
            console.log('Received env data.');
            scope.env = JSON.parse(envData.Body.toString());
            scope.apiParams = {
                url: 'https://api.wmata.com/StationPrediction.svc/json/GetPrediction/All',
                headers: {
                    'api_key': scope.env.WMATA_API_KEY
                }
            };
        })
        .then(function() {
            //console.log(scope);
            var count = 0;
            //console.log(context);
            //console.log(context.getRemainingTimeInMillis());
            function loop() {
                if(count < 7) {
                    count++;
                    console.log('Looping...'+count);
                    setTimeout(loop, 1000);
                } else {
                    //console.log('Finished executing, closing.');
                    context.succeed('Finished.');
                }
            }
            setTimeout(loop, 1000);
            //context.done();
        })
      /*
        .then(request.getAsync(scope.apiParams))
        .then(function(response, body) {
            console.log(scope);
            console.log('Received response ' + response.statusCode);

            var apiData = JSON.parse(body);
            apiData.timestamp = Date.now();

            console.log(apiData.timestamp);

            var cacheParams = {
                Bucket: env.S3_CACHE_BUCKET,
                Key: 'dcrailprediction.json',
                ACL: 'public-read',
                Body: JSON.stringify(apiData)
            };

            s3.putObject(cacheParams, function(err, s3Data) {
                if(err) {
                    console.log('There was an error: ' + err);
                    context.done('There was an error: ' + err);
                }
                else {
                    console.log('Refreshed the rail prediction cache.');
                    context.done('Uploaded to S3.');
                }
            });

        })
        */
        .catch(function(error) {
            context.fail(error);
        });
        //context.done();
};
{'fraction_non_alphanumeric': 0.1374804992199688, 'fraction_numerical': 0.0074102964118564745, 'mean_word_length': 1.4552417424605075, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1662592', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1410, 'n_words': 267}