Created
October 4, 2012 16:08
-
-
Save dannypage/3834642 to your computer and use it in GitHub Desktop.
Importing Opta F24 XML files - Will add F7 importing soon.
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
require 'nokogiri' | |
module Import | |
class F24 | |
def self.get_xml_doc | |
print "File Name > " | |
fname = gets.strip | |
pn = "#{::Rails.root.to_s}/db/match_data/#{fname}.xml" | |
puts "Path and File Name: #{pn}" | |
puts "\nOpening and parsing file..." | |
f = File.open(pn) | |
doc = Nokogiri::XML(f) | |
f.close | |
return doc, fname | |
end | |
def self.parse | |
doc, fname = get_xml_doc | |
games = doc.xpath("//Games") | |
games.each do |g| | |
timestamp = g.attributes["timestamp"].value | |
game = g.xpath("./Game") | |
game.each do |ga| | |
id = ga.attributes["id"].value | |
temp = OptaGame.find_by_id(id) | |
if temp | |
oxg = temp | |
puts "Update Game." | |
else | |
oxg = OptaGame.new | |
puts "New Game." | |
end | |
oxg.id = id | |
oxg.timestamp = timestamp | |
oxg.away_team_id = ga.attributes["away_team_id"].value | |
oxg.away_team_name = ga.attributes["away_team_name"].value | |
oxg.home_team_id = ga.attributes["home_team_id"].value | |
oxg.home_team_name = ga.attributes["home_team_name"].value | |
oxg.game_date = ga.attributes["game_date"].value | |
oxg.competition_id = ga.attributes["competition_id"].value | |
oxg.competition_name = ga.attributes["competition_name"].value | |
oxg.matchday= ga.attributes["matchday"].value | |
oxg.period_1_start= ga.attributes["period_1_start"].value | |
oxg.period_2_start= ga.attributes["period_2_start"].value | |
oxg.period_3_start= ga.attributes["period_3_start"].value unless ga.attributes["period_3_start"].nil? | |
oxg.period_4_start= ga.attributes["period_4_start"].value unless ga.attributes["period_4_start"].nil? | |
oxg.period_5_start= ga.attributes["period_5_start"].value unless ga.attributes["period_5_start"].nil? | |
oxg.season_id= ga.attributes["season_id"].value | |
oxg.season_name= ga.attributes["season_name"].value | |
oxg.filename = fname | |
oxg.save! | |
timeline = 0 | |
events = ga.xpath("./Event") | |
events.each do |e| | |
id = e.attributes["id"].value | |
temp = OptaEvent.find_by_id(id) | |
if temp | |
event = temp | |
else | |
event = OptaEvent.new | |
end | |
event.opta_game_id = oxg.id | |
event.id = id | |
event.event_id = e.attributes["event_id"].value | |
event.type_id = e.attributes["type_id"].value | |
event.period_id = e.attributes["period_id"].value | |
event.timeline = timeline | |
event.min = e.attributes["min"].value | |
event.sec = e.attributes["sec"].value | |
event.team_id = e.attributes["team_id"].value | |
event.player_id = e.attributes["player_id"].value unless e.attributes["player_id"].nil? | |
event.outcome = e.attributes["outcome"].value | |
event.assist = e.attributes["assist"].value unless e.attributes["assist"].nil? | |
event.keypass = e.attributes["keypass"].value unless e.attributes["keypass"].nil? | |
event.x = e.attributes["x"].value | |
event.y = e.attributes["y"].value | |
event.timestamp = e.attributes["timestamp"].value | |
event.last_modified = e.attributes["last_modified"].value | |
result = event.save! | |
if result | |
timeline = timeline + 1 | |
end | |
quals = e.xpath("./Q") | |
quals.each do |q| | |
id = q.attributes["id"].value | |
temp = OptaQual.find_by_id(id) | |
if temp | |
qual = temp | |
else | |
qual = OptaQual.new | |
end | |
qual.opta_event_id = event.id | |
qual.id = id | |
qual.qualification_id = q.attributes["qualifier_id"].value | |
qual.value = q.attributes["value"].value unless q.attributes["value"].nil? | |
qual.save! | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
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
ActiveRecord::Schema.define(:version => 20120930060013) do | |
create_table "opta_events", :id => false, :force => true do |t| | |
t.integer "opta_game_id", :null => false | |
t.integer "id" | |
t.integer "event_id" | |
t.integer "type_id" | |
t.integer "period_id" | |
t.integer "timeline" | |
t.integer "min" | |
t.integer "sec" | |
t.integer "team_id" | |
t.integer "player_id" | |
t.boolean "outcome" | |
t.integer "assist" | |
t.integer "keypass" | |
t.decimal "x" | |
t.decimal "y" | |
t.datetime "timestamp" | |
t.datetime "last_modified" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
create_table "opta_games", :id => false, :force => true do |t| | |
t.datetime "timestamp", :null => false | |
t.integer "id" | |
t.integer "away_team_id" | |
t.string "away_team_name" | |
t.integer "home_team_id" | |
t.string "home_team_name" | |
t.integer "competition_id" | |
t.string "competition_name" | |
t.datetime "game_date" | |
t.integer "matchday" | |
t.datetime "period_1_start" | |
t.datetime "period_2_start" | |
t.datetime "period_3_start" | |
t.datetime "period_4_start" | |
t.datetime "period_5_start" | |
t.integer "season_id" | |
t.string "season_name" | |
t.string "filename" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
create_table "opta_quals", :id => false, :force => true do |t| | |
t.integer "opta_event_id", :null => false | |
t.integer "id" | |
t.integer "qualification_id" | |
t.string "value" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
end | |
end |
Hi there, do you have a copy of the xml file itself?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yay Ruby!!