sportdb-writers 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +6 -4
- data/Manifest.txt +4 -4
- data/README.md +26 -28
- data/Rakefile +30 -30
- data/bin/fbgen +99 -0
- data/bin/fbtxt +52 -0
- data/lib/sportdb/leagues/leagues_at.rb +35 -39
- data/lib/sportdb/leagues/leagues_de.rb +21 -29
- data/lib/sportdb/leagues/leagues_eng.rb +58 -70
- data/lib/sportdb/leagues/leagues_es.rb +15 -19
- data/lib/sportdb/leagues/leagues_europe.rb +185 -213
- data/lib/sportdb/leagues/leagues_it.rb +16 -20
- data/lib/sportdb/leagues/leagues_mx.rb +23 -25
- data/lib/sportdb/leagues/leagues_south_america.rb +17 -21
- data/lib/sportdb/leagues/leagues_world.rb +14 -16
- data/lib/sportdb/writers/github.rb +195 -0
- data/lib/sportdb/writers/goals.rb +57 -0
- data/lib/sportdb/writers/txt_writer.rb +225 -407
- data/lib/sportdb/writers/version.rb +24 -25
- data/lib/sportdb/writers/write.rb +269 -367
- data/lib/sportdb/writers.rb +89 -31
- metadata +34 -19
- data/lib/sportdb/writers/config.rb +0 -18
- data/test/helper.rb +0 -19
- data/test/test_txt_writer.rb +0 -124
- data/test/test_version.rb +0 -16
@@ -1,407 +1,225 @@
|
|
1
|
-
module SportDb
|
2
|
-
class TxtMatchWriter
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
buf << "#{round} #{match.round}"
|
227
|
-
end
|
228
|
-
else ## use as is from match
|
229
|
-
## note: for now assume english names
|
230
|
-
if round_translations
|
231
|
-
buf << "#{round_translations[match.round] || match.round}"
|
232
|
-
else
|
233
|
-
buf << "#{match.round}"
|
234
|
-
end
|
235
|
-
end
|
236
|
-
buf << "\n"
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
|
241
|
-
date = if match.date.is_a?( String )
|
242
|
-
Date.strptime( match.date, '%Y-%m-%d' )
|
243
|
-
else ## assume it's already a date (object)
|
244
|
-
match.date
|
245
|
-
end
|
246
|
-
|
247
|
-
time = if match.time.is_a?( String )
|
248
|
-
Time.strptime( match.time, '%H:%M')
|
249
|
-
else ## assume it's already a time (object) or nil
|
250
|
-
match.time
|
251
|
-
end
|
252
|
-
|
253
|
-
|
254
|
-
date_yyyymmdd = date.strftime( '%Y-%m-%d' )
|
255
|
-
|
256
|
-
## note: time is OPTIONAL for now
|
257
|
-
## note: use 17.00 and NOT 17:00 for now
|
258
|
-
time_hhmm = time ? time.strftime( '%H.%M' ) : nil
|
259
|
-
|
260
|
-
|
261
|
-
if rounds
|
262
|
-
if match.round != last_round || date_yyyymmdd != last_date
|
263
|
-
buf << "[#{format_date.call( date )}]\n"
|
264
|
-
last_time = nil ## note: reset time for new date
|
265
|
-
end
|
266
|
-
else
|
267
|
-
if date_yyyymmdd != last_date
|
268
|
-
buf << "\n" ## note: add an extra leading blank line (if no round headings printed)
|
269
|
-
buf << "[#{format_date.call( date )}]\n"
|
270
|
-
last_time = nil
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
|
275
|
-
## allow strings and structs for team names
|
276
|
-
team1 = match.team1.is_a?( String ) ? match.team1 : match.team1.name
|
277
|
-
team2 = match.team2.is_a?( String ) ? match.team2 : match.team2.name
|
278
|
-
|
279
|
-
|
280
|
-
line = String.new('')
|
281
|
-
line << ' '
|
282
|
-
|
283
|
-
if time
|
284
|
-
if last_time.nil? || last_time != time_hhmm
|
285
|
-
line << "%5s" % time_hhmm
|
286
|
-
else
|
287
|
-
line << ' '
|
288
|
-
end
|
289
|
-
line << ' '
|
290
|
-
else
|
291
|
-
## do nothing for now
|
292
|
-
end
|
293
|
-
|
294
|
-
|
295
|
-
line << "%-23s" % team1 ## note: use %-s for left-align
|
296
|
-
|
297
|
-
line << " #{format_score.call( match )} " ## note: separate by at least two spaces for now
|
298
|
-
|
299
|
-
line << "%-23s" % team2
|
300
|
-
|
301
|
-
|
302
|
-
if match.status
|
303
|
-
line << ' '
|
304
|
-
case match.status
|
305
|
-
when Status::CANCELLED
|
306
|
-
line << '[cancelled]'
|
307
|
-
when Status::AWARDED
|
308
|
-
line << '[awarded]'
|
309
|
-
when Status::ABANDONED
|
310
|
-
line << '[abandoned]'
|
311
|
-
when Status::REPLAY
|
312
|
-
line << '[replay]'
|
313
|
-
when Status::POSTPONED
|
314
|
-
## note: add NOTHING for postponed for now
|
315
|
-
else
|
316
|
-
puts "!! WARN - unknown match status >#{match.status}<:"
|
317
|
-
pp match
|
318
|
-
line << "[#{match.status.downcase}]" ## print "literal" downcased for now
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
## add match line
|
323
|
-
buf << line.rstrip ## remove possible right trailing spaces before adding
|
324
|
-
buf << "\n"
|
325
|
-
|
326
|
-
if match.goals
|
327
|
-
buf << ' ' # 4 space indent
|
328
|
-
buf << ' ' if time # 7 (5+2) space indent (for hour e.g. 17.30)
|
329
|
-
buf << "[#{build_goals(match.goals, lang: lang )}]"
|
330
|
-
buf << "\n"
|
331
|
-
end
|
332
|
-
|
333
|
-
|
334
|
-
last_round = match.round
|
335
|
-
last_date = date_yyyymmdd
|
336
|
-
last_time = time_hhmm
|
337
|
-
end
|
338
|
-
buf
|
339
|
-
end
|
340
|
-
|
341
|
-
|
342
|
-
def self.write( path, matches, name:, lang: 'en', rounds: true)
|
343
|
-
|
344
|
-
buf = build( matches, lang: lang, rounds: rounds )
|
345
|
-
|
346
|
-
## for convenience - make sure parent folders/directories exist
|
347
|
-
FileUtils.mkdir_p( File.dirname( path) ) unless Dir.exists?( File.dirname( path ))
|
348
|
-
|
349
|
-
File.open( path, 'w:utf-8' ) do |f|
|
350
|
-
f.write( "= #{name}\n" )
|
351
|
-
f.write( buf )
|
352
|
-
end
|
353
|
-
end # method self.write
|
354
|
-
|
355
|
-
|
356
|
-
def self.build_goals( goals, lang: )
|
357
|
-
## todo/fix: for now assumes always minutes (without offset) - add offset support
|
358
|
-
|
359
|
-
## note: "fold" multiple goals by players
|
360
|
-
team1_goals = {}
|
361
|
-
team2_goals = {}
|
362
|
-
goals.each do |goal|
|
363
|
-
team_goals = goal.team == 1 ? team1_goals : team2_goals
|
364
|
-
player_goals = team_goals[ goal.player ] ||= []
|
365
|
-
player_goals << goal
|
366
|
-
end
|
367
|
-
|
368
|
-
buf = String.new('')
|
369
|
-
if team1_goals.size > 0
|
370
|
-
buf << build_goals_for_team( team1_goals, lang: lang )
|
371
|
-
end
|
372
|
-
|
373
|
-
## note: only add a separator (;) if BOTH teams have goal scores
|
374
|
-
if team1_goals.size > 0 && team2_goals.size > 0
|
375
|
-
buf << '; '
|
376
|
-
end
|
377
|
-
|
378
|
-
if team2_goals.size > 0
|
379
|
-
buf << build_goals_for_team( team2_goals, lang: lang )
|
380
|
-
end
|
381
|
-
buf
|
382
|
-
end
|
383
|
-
|
384
|
-
|
385
|
-
def self.build_goals_for_team( team_goals, lang: )
|
386
|
-
buf = String.new('')
|
387
|
-
team_goals.each_with_index do |(player_name, goals),i|
|
388
|
-
buf << ' ' if i > 0
|
389
|
-
buf << "#{player_name} "
|
390
|
-
buf << goals.map do |goal|
|
391
|
-
str = "#{goal.minute}'"
|
392
|
-
if ['de', 'de_AT', 'de_DE', 'de_CH'].include?( lang )
|
393
|
-
str << " (Eigentor)" if goal.owngoal?
|
394
|
-
str << " (Elf.)" if goal.penalty?
|
395
|
-
else ## fallback to english (by default)
|
396
|
-
str << " (o.g.)" if goal.owngoal?
|
397
|
-
str << " (pen.)" if goal.penalty?
|
398
|
-
end
|
399
|
-
str
|
400
|
-
end.join( ', ' )
|
401
|
-
end
|
402
|
-
buf
|
403
|
-
end
|
404
|
-
|
405
|
-
|
406
|
-
end # class TxtMatchWriter
|
407
|
-
end # module SportDb
|
1
|
+
module SportDb
|
2
|
+
class TxtMatchWriter
|
3
|
+
|
4
|
+
|
5
|
+
## translate from lang x (german, etc) to english
|
6
|
+
ROUND_TRANSLATIONS = {
|
7
|
+
# de/german
|
8
|
+
'1. Runde' => 'Round 1',
|
9
|
+
'2. Runde' => 'Round 2',
|
10
|
+
'Achtelfinale' => 'Round of 16',
|
11
|
+
'Viertelfinale' => 'Quarterfinals',
|
12
|
+
'Halbfinale' => 'Semifinals',
|
13
|
+
'Finale' => 'Final',
|
14
|
+
}
|
15
|
+
|
16
|
+
|
17
|
+
## note: build returns buf - an (in-memory) string buf(fer)
|
18
|
+
def self.build( matches, rounds: true )
|
19
|
+
## note: make sure rounds is a bool, that is, true or false (do NOT pass in strings etc.)
|
20
|
+
raise ArgumentError, "rounds flag - bool expected; got: #{rounds.inspect}" unless rounds.is_a?( TrueClass ) || rounds.is_a?( FalseClass )
|
21
|
+
|
22
|
+
## note: for now always english
|
23
|
+
round = 'Matchday'
|
24
|
+
format_date = ->(date) { date.strftime( '%a %b/%-d' ) }
|
25
|
+
format_score = ->(match) { match.score.to_s( lang: 'en' ) }
|
26
|
+
round_translations = ROUND_TRANSLATIONS
|
27
|
+
|
28
|
+
|
29
|
+
buf = String.new
|
30
|
+
|
31
|
+
last_round = nil
|
32
|
+
last_date = nil
|
33
|
+
last_time = nil
|
34
|
+
|
35
|
+
|
36
|
+
matches.each do |match|
|
37
|
+
|
38
|
+
## note: make rounds optional (set rounds flag to false to turn off)
|
39
|
+
if rounds
|
40
|
+
if match.round != last_round
|
41
|
+
buf << "\n\n"
|
42
|
+
if match.round.is_a?( Integer ) ||
|
43
|
+
match.round =~ /^[0-9]+$/ ## all numbers/digits
|
44
|
+
## default "class format
|
45
|
+
## e.g. Runde 1, Spieltag 1, Matchday 1, Week 1
|
46
|
+
buf << "#{round} #{match.round}"
|
47
|
+
else ## use as is from match
|
48
|
+
## note: for now assume english names
|
49
|
+
if match.round.nil?
|
50
|
+
## warn
|
51
|
+
puts "!! ERROR - match with round nil?"
|
52
|
+
pp match
|
53
|
+
exit 1
|
54
|
+
end
|
55
|
+
|
56
|
+
buf << (round_translations[match.round] || match.round)
|
57
|
+
end
|
58
|
+
buf << "\n"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
date = if match.date.is_a?( String )
|
64
|
+
Date.strptime( match.date, '%Y-%m-%d' )
|
65
|
+
else ## assume it's already a date (object)
|
66
|
+
match.date
|
67
|
+
end
|
68
|
+
|
69
|
+
time = if match.time.is_a?( String )
|
70
|
+
Time.strptime( match.time, '%H:%M')
|
71
|
+
else ## assume it's already a time (object) or nil
|
72
|
+
match.time
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
date_yyyymmdd = date.strftime( '%Y-%m-%d' )
|
77
|
+
|
78
|
+
## note: time is OPTIONAL for now
|
79
|
+
## note: use 17.00 and NOT 17:00 for now
|
80
|
+
time_hhmm = time ? time.strftime( '%H.%M' ) : nil
|
81
|
+
|
82
|
+
|
83
|
+
if rounds
|
84
|
+
if match.round != last_round || date_yyyymmdd != last_date
|
85
|
+
buf << "[#{format_date.call( date )}]\n"
|
86
|
+
last_time = nil ## note: reset time for new date
|
87
|
+
end
|
88
|
+
else
|
89
|
+
if date_yyyymmdd != last_date
|
90
|
+
buf << "\n" ## note: add an extra leading blank line (if no round headings printed)
|
91
|
+
buf << "[#{format_date.call( date )}]\n"
|
92
|
+
last_time = nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
## allow strings and structs for team names
|
98
|
+
team1 = match.team1.is_a?( String ) ? match.team1 : match.team1.name
|
99
|
+
team2 = match.team2.is_a?( String ) ? match.team2 : match.team2.name
|
100
|
+
|
101
|
+
|
102
|
+
line = String.new
|
103
|
+
line << ' '
|
104
|
+
|
105
|
+
if time
|
106
|
+
if last_time.nil? || last_time != time_hhmm
|
107
|
+
line << "%5s" % time_hhmm
|
108
|
+
else
|
109
|
+
line << ' '
|
110
|
+
end
|
111
|
+
line << ' '
|
112
|
+
else
|
113
|
+
## do nothing for now
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
line << "%-23s" % team1 ## note: use %-s for left-align
|
118
|
+
|
119
|
+
line << " #{format_score.call( match )} " ## note: separate by at least two spaces for now
|
120
|
+
|
121
|
+
line << "%-23s" % team2
|
122
|
+
|
123
|
+
|
124
|
+
if match.status
|
125
|
+
line << ' '
|
126
|
+
case match.status
|
127
|
+
when Status::CANCELLED
|
128
|
+
line << '[cancelled]'
|
129
|
+
when Status::AWARDED
|
130
|
+
line << '[awarded]'
|
131
|
+
when Status::ABANDONED
|
132
|
+
line << '[abandoned]'
|
133
|
+
when Status::REPLAY
|
134
|
+
line << '[replay]'
|
135
|
+
when Status::POSTPONED
|
136
|
+
## note: add NOTHING for postponed for now
|
137
|
+
else
|
138
|
+
puts "!! WARN - unknown match status >#{match.status}<:"
|
139
|
+
pp match
|
140
|
+
line << "[#{match.status.downcase}]" ## print "literal" downcased for now
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
## add match line
|
145
|
+
buf << line.rstrip ## remove possible right trailing spaces before adding
|
146
|
+
buf << "\n"
|
147
|
+
|
148
|
+
if match.goals
|
149
|
+
buf << ' ' # 4 space indent
|
150
|
+
buf << ' ' if time # 7 (5+2) space indent (for hour e.g. 17.30)
|
151
|
+
buf << "[#{build_goals(match.goals)}]"
|
152
|
+
buf << "\n"
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
last_round = match.round
|
157
|
+
last_date = date_yyyymmdd
|
158
|
+
last_time = time_hhmm
|
159
|
+
end
|
160
|
+
buf
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
def self.write( path, matches, name:, rounds: true)
|
165
|
+
|
166
|
+
buf = build( matches, rounds: rounds )
|
167
|
+
|
168
|
+
## for convenience - make sure parent folders/directories exist
|
169
|
+
FileUtils.mkdir_p( File.dirname( path) ) unless Dir.exist?( File.dirname( path ))
|
170
|
+
|
171
|
+
puts "==> writing to >#{path}<..."
|
172
|
+
File.open( path, 'w:utf-8' ) do |f|
|
173
|
+
f.write( "= #{name}\n" )
|
174
|
+
f.write( buf )
|
175
|
+
end
|
176
|
+
end # method self.write
|
177
|
+
|
178
|
+
|
179
|
+
def self.build_goals( goals )
|
180
|
+
## todo/fix: for now assumes always minutes (without offset) - add offset support
|
181
|
+
|
182
|
+
## note: "fold" multiple goals by players
|
183
|
+
team1_goals = {}
|
184
|
+
team2_goals = {}
|
185
|
+
goals.each do |goal|
|
186
|
+
team_goals = goal.team == 1 ? team1_goals : team2_goals
|
187
|
+
player_goals = team_goals[ goal.player ] ||= []
|
188
|
+
player_goals << goal
|
189
|
+
end
|
190
|
+
|
191
|
+
buf = String.new
|
192
|
+
if team1_goals.size > 0
|
193
|
+
buf << build_goals_for_team( team1_goals )
|
194
|
+
end
|
195
|
+
|
196
|
+
## note: only add a separator (;) if BOTH teams have goal scores
|
197
|
+
if team1_goals.size > 0 && team2_goals.size > 0
|
198
|
+
buf << '; '
|
199
|
+
end
|
200
|
+
|
201
|
+
if team2_goals.size > 0
|
202
|
+
buf << build_goals_for_team( team2_goals )
|
203
|
+
end
|
204
|
+
buf
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
def self.build_goals_for_team( team_goals )
|
209
|
+
buf = String.new
|
210
|
+
team_goals.each_with_index do |(player_name, goals),i|
|
211
|
+
buf << ' ' if i > 0
|
212
|
+
buf << "#{player_name} "
|
213
|
+
buf << goals.map do |goal|
|
214
|
+
str = "#{goal.minute}'"
|
215
|
+
str << " (o.g.)" if goal.owngoal?
|
216
|
+
str << " (pen.)" if goal.penalty?
|
217
|
+
str
|
218
|
+
end.join( ', ' )
|
219
|
+
end
|
220
|
+
buf
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
end # class TxtMatchWriter
|
225
|
+
end # module SportDb
|