Class: SportDb::Updater

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Models
Defined in:
lib/sportdb/update/updater.rb

Instance Method Summary collapse

Instance Method Details

#map_event_to_dlurl(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sportdb/update/updater.rb', line 20

def map_event_to_dlurl( event )

  league_key = event.league.key
  season_key = event.season.key

  repo_path, folder_path = map_key_to_repo_n_folder_path( league_key )
  
  return nil if repo_path.nil?   # no match/mapping found; cancel download

  season_path = season_key.gsub( '/', '_')  # convert 2013/14 to 2013_14

  #####
  # quick hack!!!!
  # - find something better e.g. more generic/easy to configure etc.
  if league_key == 'world'   # world cup repo mappings include host country e.g. 2014--brazil etc.
    season_path = '2006--germany'       if season_path == '2006'
    season_path = '2010--south-africa'  if season_path == '2010'
    season_path = '2014--brazil'        if season_path == '2014'
  end

  ###
  # e.g. https://raw.github.com/openfootball/at-austria/master/2013_14

  dlurl = "https://raw.github.com/openfootball/#{repo_path}/master"
  dlurl << "/#{folder_path}" if folder_path.present?
  dlurl << "/#{season_path}"
  dlurl
end

#map_key_to_repo_n_folder_path(key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sportdb/update/updater.rb', line 50

def map_key_to_repo_n_folder_path( key )

  ### allow * for regex match w/ .+
  map = [
    [ 'at',         'at-austria'  ],
    [ 'at.*',       'at-austria'  ],
    [ 'de',         'de-deutschland' ],
    [ 'de.*',       'de-deutschland'  ],
    [ 'en',         'en-england' ],
    [ 'es',         'es-espana' ],
    [ 'it',         'it-italy' ],
    [ 'be',         'europe', 'be-belgium' ], # NB: europe/be-belgium
    [ 'ro',         'europe', 'ro-romania' ],
    [ 'cl',         'europe-champions-league' ],
    [ 'el',         'europe-champions-league' ],
    [ 'br',         'br-brazil' ],
    [ 'mx',         'mx-mexico' ],  # todo: add mx.* for clausura etc ??
    [ 'euro',       'euro-cup'  ],
    [ 'world',      'world-cup' ],
    [ 'world.*',    'world-cup' ]]

  map.each do |entry|
     pattern = entry[0]
     path    = [ entry[1], entry[2] ]  # repo n folder path
     
     if pattern.index( '*' ).nil?  # match just plain string (no wildcard *)
       return path if key == pattern
     else
       # assume regex match
       regex = pattern.gsub( '.', '\.' ).gsub( '*', '.+' )
       return path if key =~ /#{regex}/
     end
  end
  nil  # return nil; no match found
end

#runObject



147
148
149
150
151
152
153
154
# File 'lib/sportdb/update/updater.rb', line 147

def run
  # for now update all events (fixtures only) - not *.yml

  Event.all.each do |event|
    update_event( event )
  end

end

#update_event(event) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sportdb/update/updater.rb', line 86

def update_event( event )
  logger.info "update event >>#{event.title}<< (#{event.league.key}+#{event.season.key})"

  dlbase = map_event_to_dlurl( event )
  if dlbase.nil?
    logger.warn "  [Updater] skip download; no download source mapping found for >#{event.key}<"
    return  # cancel download; no mapping found
  end

  puts "  using dlbase >>#{dlbase}<<"

  if event.sources.nil?
    logger.warn "  [Updater] skip download; no download event source configured/found for >#{event.key}<"
    return
  end

  sources = event.sources.gsub(' ','').split(',')   # NB: remove all blanks (leading,trailing,inside)


  text_ary = []   # array of fixtures (text)

  ## collect all fixtures (text)
  sources.each_with_index do |source,i|
    dlurl = "#{dlbase}/#{source}.txt"
    logger.info "   downloading source (#{i+1}/#{sources.length}) >>#{dlurl}<< ..."     # todo/check: use size for ary or length - does it matter?

    # download fixtures into string
    text = Fetcher.read( dlurl )

    logger.debug "text.encoding.name (before): #{text.encoding.name}"
    
    ###
    # NB: Net::HTTP will NOT set encoding UTF-8 etc.
    #  will mostly be ASCII
    #  - try to change encoding to UTF-8 ourselves

    #####
    # NB:  ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here

    ## NB:
    #  for now "hardcoded" to utf8 - what else can we do?
    #  - note: force_encoding will NOT change the chars only change the assumed encoding w/o translation
    text = text.force_encoding( Encoding::UTF_8 )
    logger.debug "text.encoding.name (after): #{text.encoding.name}"

    text_ary << text
  end

  ## note: need to pass in all fixtures at once (e.g as array) for @last_pos calc etc to work
  ##         if multipe "files"/strings are used
  unless text_ary.empty?
    puts "   importing/reading source..."
    # passing dummy include_path (not needed for reading from string)
    # fix: use/add proper api for reading from string e.g. read and read_file ?? etc.
    reader= GameReader.new( '/tmp' )
    ### fix: allow to pass in event (to avoid lookup)
    reader.read_fixtures_from_string( event.key, text_ary )
  end
end