class SportDb::JsonExporter

Constants

CITY_TO_TIMEZONE

hack: for timezone (add timezone to city - fix/fix/fix)

LEAGUE_TO_BASENAME

Public Class Methods

build_clubs( event ) click to toggle source

helpers

# File lib/sportdb/exporters/json_exporter.rb, line 92
def self.build_clubs( event )
  clubs = []
  event.teams.each do |team|
    clubs << { name:    team.name,
               code:    team.code,
               country: team.country.name, }
  end

  data = {
   name:  event.name,
   clubs: clubs
  }

  data
end
build_goals( match ) click to toggle source
# File lib/sportdb/exporters/json_exporter.rb, line 208
def self.build_goals( match )
  goals1 = []
  goals2 = []

  match.goals.each do |goal|
    if goal.team_id == match.team1_id
      goals = goals1
    elsif goal.team_id == match.team2_id
      goals = goals2
    else
      puts "*** team id NOT matching for goal; must be team1 or team2 id"
      exit 1   ## exit - why? why not?
    end

    g = { name:   goal.person.name,
          minute: goal.minute,
        }
    g[:offset]  = goal.offset     if goal.offset != 0
    g[:score1]  = goal.score1
    g[:score2]  = goal.score2
    g[:penalty] = goal.penalty    if goal.penalty
    g[:owngoal] = goal.owngoal    if goal.owngoal
    goals << g
  end
  [goals1, goals2]
end
build_groups( event ) click to toggle source
# File lib/sportdb/exporters/json_exporter.rb, line 109
def self.build_groups( event )
  groups = []
  event.groups.each do |group|
    teams  = []

    if group.teams.empty?
      puts "!! WARN - group #{group.name} has no teams/is empty"
    end

    group.teams.each do |team|
      teams << team.name
    end
    groups << { name: group.name, teams: teams }
  end

  data = {
   name:   event.name,
   groups: groups
  }

  data
end
build_matches( event ) click to toggle source
# File lib/sportdb/exporters/json_exporter.rb, line 133
def self.build_matches( event )
  ## note: no longer list by rounds
  ##  now list by dates and add round as a "regular" field
  ##    note: make round optional too!!!

  matches = []
  event.matches.order( 'date ASC' ).each do |match|
      h = {}

      ## let stage and/or group go first if present/available
      h[ :stage ] = match.stage.name    if match.stage
      h[ :round ] = match.round.name    if match.round
      h[ :group ] = match.group.name    if match.group


      h[ :date  ] = match.date.strftime( '%Y-%m-%d')
      h[ :team1 ] = match.team1.name
      h[ :team2 ] = match.team2.name


      score = {}
      if match.score1 && match.score2
        score[:ft] = [match.score1, match.score2]
      end

      if match.score1et && match.score2et
        score[:et] = [match.score1et, match.score2et]
      end

      if match.score1p && match.score2p
        score[:p]  = [match.score1p, match.score2p]
      end

      h[ :score ] = score   unless score.empty?  ## note: only add if has some data


      unless match.goals.empty?
        goals1, goals2 = build_goals( match )
        h[ :goals1 ] = goals1
        h[ :goals2 ] = goals2
      end


      if match.status
        case match.status
        when Status::CANCELLED
          h[ :status ] = 'CANCELLED'
        when Status::AWARDED
          h[ :status ] = 'AWARDED'
        when Status::ABANDONED
          h[ :status ] = 'ABANDONED'
        when Status::REPLAY
          h[ :status ] = 'REPLAY'
        when Status::POSTPONED
          ## note: add NOTHING for postponed for now
        else
          puts "!! WARN - unknown match status >#{match.status}<:"
          pp match
          h[ :status ] = match.status.downcase  ## print "literal" downcased for now
        end
      end

      matches << h
  end

  data = {
    name:    event.name,
    matches: matches
  }

  data
end
city_to_timezone( city ) click to toggle source
# File lib/sportdb/exporters/json_exporter_more.rb, line 34
def self.city_to_timezone( city )
  CITY_TO_TIMEZONE[ city ] || '?'
end
export( league_key, out_root: ) click to toggle source
# File lib/sportdb/exporters/json_exporter.rb, line 18
def self.export( league_key, out_root: )

  puts "find league >#{league_key}<"
  league = Model::League.find_by( key: league_key )

  if league.nil?
    puts "!! WARN: no league found for >#{league_key}<; skipping export json"
    return
  end


  league.events.each do |event|
     puts "** event:"
     pp event.name
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "matches.count: #{event.matches.count}"


     ## build path e.g.
     ##  2014-15/at.1.clubs.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     league_basename = LEAGUE_TO_BASENAME[ event.league.key ] || event.league.key

     season_basename = event.season.name.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exists?( out_dir )

     ### note:
     ##  skip teams for now if no teams on "top-level"
     ##  - what to do? use all unique teams from matches? yes, yes!!
     ##  - maybe add stages array to team - why? why not?
     ##  -  or use teams by stage?

     ## if empty - try regular season stage
     ##                or apertura stage?


     unless event.teams.empty?
       data_clubs = build_clubs( event )
       ## pp data_clubs
       File.open( "#{out_dir}/#{league_basename}.clubs.json", 'w:utf-8' ) do |f|
         f.write JSON.pretty_generate( data_clubs )
       end
     end

     # note: make groups export optional for now - why? why not?
     unless event.groups.empty?
       data_groups = build_groups( event )
       ## pp data_groups
       File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
         f.write JSON.pretty_generate( data_groups )
       end
     end

     data_matches = build_matches( event )
     ## pp data_matches
     File.open( "#{out_dir}/#{league_basename}.json", 'w:utf-8' ) do |f|
       f.write JSON.pretty_generate( data_matches )
     end
  end
end
export_worldcup( league_key, out_root: ) click to toggle source
# File lib/sportdb/exporters/json_exporter_more.rb, line 39
def self.export_worldcup( league_key, out_root: )

  league = Model::League.find_by_key!( league_key )

  league.events.each do |event|
     puts "** event:"
     pp event.title
     pp event.season
     pp event.league
     puts "teams.count: #{event.teams.count}"
     puts "rounds.count: #{event.rounds.count}"
     puts "groups.count: #{event.groups.count}"
     puts "grounds.count: #{event.grounds.count}"


     grounds = []
     event.grounds.each do |ground|
        grounds << { key:      ground.key,
                     name:     ground.title,
                     capacity: ground.capacity,
                     city:     ground.city.name,
                     timezone: city_to_timezone( ground.city.name ) }
     end

     hash_grounds = {
      name:     event.title,
      stadiums: grounds
     }

     pp hash_grounds


     teams = []
     event.teams.each do |team|
       if team.country.assoc
         continental = {}
         team.country.assoc.parent_assocs.each do |parent|
           ## next if parent.key == 'fifa'  ## skip fifa
           ##  todo/fix: only include / use (single) continental (parent) org/assoc
           ##  find/use continental parent only for now
           if ['caf', 'afc', 'concacaf', 'uefa', 'conmebol', 'ofc'].include? parent.key
             continental = { name: parent.title,
                             code: parent.key.upcase }
           end
         end
         assoc = { key:          team.country.assoc.key,
                   name:         team.country.assoc.title,
                 }
         assoc[ :continental ] = continental   unless continental.empty?
       else
         assoc = {}
       end
       t = { name:      team.title,
             code:      team.code,
             continent: team.country.continent.name }
       t[ :assoc ] = assoc   unless assoc.empty?
       teams << t
     end

     hash_teams = {
      name: event.title,
      teams: teams
     }

     pp hash_teams


     standings = []
     event.groups.each do |group|
       entries = []
       group_standing = Model::GroupStanding.find_by( group_id: group.id )
       if group_standing
         group_standing.entries.each do |entry|
            entries << { team: { name: entry.team.name, code: entry.team.code },
                         pos:           entry.pos,
                         played:        entry.played,
                         won:           entry.won,
                         drawn:         entry.drawn,
                         lost:          entry.lost,
                         goals_for:     entry.goals_for,
                         goals_against: entry.goals_against,
                         pts:           entry.pts
                       }
          end
        end
        standings << { name: group.title, standings: entries }
     end

     hash_standings = {
      name:   event.title,
      groups: standings
     }

     pp hash_standings


     groups = []
     event.groups.each do |group|
       teams  = []
       group.teams.each do |team|
         teams << { name: team.title,
                    code: team.code
                  }
       end
       groups << { name: group.title, teams: teams }
     end

     hash_groups = {
      name: event.title,
      groups: groups
     }

     pp hash_groups


     rounds = []
     event.rounds.each do |round|
       matches = []
       round.games.each do |game|
         m = {        num:  game.pos,    ## use id - why? why not?
                      date: game.play_at.strftime( '%Y-%m-%d'),
                      time: game.play_at.strftime( '%H:%M'),
                      team1: {
                        name: game.team1.title,
                        code: game.team1.code
                      },
                      team2: {
                        name: game.team2.title,
                        code: game.team2.code
                      },
                      score1:    game.score1,
                      score2:    game.score2,
                      score1i:   game.score1i,   # half time / first third (opt)
                      score2i:   game.score2i,   # half time - team 2
                }

                if game.knockout
                  m[ :score1et ] = game.score1et  # extratime - team 1 (opt)
                  m[ :score2et ] = game.score2et  # extratime - team 2 (opt)
                  m[ :score1p  ] = game.score1p   # penalty  - team 1 (opt)
                  m[ :score2p  ] = game.score2p   # penalty  - team 2 (opt) elfmeter (opt)
                  m[ :knockout ] = game.knockout
                end

                unless game.goals.empty?
                  goals1, goals2 = build_goals( game )
                  m[ :goals1 ] = goals1
                  m[ :goals2 ] = goals2
                end

                if game.group
                  m[ :group ]    =  game.group.title
                end

                if game.ground
                  m[ :stadium  ] = { key: game.ground.key, name: game.ground.title }
                  m[ :city     ] = game.ground.city.name
                  m[ :timezone ] = city_to_timezone( game.ground.city.name )
                end

          matches << m
       end

       rounds << { name: round.title, matches: matches }
     end

     hash_matches =  {
       name: event.title,
       rounds: rounds
     }

     pp hash_matches


     ## build path e.g.
     ##  2014-15/at.1.clubs.json
     ##  2018/worldcup.teams.json

     ##  -- check for remapping (e.g. add .1); if not found use league key as is
     league_basename = LEAGUE_TO_BASENAME[ event.league.key ] || event.league.key

     season_basename = event.season.title.sub('/', '-')  ## e.g. change 2014/15 to 2014-15


     out_dir   = "#{out_root}/#{season_basename}"
     ## make sure folders exist
     FileUtils.mkdir_p( out_dir ) unless Dir.exists?( out_dir )

     File.open( "#{out_dir}/#{league_basename}.stadiums.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_grounds )
     end

     File.open( "#{out_dir}/#{league_basename}.teams.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_teams )
     end

     File.open( "#{out_dir}/#{league_basename}.groups.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_groups )
     end

     File.open( "#{out_dir}/#{league_basename}.standings.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_standings )
     end

     File.open( "#{out_dir}/#{league_basename}.json", 'w' ) do |f|
       f.write JSON.pretty_generate( hash_matches )
     end
  end
end