Class: DbImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/db_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, user, password, db_name) ⇒ DbImporter

team game atbat pitch pitch type game type umpire



19
20
21
# File 'lib/db_importer.rb', line 19

def initialize(host, user, password, db_name)
  @db = PitchfxDbManager.new(host, user, password, db_name)
end

Instance Method Details

#import_for_date(year, month, day) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/db_importer.rb', line 58

def import_for_date(year, month, day)
  games = Game.find_by_date(year, month, day)
  if games && games.length > 0
    games.each do |game|
      import_for_game(game.gid)
    end
  end
end

#import_for_game(gid) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/db_importer.rb', line 68

def import_for_game(gid)
  game = Game.new(gid)
  visitor_id = @db.find_or_create_team(game.visiting_team)
  home_id = @db.find_or_create_team(game.home_team)
  import_players_for_game(gid, visitor_id, home_id)
  game_id = @db.find_or_create_game(game, visitor_id, home_id)
  abs = game.get_atbats
  abs.each do |ab|
    atbat_id = @db.find_or_create_atbat(ab, game_id)
    pitches = ab.pitches
    pitches.each do |pitch|
      @db.find_or_create_pitch(pitch, atbat_id)
    end
  end
  import_pitcher_lines_for_game(gid)
end

#import_for_month(year, month) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/db_importer.rb', line 38

def import_for_month(year, month)
  start_date = Date.new(year.to_i, month.to_i) # first day of month
  end_date = (start_date >> 1)-1 # last day of month
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    import_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end

#import_for_range(year, start_month, start_day, end_month, end_day) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/db_importer.rb', line 48

def import_for_range(year, start_month, start_day, end_month, end_day)
  start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
  end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    import_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end

#import_pitcher_lines_for_date(year, month, day) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/db_importer.rb', line 110

def import_pitcher_lines_for_date(year, month, day)
  games = Game.find_by_date(year, month, day)
  if games && games.length > 0
    games.each do |game|
      import_pitcher_lines_for_game(game.gid)
    end
  end
end

#import_pitcher_lines_for_game(gid) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/db_importer.rb', line 130

def import_pitcher_lines_for_game(gid)
  game = Game.new(gid)
  pitchers = game.get_boxscore.pitchers
  pitchers.each do |h_or_a_pitchers|
    h_or_a_pitchers.each do |pitcher|
      @db.find_or_create_pitcher_line(pitcher, game)
    end
  end
end

#import_pitcher_lines_for_month(year, month) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/db_importer.rb', line 100

def import_pitcher_lines_for_month(year, month)
  start_date = Date.new(year.to_i, month.to_i) # first day of month
  end_date = (start_date >> 1)-1 # last day of month
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    import_pitcher_lines_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end

#import_pitcher_lines_for_range(year, start_month, start_day, end_month, end_day) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/db_importer.rb', line 120

def import_pitcher_lines_for_range(year, start_month, start_day, end_month, end_day)
  start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
  end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    import_pitcher_lines_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end

#import_players_for_game(gid, visitor_id, home_id) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/db_importer.rb', line 86

def import_players_for_game(gid, visitor_id, home_id)
  players = Players.new
  players.load_from_id(gid)
  away = players.rosters[0]
  home = players.rosters[1]    
  away.players.each do |player|
    @db.find_or_create_player(player, visitor_id) 
  end
  home.players.each do |player|
    @db.find_or_create_player(player, home_id)
  end
end

#import_rosters_for_date(year, month, day) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/db_importer.rb', line 177

def import_rosters_for_date(year, month, day)
  games = Game.find_by_date(year, month, day)
  if games && games.length > 0
    games.each do |game|
      import_rosters_for_game(game.gid)
    end
  end
end

#import_rosters_for_game(gid) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/db_importer.rb', line 187

def import_rosters_for_game(gid)
  game = Game.new(gid)
  away_id = @db.find_or_create_team(game.visiting_team)
  home_id = @db.find_or_create_team(game.home_team)
  players = Players.new
  players.load_from_id(gid)
  away = players.rosters[0]
  home = players.rosters[1]     
  roster_ids = @db.find_or_create_rosters(gid, away_id, home_id) 
  away.players.each do |player|
    @db.find_or_create_roster_player(player, roster_ids[0]) 
  end
  home.players.each do |player|
    @db.find_or_create_roster_player(player, roster_ids[1])
  end
end

#import_rosters_for_range(year, start_month, start_day, end_month, end_day) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/db_importer.rb', line 167

def import_rosters_for_range(year, start_month, start_day, end_month, end_day)
  start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
  end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    import_rosters_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end

#import_team_for_month(team_abbrev, year, month) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/db_importer.rb', line 24

def import_team_for_month(team_abbrev, year, month)
  start_date = Date.new(year.to_i, month.to_i) # first day of month
  end_date = (start_date >> 1)-1 # last day of month
  ((start_date)..(end_date)).each do |dt| 
    puts year.to_s + '/' + month.to_s + '/' + dt.day
    team = Team.new('det')
    games = team.games_for_date(year, month, dt.day.to_s)
    games.each do |game|
      import_for_game(game.gid)
    end
  end
end

#set_status_for_date(year, month, day) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/db_importer.rb', line 151

def set_status_for_date(year, month, day)
  games = Game.find_by_date(year, month, day)
  if games && games.length > 0
    games.each do |game|
      set_status_for_game(game.gid)
    end
  end
end

#set_status_for_game(gid) ⇒ Object



161
162
163
164
# File 'lib/db_importer.rb', line 161

def set_status_for_game(gid)
  game = Game.new(gid)
  @db.update_status_for_game(game)
end

#set_status_for_range(year, start_month, start_day, end_month, end_day) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/db_importer.rb', line 141

def set_status_for_range(year, start_month, start_day, end_month, end_day)
  start_date = Date.new(year.to_i, start_month.to_i, start_day.to_i)
  end_date = Date.new(year.to_i, end_month.to_i, end_day.to_i)
  ((start_date)..(end_date)).each do |dt| 
    puts dt.year.to_s + '/' + dt.month.to_s + '/' + dt.day.to_s
    set_status_for_date(dt.year.to_s, dt.month.to_s, dt.day.to_s)
  end
end