class Fbref::Page::Schedule
Constants
- COLUMN_RENAMES
helpers
- SCHED_KS_ID_RE
Public Class Methods
from_cache( league:, season: )
click to toggle source
# 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
Public Instance Methods
match_rows( table, **fixed_columns )
click to toggle source
# 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
matches()
click to toggle source
# 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
stages()
click to toggle source
# 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