Created
July 10, 2009 16:44
-
-
Save bernsno/144610 to your computer and use it in GitHub Desktop.
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
module PFR | |
class CareerStat < ActiveRecord::Base | |
tableless :columns => [[:player_id, :integer], | |
[:season_id, :integer], | |
[:stat_name_id, :integer], | |
[:value, :string]] | |
belongs_to :player | |
belongs_to :season | |
belongs_to :stat_name | |
def self.new_from_url(url) | |
# url should be the player's base page | |
agent = WWW::Mechanize.new { |a| a.user_agent_alias = 'Mac Safari' } | |
agent.get(url) do |page| | |
player = self.player_from_page(page) | |
Table_id_and_Stat_Hash.keys.each do |table, hash| | |
page.search("table[id=#{table}] tbody tr").each do |row| | |
cells = row.search('td').collect(&:content) | |
if season = season_from_cell(cells) | |
career_stats_from_cells(hash, cell) | |
end | |
end | |
end | |
end | |
end | |
def self.career_stats_from_cells(career_stat_hash, cell) | |
career_stat_hash.each do |key, value| | |
if stat_name = StatName[key] | |
cs = ::CareerStat.find_or_create_by_player_id_and_season_id(:player_id => player.id, :season_id => season.id) | |
cs.update_attributes!(:stat_name => stat_name, :value => cells[value]) | |
end | |
end | |
end | |
def self.season_from_cell(cells) | |
year = cells[0].match(/\d+/)[0] | |
season = Season.for_date(year) | |
end | |
def self.player_from_page(page) | |
player = PFR::Player.new_from_page(page) | |
::Player.find_by_first_name_and_last_name(player.first_name, player.last_name) | |
end | |
Table_id_and_Stat_Hash = { | |
"passing" => Passing, | |
"receiving_and_rushing" => Receiving_and_Rushing, | |
"rushing_and_receiving" => Rushing_and_Receiving | |
} | |
# Replace these 'Game Stat Names' with 'Stat Names' | |
Receiving_and_Rushing = { | |
"Games Played" => 5, | |
"Games Started" => 6, | |
"Receptions Total" => 7, | |
"Reception Yards" => 8, | |
"Yards per Reception" => 9, | |
"Reception Touchdowns" => 10, | |
"Longest Reception" => 11, | |
"Carries" => 12, | |
"Rush Yards" => 13, | |
"Rush Yards per Attempt" => 14, | |
"Rush Touchdowns" => 15, | |
"Fumbles" => 22 | |
} | |
Rushing_and_Receiving = { | |
"Games Played" => 5, | |
"Games Started" => 6, | |
"Carries" => 7, | |
"Rush Yards" => 8, | |
"Rush Touchdowns" => 9, | |
"Longest Rush" => 10, | |
"Rush Yards per Attempt" => 11, | |
"Receptions Total" => 14, | |
"Reception Yards" => 15, | |
"Yards per Reception" => 16, | |
"Reception Touchdowns" => 17, | |
"Fumbles" => 23 | |
} | |
Passing = { | |
"Games Played" => 5, | |
"Games Started" => 6, | |
"Completions" => 8, | |
"Attempts" => 9, | |
"Percentage of Passes Completed" => 9, | |
"Pass Yards" => 10, | |
"Pass Touchdowns" => 11, | |
"Interceptions" => 13, | |
"Yards per Attempt" => 14, | |
"Longest Pass" => 15, | |
"Sacks" => 21, | |
"Rating" => 20, | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment