Class: SportDb::Updater

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

Constant Summary

Constants included from Model

Model::City, Model::Continent, Model::Country, Model::Prop, Model::Region

Instance Method Summary collapse

Instance Method Details

#map_event_to_dlurl(event) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sportdb/updater.rb', line 14

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

  ###
  # 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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sportdb/updater.rb', line 34

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



123
124
125
126
127
128
129
130
# File 'lib/sportdb/updater.rb', line 123

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/sportdb/updater.rb', line 70

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

  dlbase = map_event_to_dlurl( event )
  if dlbase.nil?
    puts "  skip download; no download source mapping found"
    return  # cancel download; no mapping found
  end

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

  if event.sources.nil?
    puts "  skip download; no download event source configured/found"
    return
  end

  sources = event.sources.gsub(' ','').split(',')   # NB: remove all blanks (leading,trailing,inside)
  sources.each_with_index do |source,i|
    dlurl = "#{dlbase}/#{source}.txt"
    puts "   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}"


    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' )

    ## todo/fix: offer a version that lets us pass in event (not event.key)
    #    no need for another event lookup
    reader.read_fixtures_from_string( event.key, text )
  end
end