Class: Fbref::Page::Schedule

Inherits:
Fbref::Page show all
Defined in:
lib/webget-football/fbref/page_schedule.rb

Overview

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

Constant Summary collapse

SCHED_KS_ID_RE =
%r{(sched_ks_[a-z0-9_]+)}
COLUMN_RENAMES =

helpers

{
  'gameweek' => 'matchweek',
  'squad_a'  => 'team1',
  'squad_b'  => 'team2',
  'notes'    => 'comments',
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fbref::Page

#doc, from_file, #initialize, #title

Constructor Details

This class inherits a constructor from Fbref::Page

Class Method Details

.from_cache(league:, season:) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/webget-football/fbref/page_schedule.rb', line 7

def self.from_cache( league:, season: )
  url = Metal.schedule_url( league: league, season: season )

  ## use - super.from_cache( url ) - why? why not?
  html = Webcache.read( url )
  new( html )
end

Instance Method Details

#match_rows(table, **fixed_columns) ⇒ Object



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
# File 'lib/webget-football/fbref/page_schedule.rb', line 134

def match_rows( table, **fixed_columns )
  rows = []

  trs = table.css( 'tbody tr')
  puts " #{trs.size} row(s):"

  trs.each_with_index do |tr,i|
    print "#{i+1}: "

    if tr.attr('class').to_s.index( 'spacer')
      print "---"
    else
      row = {}
      row = row.merge( fixed_columns )

      tds = tr.css( 'th,td' )
      tds.each do |td|
        key   = td.attr('data-stat')
        value = td.text.strip

        print "#{key}: >#{value}< | "

        ## note: skip expected goal (xg) for now
        next if ['xg_a', 'xg_b'].include?( key )

        ### note: rename some column keys!!
        key =  COLUMN_RENAMES[ key ] if COLUMN_RENAMES[ key ]


        if key == 'score'
          value = value.gsub( '', '-' )  ## translate fancy dash (\u2013)
        end

        if key == 'attendance'
          ## remove thousand seperator e.g.
          ##     24,200  => 24200   etc.
          ##      1,900  =>  1900
          value = value.gsub( ',', '' )
        end

        if key == 'match_report'
          ## get href from embedded anchor link (<a href="">)
          links = td.css('a')
          value = if links.size > 0
                     links[0]['href']
                  else
                    ''   # no (linked) match report found
                  end
        end

        ## note: use ALwAYS symbols for now - why? why not?
        row[ key.to_sym ] = value
      end
      rows << row
    end
    print "\n\n"
  end

  rows
end

#matchesObject



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
# File 'lib/webget-football/fbref/page_schedule.rb', line 61

def matches
  tables = @doc.css( 'table.stats_table' )

  puts " found #{tables.size} table(s):"
  tables.each do |table|
     puts "     table >#{table['id']}<"   ## add no of rows too?
  end

  rows = []
  if tables.size == 2
    ## note:  if tables size == 2 assume   all table and "regular" season table, thus, is the same (missing table for more stages!!!)
    rows += match_rows( tables[1] )
    ## todo/fix: issue a warning about missing table!!!!
    puts "!! WARN: missing stage table(s):"
    pp stages
  elsif tables.size > 1
    stages.each do |stage|
      name     = stage[:name]
      table_id = stage[:table_id]

      table = tables.find {|table| table['id'] == table_id }
      if table
       puts " adding stage >#{name}<..."
       rows += match_rows( table, stage: name )
      else
       puts "!! ERROR: table with id >< for stage >< not found; mismatch sorry"
       exit 1
      end
    end
  else
    ## assume single-table
    rows += match_rows( tables[0] )
  end

  puts " found #{tables.size} table(s):"
  tables.each do |table|
     puts "     table >#{table['id']}<"   ## add no of rows too?
  end

  rows
end

#stagesObject



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
# File 'lib/webget-football/fbref/page_schedule.rb', line 18

def stages

  stages = []
  buttons = doc.css( 'div#all_sched_ks_all div.section_heading div.sub_section_heading button' )
  puts "#{buttons.size} button(s):"
  ## pp buttons

  buttons.each do |button|
    name     = button.text
    table_id = ''
    if button['onclick'].to_s =~ SCHED_KS_ID_RE
       table_id = $1
    else
       puts "!! ERROR - no table id found"
       exit 1
    end

    if table_id == 'sched_ks_all'  ## skip all-in-one
    else
      stages << { name:     name,
                  table_id: table_id }
    end
  end

=begin
  <div class="section_heading">
    <span class="section_anchor" id="sched_ks_all_link" data-label="Scores & Fixtures" data-no-inpage="1"></span><h2>Scores & Fixtures <span style="color: #777; font-size:smaller">2019-2020 Austrian Bundesliga</span></h2>    <div class="section_heading_text">
        <ul><li><span class="shim"></span></li>
        </ul>
      </div>
      <div class="sub_section_heading">
      <button onclick="setTimeout(function(){sr_st_construct_stats_table_features('sched_ks_all'); }, 100);" data-hide="[id^=all_sched_ks]" data-show="#all_sched_ks_all">All Rounds</button>
      <button onclick="setTimeout(function(){sr_st_construct_stats_table_features('sched_ks_3213_1'); }, 100);" data-hide="[id^=all_sched_ks]" data-show="#all_sched_ks_3213_1">Regular Season</button>
      <button onclick="setTimeout(function(){sr_st_construct_stats_table_features('sched_ks_3213_2'); }, 100);" data-hide="[id^=all_sched_ks]" data-show="#all_sched_ks_3213_2">Championship round</button>
      <button onclick="setTimeout(function(){sr_st_construct_stats_table_features('sched_ks_3213_3'); }, 100);" data-hide="[id^=all_sched_ks]" data-show="#all_sched_ks_3213_3">Relegation round</button>
      <button onclick="setTimeout(function(){sr_st_construct_stats_table_features('sched_ks_3213_4'); }, 100);" data-hide="[id^=all_sched_ks]" data-show="#all_sched_ks_3213_4">Europa League play-offs</button>
  </div>
=end
  stages
end