Skip to content

Commit

Permalink
Start scraping box scores
Browse files Browse the repository at this point in the history
  • Loading branch information
zachwill committed Jan 10, 2012
1 parent ea8f4e0 commit f3bfa2e
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions scrape.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
_ = require 'underscore'
fs = require 'fs'
repl = require 'repl'
zombie = require 'zombie'

# Database stuff.
Expand Down Expand Up @@ -62,7 +61,7 @@ class Game
plays: "playbyplay?gameId=#{id}"
@data = {}
@period = 1
@scrape id
@scrape(id)

# The scrape method is a little convoluted since the `Zombie.js` requests are
# being sent asynchronously.
Expand All @@ -75,7 +74,36 @@ class Game
boxscore: (browser) ->
$ = Zepto(browser)
tables = $('.mod-data > tbody')
console.log "#{tables.length} tables"
@scrape_stats $, tables

scrape_stats: ($, tables) ->
tables.each (index, table) =>
table = $(table)
rows = table.children()
if index in [0, 3]
# Make sure to recognize the starters.
rows.find('td > a').addClass('primary')
else if index in [2, 5]
# Get rid of the total percentages.
rows = $(rows.first())
rows.each (index, value) ->
stats = $(value).children()
first = stats.eq(0)
player = first.children('a')
if not _.isEmpty player
[name, starter] = [player.text(), player.hasClass('primary')]
player.remove()
position = first.html().match(/, (.*)/)[1]
console.log position.split('-')
switch index
when 0, 1
'away'
when 2
'away total'
when 3, 4
'home'
when 5
'home total'

plays: (browser) ->
$ = Zepto(browser)
Expand Down Expand Up @@ -191,14 +219,13 @@ class Output
return _.extend original, refined


id = '320072031'
day = '20120103'
last = (games) ->
return if _.isEmpty games
console.log games.length
last = _.last games
new Game(last)

do ->
new Day day, last
new Game id
#day = '20120103'
#last = (games) ->
#return if _.isEmpty games
#console.log games.length
#game = _.last games
#new Game game
#new Day day, last
id = "320032539"
new Game(id)

0 comments on commit f3bfa2e

Please sign in to comment.