Created
February 1, 2016 03:58
-
-
Save jonsadka/313e1a392f40d0ad3681 to your computer and use it in GitHub Desktop.
Process NFL data
This file contains 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
var fs = require('fs'); | |
var async = require('async'); | |
var dirname = '/Users/jonsadka/Desktop/gamecenter'; | |
var result = []; | |
fs.readdir(dirname + '/json', function(err, filenames){ | |
if (err) {throw(err);} | |
async.eachSeries(filenames, function(filename, cb){ | |
fs.readFile(dirname + '/json/' + filename, 'utf-8', function(err, data){ | |
if (err) {cb(err);} | |
var gameId = filename.replace(/\.json/, '') | |
var game = JSON.parse(data)[gameId]; | |
// date | |
var year = gameId.slice(0, 4); | |
var month = gameId.slice(4, 6); | |
var day = gameId.slice(6, 8); | |
var date = +new Date(year + '-' + month + '-' + day); | |
// score | |
var homeTeam = game.home.abbr; | |
var awayTeam = game.away.abbr; | |
var homeScore = game.home.score; | |
var awayScore = game.away.score; | |
// compile data | |
var data = { | |
gameId: gameId, | |
date: date, | |
home: {team: homeTeam, score: homeScore}, | |
away: {team: awayTeam, score: awayScore} | |
}; | |
result.push(data); | |
console.log('~~processed ', filename); | |
cb(); | |
}) | |
}, function(err){ | |
if (err) {throw(err);} | |
fs.writeFile(dirname + '/gamedata.json', JSON.stringify(result), function(err){ | |
if (err) {throw(err);} | |
console.log('~~finished writing file'); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment