Class: Worldfootball::Page::Report

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

Overview

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

Constant Summary

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_report.rb', line 8

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

Instance Method Details

#find_table_toreObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/worldfootball/page_report.rb', line 16

def find_table_tore
  # <table class="" cellpadding="3" cellspacing="1">
  # <tr>
  #   <td colspan="2" class="ueberschrift" align="center">Tore</td>
  # </tr>

 ## get table
 ##   first table row is Tore
  tables = doc.css( 'table.standard_tabelle' )
  # puts "  found #{tables.size} table.standard_tabelle" # e.g. found 6 table.standard_tabelle
  tables.each do |table|
    trs = table.css( 'tr' )
    ## puts "     found #{trs.size} trs"
    tds = trs[0].css( 'td' )
    ## puts "     found #{tds.size} tds"

    if tds.size > 0 && tds[0].text == 'Tore'
      return table
    end
  end

  nil  ## nothing found; auto-report error -why? why not?
end

#goalsObject



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
# File 'lib/worldfootball/page_report.rb', line 40

def goals
   @goals ||= begin

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

# puts table.class.name  #=> Nokogiri::XML::Element
# puts table.text

table  = find_table_tore
## pp table

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



rows = []
last_score1 = 0
last_score2 = 0

trs.each_with_index do |tr,i|

  next if i==0   # skip Tore headline row

  break if i==1 && tr.text.strip == 'keine'  ## assume 0:0 - no goals

# <tr>
#  <td class="hell" align="center" width="20%">
#   <b>0 : 1</b>
#  </td>
#  <td class="hell" style="padding-left: 50px;">
#   <a href="/spieler_profil/luis-phelipe/" title="Luis Phelipe">Luis Phelipe</a> 34. / Rechtsschuss								&nbsp;(<a href="/spieler_profil/alexander-prass/" title="Alexander Prass">Alexander Prass</a>)
# </td>
# </tr>

      tds = tr.css( 'td' )

    score_str  = squish( tds[0].text )

    player_str = squish( tds[1].text )

    print '[%03d] ' % i
    print score_str
    print " | "
    print player_str
    print "\n"

    score_str = score_str.gsub( ':', '-' )
    score_str = score_str.gsub( ' ', '' )   ## remove all white space


    ### todo/fix: use new Score.split helper here
    ## score1, score2 = Score.split( score_str )
    parts = score_str.split('-')
    score1 = parts[0].to_i
    score2 = parts[1].to_i

    if last_score1+1 == score1 && last_score2 == score2
      team = 1
    elsif last_score2+1 == score2 && last_score1 == score1
      team = 2
    else
      puts "!! ERROR - unexpected score advance (one goal at a time expected):"
      puts "  #{last_score1}-#{last_score2}=> #{score1}-#{score2}"
      exit 1
    end


    last_score1 = score1
    last_score2 = score2



    if player_str.index('/')
      parts = player_str.split('/')
      # pp parts
      notes = parts[1].strip

      if parts[0].strip =~ /^([^0-9]+)[ ]+([0-9]+)\.$/
        player_name = $1
        goal_minute = $2
         # puts " >#{player_name}< | >#{goal_minute}<"
      else
        puts "!! ERROR - unknown goal format (in part i):"
        puts player_str
        pp parts
        exit 1
      end
     else  # (simple line with no divider (/)
       #  Andrés Andrade 88.  (Nicolas Meister)
       if m = %r{^([^0-9]+)
                 [ ]+
                ([0-9]+)\.
                 (?:
                   [ ]+
                   (\([^)]+\))
                 )?
               $}x.match( player_str )
           player_name = m[1]
           goal_minute = m[2]
           notes       = m[3] ? m[3] : ''
       else
         puts "!! ERROR - unknown goal format:"
         puts player_str
         exit 1
       end
     end


    ## check for "flags" e.g. own goal or penalty
    ##   if found - remove from notes (use its own flag)
    owngoal = false
    penalty = false

   if notes.index( 'Eigentor' )
      owngoal = true
      notes = notes.sub('Eigentor', '' ).strip
   elsif notes.index( 'Elfmeter' )
     ## e.g.  Elfmeter  (Marco Hausjell)
      penalty = true
      notes = notes.sub('Elfmeter', '' ).strip
   else
     ## nothing - keep going
   end

    rec = { score:   score_str,
            team:    team,     # 1 or 2
            player:  player_name,
            minute:  goal_minute
            }
     rec[:owngoal] = true   if owngoal
     rec[:penalty] = true   if penalty
     rec[:notes]   = notes  unless notes.empty?

     rows << rec
  end ## each tr
     rows
  end
end