<commit_before>'use strict';

var Nightmare = require('nightmare');
var camelCase = require('camel-case');
var _ = require('lodash');

var output = {};

var app = new Nightmare()
  .goto('http://teamtreehouse.com/tracks')
  .evaluate(getTrackInfo, function(tracks){
    _.each(tracks, function(track){
      setTrackInfo(track);
    });
  })
  .use(function(){
    _.each(output, function(track){
      var url = 'http://teamtreehouse.com' + track.url;

      app
        .use(function(){
          console.log('Loading Url:', url);
        })
        .goto(url)
        .screenshot(track.name + '.png');
    });
  });

app.run(function (err, nightmare) {
  if (err) return console.log(err);
  console.log('Done!', output);
});

function getTrackInfo(){
  return $('.track').map(function(i, track){
    var info = {};
    var $track = $(track);
    var classList = $track.attr('class').split(' ');

    info.url = $track.find('a.title').attr('href');
    info.area = _.difference(classList, ['track', 'card'])[0];
    info.name = $track.find('a.title h3').text();

    return info;
  }).toArray();
}

function setTrackInfo(trackInfo){
  var key = trackUrlToKey(trackInfo.url);

  output[key] = trackInfo;

  return trackInfo;
}

function trackUrlToKey(url){
  var hypenatedKey = url.split('/').pop();
  return camelCase(hypenatedKey);
}
<commit_msg>camelCase all keys and output JSON<commit_after>'use strict';

var Nightmare = require('nightmare');
var camelCase = require('camel-case');
var _ = require('lodash');

var output = {};

var app = new Nightmare()
  .goto('http://teamtreehouse.com/tracks')
  .evaluate(getTrackInfo, function(tracks){
    _.each(tracks, function(track){
      setTrackInfo(track);
    });
  })
  .use(function(){
    _.each(output, function(track){
      var url = 'http://teamtreehouse.com' + track.url;

      app
        .use(function(){
          console.log('Loading Url:', url);
        })
        .goto(url)
        .screenshot(track.name + '.png');
    });
  });

app.run(function (err, nightmare) {
  if (err) return console.log(err);

  var camelizedOutput = allKeysToCamelCase(output);
  console.log(JSON.stringify(camelizedOutput));
});

function getTrackInfo(){
  return $('.track').map(function(i, track){
    var info = {};
    var $track = $(track);
    var classList = $track.attr('class').split(' ');

    info.url = $track.find('a.title').attr('href');
    info.area = _.difference(classList, ['track', 'card'])[0];
    info.name = $track.find('a.title h3').text();

    return info;
  }).toArray();
}

function setTrackInfo(trackInfo){
  var key = trackUrlToKey(trackInfo.url);

  output[key] = trackInfo;

  return trackInfo;
}

function trackUrlToKey(url){
  return url.split('/').pop();
}

function allKeysToCamelCase(obj){
  var output = {};

  // Recursively changes object keys to use camelCase
  // similar to the solution at: http://stackoverflow.com/a/10196772
  for (var i in obj) {
    if (Object.prototype.toString.apply(obj[i]) === '[object Object]') {
      output[camelCase(i)] = allKeysToCamelCase(obj[i]);
    } else {
      output[camelCase(i)] = obj[i];
    }
  }

  return output;
}

{'fraction_non_alphanumeric': 0.18490326673009833, 'fraction_numerical': 0.003805899143672693, 'mean_word_length': 3.423562412342216, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4561981', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1045, 'n_words': 211}