Class: Worldfootball::Page::Schedule
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
GENERATED_RE
Class Method Summary
collapse
Instance Method Summary
collapse
#assert, #doc, from_file, #generated, #generated_in_days_ago, #initialize, #keywords, #squish, #title, #url
Class Method Details
.from_cache(slug) ⇒ Object
8
9
10
11
12
|
# File 'lib/webget-football/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
#matches ⇒ Object
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
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 16
def matches
@matches ||= begin
table = doc.css( 'div.data > table.standard_tabelle' ).first
trs = table.css( 'tr' )
i = 0
last_date_str = nil
last_round = nil
rows = []
trs.each do |tr|
i += 1
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
Playoffs # see EL Quali
/x
puts
print '[%03d] ' % i
print "round >#{tr.text.strip}<"
print "\n"
last_round = tr.text.strip
else tds = tr.css( 'td' )
date_str = squish( tds[0].text )
time_str = squish( tds[1].text )
team1_anchor = tds[2].css( 'a' )[0]
if team1_anchor 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
vs_str = squish( tds[3].text ) assert( vs_str == '-', "- for vs. expected; got #{vs_str}")
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
score_anchor = tds[5].css( 'a' )[0]
if score_anchor 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
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 = '-:-' end
date_str = last_date_str if date_str.empty?
print '[%03d] ' % i
print "%-10s | " % date_str
print "%-5s | " % time_str
print "%-22s | " % team1_str
print "%-22s | " % team2_str
print "%-10s | " % score_str
print (score_ref ? score_ref : 'n/a')
print "\n"
score_str = score_str.gsub( ':', '-' )
date = Date.strptime( date_str, '%d.%m.%Y' )
rows << { round: last_round,
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
rows
end
end
|
#norm_score_ref(str) ⇒ Object
221
222
223
224
225
226
227
228
229
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 221
def norm_score_ref( str )
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
249
250
251
252
253
254
255
256
257
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 249
def norm_season_ref( str )
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
235
236
237
238
239
240
241
242
243
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 235
def norm_team_ref( str )
if m=REF_TEAM_RE.match( str )
m[1]
else
puts "!! ERROR: unexpected team href format >#{str}<"
exit 1
end
end
|
#rounds ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 183
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
|
#seasons ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 197
def seasons
@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
|
#teams ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/webget-football/worldfootball/page_schedule.rb', line 162
def teams
@teams ||= begin
h = {}
matches.each do |match|
[{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
end
end
h.values
end
end
|