Class: Worldfootball::Page::Schedule

Inherits:
Worldfootball::Page show all
Defined in:
lib/worldfootball/page_schedule.rb

Overview

note: use nested class for now - why? why not?

Constant Summary collapse

REF_SCORE_RE =

todo/check - rename/use HREF and not REF - why? why not?

%r{^/spielbericht/
([a-z0-9_-]+)/$}x
REF_TEAM_RE =
%r{^/teams/
([a-z0-9_-]+)/$}x
REF_SEASON_RE =
%r{^/alle_spiele/
([a-z0-9_-]+)/$}x

Constants inherited from Worldfootball::Page

GENERATED_RE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Worldfootball::Page

#assert, #debug?, #doc, from_file, #generated, #generated_in_days_ago, #initialize, #keywords, #log, #squish, #title, #url

Constructor Details

This class inherits a constructor from Worldfootball::Page

Class Method Details

.from_cache(slug) ⇒ Object



8
9
10
11
12
# File 'lib/worldfootball/page_schedule.rb', line 8

def self.from_cache( slug )
  url  = Metal.schedule_url( slug )
  html = Webcache.read( url )
  new( html )
end

Instance Method Details

#matchesObject



16
17
18
19
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
48
49
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/worldfootball/page_schedule.rb', line 16

def matches
   @matches ||= begin

# <div class="data">
# <table class="standard_tabelle" cellpadding="3" cellspacing="1">

## note: use > for "strict" sibling (child without any in-betweens)
table = doc.css( 'div.data > table.standard_tabelle' ).first    ## get table
# puts table.class.name  #=> Nokogiri::XML::Element
# puts table.text

assert( table, 'no table.standard_tabelle found in schedule page!!')

trs   = table.css( 'tr' )
# puts trs.size
i = 0

last_date_str = nil
last_round    = nil

rows = []



trs.each do |tr|

## ghost trs? what for? see for an example in bra
##    check for style display:none - why? why not?
##
## <tr class="e2-parent" data-liga_id="88" data-gs_match_id="9062777"
##                         style="display:none;">
##          <td colspan="2"></td>
##          <td colspan="3">
##            <span class="e2" data-liga_id="88"  data-gs_match_id="9062777"></span>
##          </td>
##          <td colspan="2"></td>
##        </tr>
 ##
 #   <tr class="e2-parent" data-liga_id="530" data-gs_match_id="10259222"
 #       style="display:none;">
 ##    <td colspan="2"></td>
 ##    <td colspan="3">
 ##      <span class="e2" data-liga_id="530" data-gs_match_id="10259222"></span>
 ##    </td>
 ##    <td colspan="2"></td>
 ##  </tr>
  if tr['style'] && tr['style'].index( 'display') &&
                    tr['style'].index( 'none')
    puts "!! WARN: skipping ghost line >#{tr.text.strip}<"
    next
  end


  i += 1

   ## puts "[debug] row #{i} >#{tr.text.strip}<"

   ### note - assume for now match lines use tds
   ##            and round lines use ths (NOT tds)!!
   ##  e.g. <th colspan="7">8. Spieltag</th>

   ths =  tr.css( 'th' )
   tds =  tr.css( 'td' )

  if tr.text.strip =~ /Spieltag/ ||
     tr.text.strip =~ /[1-9]\.[ ]Runde|
                          Qual\.[ ][1-9]\.[ ]Runde|  # see EL or CL Quali
                          Qualifikation|     # see CA Championship
                          Sechzehntelfinale|   # see EL
                          Achtelfinale|
                          Viertelfinale|
                          Halbfinale|
                          Finale|
                          Gruppe[ ][A-Z]|    # see CL
                          Gruppe[ ][1-9]|    # see slv-primera-division-2020-2021-clausura_3
                          Playoffs|           # see EL Quali
                          Entscheidungsspiel|   # see Serie A 2022-23 Entscheidung Abstieg
                          Spiele|                # see Serie A 1960-61 Relegation
                          3\.[ ]Platz|      # see bra-serie-a-2000-yellow-module-playoffs
                          Spiel[ ]um[ ]Platz[ ]3|  # see campeonato-2009-cuadrangulares-deportivo-cuenca-cs-emelec
                          Relegation|     # see egy-premiership-2013-2014-abstiegsplayoff
                          Copa[ ]Libertadores|  # see ecu-campeonato-2012-segunda-etapa-playoffs
                          Copa[ ]Sudamericana|  # see campeonato-2012-liguilla-final-playoffs-cs-emelec-ldu-quito
                          Repechaje|  # see nca-liga-primera-2023-2024-clausura-playoffs
                          Final[ ]de[ ]Grupos|   # see hon-liga-nacional-2020-2021-clausura-playoffs
                          Gran[ ]Final|   # see liga-nacional-2020-2021-apertura-playoffs-finale-olimpia-motagua
                          Finalrunde|  # see hon-liga-nacional-2019-2020-apertura-pentagonal
                          Zona[ ]A|    # see gua-liga-nacional-2020-2021-clausura
                          Zona[ ]B|    # see liga-nacional-2020-2021-clausura-zona-a-comunicaciones-deportivo-malacateco
                          Interzone|    # see liga-nacional-2020-2021-clausura-zona-b-achuapa-sanarate
                          Final[ ]Segunda[ ]Ronda|    # see crc-primera-division-2018-2019-apertura-playoffs
                          Quadrangular      # see crc-primera-division-2016-2017-verano-playoffs
                          /x

    if debug?
      puts
      print '[%03d] ' % i
      ## print squish( tr.text )
      print "round >#{tr.text.strip}<"
      print "\n"
    end

    last_round = tr.text.strip
  elsif ths.count > 0 &&
        tds.count == 0
      ## check for round NOT yet configured!!!
      puts "!! WARN: found unregistered round line >#{tr.text.strip}<"
      log( "!! WARN: found unregistered round line >#{tr.text.strip}< in page #{title}" )

      last_round = tr.text.strip
  else   ## assume table row (tr) is match line

    date_str  = squish( tds[0].text )
    time_str  = squish( tds[1].text )

    date_str = last_date_str    if date_str.empty?

    if debug?
      ## note: for debugging - print as we go along (parsing)
      print '[%03d]    ' % i
      print "%-10s | " % date_str
      print "%-5s | " % time_str
    end

    # was: team1_str = squish( tds[2].text )

    ## <td><a href="/teams/hibernian-fc/" title="Hibernian FC">Hibernian FC</a></td>
    ##  todo/check: check if tooltip title always equals text - why? why not?
    team1_anchor = tds[2].css( 'a' )[0]
    if team1_anchor  # note: <a> might be optional (and team name only be plain text)
      team1_str    = squish( team1_anchor.text )
      team1_ref    = norm_team_ref( team1_anchor[:href] )
    else
      team1_str    = squish( tds[2].text )
      team1_ref    = nil
      puts "!! WARN: no team1_ref for >#{team1_str}< found"
    end

    if debug?
      ## note: for debugging - print as we go along (parsing)
      print "%-22s | " % team1_str
    end

    ##  <td> - </td>
    ## e.g. -
    vs_str =    squish( tds[3].text )  ## use to assert column!!!
    assert( vs_str == '-',  "- for vs. expected; got #{vs_str}")
    ## was: team2_str = squish( tds[4].text )

    ## <td><a href="/teams/st-johnstone-fc/" title="St. Johnstone FC">St. Johnstone FC</a></td>
    team2_anchor = tds[4].css( 'a' )[0]
    if team2_anchor
      team2_str    = squish( team2_anchor.text )
      team2_ref    = norm_team_ref( team2_anchor[:href] )
    else
      team2_str    = squish( tds[4].text )
      team2_ref    = nil
      puts "!! WARN: no team2_ref for >#{team2_str}< found"
    end

    if debug?
      ## note: for debugging - print as we go along (parsing)
      print "%-22s | " % team2_str
    end


    ### was: score_str = squish( tds[5].text )
    ## <a href="/spielbericht/premiership-2020-2021-hibernian-fc-st-johnstone-fc/" title="Spielschema Hibernian FC - St. Johnstone FC">-:-</a>

    score_anchor = tds[5].css( 'a' )[0]
    if score_anchor   ## note: score ref (match report) is optional!!!!
      score_str    = squish( score_anchor.text )
      score_ref    = norm_score_ref( score_anchor[:href] )
    else
      score_str    = squish( tds[5].text )
      score_ref    = nil
    end


    ##  todo - find a better way to check for live match
    ## check for live badge image
    ## <td>
    ##   <img src="https://s.hs-data.com/bilder/shared/live/2.png" /></a>
    ## </td>
    img = tds[6].css( 'img' )[0]
    if img && img[:src].index( '/live/')
      puts "!! WARN: live match badge, resetting score from #{score_str} to -:-"
      score_str = '-:-'  # note: -:- gets replaced to ---
    end

    if debug?
      print "%-10s | " % score_str
      print (score_ref ? score_ref : 'n/a')
      print "\n"
    end

    ## change  2:1 (1:1)  to 2-1 (1-1)
    score_str = score_str.gsub( ':', '-' )

    ## convert date from 25.10.2019 to 2019-25-10

    ## special case for '00.00.0000'
    ##   CANNOT parse
    ##   use empty date - why? why not?

    date     = if date_str == '00.00.0000'
                 nil
               else
                 Date.strptime( date_str, '%d.%m.%Y' )
               end

    ## note: keep structure flat for now
    ##        (AND not nested e.g. team:{text:,ref:}) - why? why not?
    rows << { round:      last_round,
              date:       date ? date.strftime( '%Y-%m-%d' ) : '',
              time:       time_str,
              team1:      team1_str,
              team1_ref:  team1_ref,
              score:      score_str,
              team2:      team2_str,
              team2_ref:  team2_ref,
              report_ref: score_ref
            }

    last_date_str = date_str
  end
 end # each tr (table row)

   rows
  end
end

#norm_score_ref(str) ⇒ Object



309
310
311
312
313
314
315
316
317
# File 'lib/worldfootball/page_schedule.rb', line 309

def norm_score_ref( str )
  ## check ref format / path
  if m=REF_SCORE_RE.match( str )
    m[1]
  else
    puts "!! ERROR: unexpected score href format >#{str}<"
    exit 1
  end
end

#norm_season_ref(str) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/worldfootball/page_schedule.rb', line 337

def norm_season_ref( str )
  ## check ref format / path
  if m=REF_SEASON_RE.match( str )
    m[1]
  else
    puts "!! ERROR: unexpected season href format >#{str}<"
    exit 1
  end
end

#norm_team_ref(str) ⇒ Object



323
324
325
326
327
328
329
330
331
# File 'lib/worldfootball/page_schedule.rb', line 323

def norm_team_ref( str )
  ## check ref format / path
  if m=REF_TEAM_RE.match( str )
    m[1]
  else
    puts "!! ERROR: unexpected team href format >#{str}<"
    exit 1
  end
end

#roundsObject



271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/worldfootball/page_schedule.rb', line 271

def rounds
 @rounds ||= begin
    h = {}
    matches.each do |match|
      rec = h[ match[:round] ] ||= { count: 0,
                                     name: match[ :round] }
       rec[ :count ] += 1
    end

    h.values
 end
end

#seasonsObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/worldfootball/page_schedule.rb', line 285

def seasons
  # <select name="saison" ...
  @seasons ||= begin
     recs = []
     season = doc.css( 'select[name="saison"]').first
     options = season.css( 'option' )

     options.each do |option|
        recs << { text: squish( option.text ),
                  ref:  norm_season_ref( option[:value] )
                }
     end
     recs
  end
end

#teamsObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/worldfootball/page_schedule.rb', line 250

def teams
 @teams ||= begin
    h = {}
    matches.each do |match|
      ## index by name/text for now NOT ref - why? why not?
      [{text: match[:team1],
        ref:  match[:team1_ref]},
       {text: match[:team2],
        ref:  match[:team2_ref]}].each do |team|
        rec = h[ team[:text] ] ||= { count: 0,
                                     name: team[ :text],
                                     ref:  team[ :ref ] }
        rec[ :count ] += 1
        ## todo/check:  check/assert that name and ref are always equal - why? why not?
      end
    end

    h.values
 end
end