Skip to content

Instantly share code, notes, and snippets.

@caged
Created February 9, 2014 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caged/8896322 to your computer and use it in GitHub Desktop.
Save caged/8896322 to your computer and use it in GitHub Desktop.
Convert raw NBA JSON to CSV.
#!/usr/bin/env coffee
fs = require 'fs'
csv = require 'csv'
extend = (object, properties) ->
for key, val of properties
object[key] = val
object
data = fs.readFileSync("./data/team-abbreviations.csv", 'utf8')
.split("\n")
.map((r) -> r.split(','))
data.shift()
teamabbrs = data.map((d) -> o = {team_name: d[0], team_abbreviation: d[1] })
teams = {}
shared = ['GP', 'W', 'L', 'MIN', 'TEAM_ID', 'TEAM_NAME', 'W_PCT']
ignore = ['CFPARAMS', 'CFID']
files = fs.readdirSync './data/teams'
for season in files
continue if season == '.gitkeep'
statfiles = fs.readdirSync "./data/teams/#{season}"
allheaders = []
for mode in statfiles
data = JSON.parse fs.readFileSync "data/teams/#{season}/#{mode}", 'utf8'
params = data.parameters
data = data.resultSets[0]
headers = data.headers
for row, i in data.rowSet
team = {}
for value,j in row
if headers[j] in ignore
hdr = null
else if headers[j] in shared
hdr = headers[j].toLowerCase()
else
stattype = params.MeasureType.toLowerCase().replace(' ', '_')
hdr = "#{stattype}_#{headers[j].toLowerCase()}"
team[hdr] = value if hdr
id = "#{team.team_id}:#{season}"
teams[id] ?= season: season
extend teams[id], team
teams[id].abbr = teamabbrs.filter((d) -> d.team_name == team.team_name)[0].team_abbreviation
headers = Object.keys(teams[Object.keys(teams)[0]])
out = csv().to.path('out/teams.csv', columns: headers, header: true)
for id,team of teams
out.write team
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment