sportdb 1.8.3 → 1.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,222 @@
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module FixtureHelpers
5
+
6
+ def is_postponed?( line )
7
+ # check if line include postponed marker e.g. =>
8
+ line =~ /=>/
9
+ end
10
+
11
+ def calculate_year( day, month, start_at )
12
+ if month >= start_at.month
13
+ # assume same year as start_at event (e.g. 2013 for 2013/14 season)
14
+ start_at.year
15
+ else
16
+ # assume year+1 as start_at event (e.g. 2014 for 2013/14 season)
17
+ start_at.year+1
18
+ end
19
+ end
20
+
21
+
22
+ def find_date!( line, opts={} )
23
+
24
+ ## NB: lets us pass in start_at/end_at date (for event)
25
+ # for auto-complete year
26
+
27
+ # extract date from line
28
+ # and return it
29
+ # NB: side effect - removes date from line string
30
+
31
+ # e.g. 2012-09-14 20:30 => YYYY-MM-DD HH:MM
32
+ # nb: allow 2012-9-3 7:30 e.g. no leading zero required
33
+ regex_db = /\b(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{2})\b/
34
+
35
+ # e.g. 2012-09-14 w/ implied hours (set to 12:00)
36
+ # nb: allow 2012-9-3 e.g. no leading zero required
37
+ regex_db2 = /\b(\d{4})-(\d{1,2})-(\d{1,2})\b/
38
+
39
+ # e.g. 14.09. 20:30 => DD.MM. HH:MM
40
+ # nb: allow 2.3.2012 e.g. no leading zero required
41
+ # nb: allow hour as 20.30 or 3.30 instead of 03.30
42
+ regex_de = /\b(\d{1,2})\.(\d{1,2})\.\s+(\d{1,2})[:.](\d{2})\b/
43
+
44
+ # e.g. 14.09.2012 20:30 => DD.MM.YYYY HH:MM
45
+ # nb: allow 2.3.2012 e.g. no leading zero required
46
+ # nb: allow hour as 20.30
47
+ regex_de2 = /\b(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{1,2})[:.](\d{2})\b/
48
+
49
+ # e.g. 14.09.2012 => DD.MM.YYYY w/ implied hours (set to 12:00)
50
+ regex_de3 = /\b(\d{1,2})\.(\d{1,2})\.(\d{4})\b/
51
+
52
+ # e.g. 14.09. => DD.MM. w/ implied year and implied hours (set to 12:00)
53
+ regex_de4 = /\b(\d{1,2})\.(\d{1,2})\.\s+/
54
+
55
+
56
+ # todo: make more generic for reuse
57
+ month_abbrev_en = 'Jan|Feb|March|Mar|April|Apr|May|June|Jun|July|Jul|Aug|Sept|Sep|Oct|Nov|Dec'
58
+ month_abbrev_en_to_i = {
59
+ 'Jan' => 1,
60
+ 'Feb' => 2,
61
+ 'Mar' => 3, 'March' => 3,
62
+ 'Apr' => 4, 'April' => 4,
63
+ 'May' => 5,
64
+ 'Jun' => 6, 'June' => 6,
65
+ 'Jul' => 7, 'July' => 7,
66
+ 'Aug' => 8,
67
+ 'Sep' => 9, 'Sept' => 9,
68
+ 'Oct' => 10,
69
+ 'Nov' => 11,
70
+ 'Dec' => 12 }
71
+
72
+
73
+ # e.g. 12 May 2013 14:00 => D|DD.MMM.YYYY H|HH:MM
74
+ regex_en = /\b(\d{1,2})\s(#{month_abbrev_en})\s(\d{4})\s+(\d{1,2}):(\d{2})\b/
75
+
76
+
77
+ # e.g. Jun/12 14:00 w/ implied year H|HH:MM
78
+ regex_en2 = /\b(#{month_abbrev_en})\/(\d{1,2})\s+(\d{1,2}):(\d{2})\b/
79
+
80
+ # e.g. Jun/12 w/ implied year and implied hours (set to 12:00)
81
+ regex_en21 = /\b(#{month_abbrev_en})\/(\d{1,2})\b/
82
+
83
+ # todo/fix - add de and es too!!
84
+ # note: in Austria - Jänner - in Deutschland Januar allow both ??
85
+ month_abbrev_de = 'J[aä]n|Feb|Mär|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez'
86
+
87
+ month_abbrev_es = 'Enero|Ene|Feb|Marzo|Mar|Abril|Abr|Mayo|May|Junio|Jun|Julio|Jul|Agosto|Ago|Sept|Set|Sep|Oct|Nov|Dic'
88
+ month_abbrev_es_to_i = {
89
+ 'Ene' => 1, 'Enero' => 1,
90
+ 'Feb' => 2,
91
+ 'Mar' => 3, 'Marzo' => 3,
92
+ 'Abr' => 4, 'Abril' => 4,
93
+ 'May' => 5, 'Mayo' => 5,
94
+ 'Jun' => 6, 'Junio' => 6,
95
+ 'Jul' => 7, 'Julio' => 7,
96
+ 'Ago' => 8, 'Agosto' => 8,
97
+ 'Sep' => 9, 'Set' => 9, 'Sept' => 9,
98
+ 'Oct' => 10,
99
+ 'Nov' => 11,
100
+ 'Dic' => 12 }
101
+
102
+ # e.g. 12 Ene w/ implied year and implied hours (set to 12:00)
103
+ regex_es21 = /\b(\d{1,2})\s(#{month_abbrev_es})\b/
104
+
105
+ if line =~ regex_db
106
+ value = '%d-%02d-%02d %02d:%02d' % [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i]
107
+ logger.debug " date: >#{value}<"
108
+
109
+ ## todo: lets you configure year
110
+ ## and time zone (e.g. cet, eet, utc, etc.)
111
+
112
+ line.sub!( regex_db, '[DATE.DB]' )
113
+
114
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
115
+ elsif line =~ regex_db2
116
+ value = '%d-%02d-%02d 12:00' % [$1.to_i, $2.to_i, $3.to_i]
117
+ logger.debug " date: >#{value}<"
118
+
119
+ line.sub!( regex_db2, '[DATE.DB2]' )
120
+
121
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
122
+ elsif line =~ regex_de2
123
+ value = '%d-%02d-%02d %02d:%02d' % [$3.to_i, $2.to_i, $1.to_i, $4.to_i, $5.to_i]
124
+ logger.debug " date: >#{value}<"
125
+
126
+ ## todo: lets you configure year
127
+ ## and time zone (e.g. cet, eet, utc, etc.)
128
+
129
+ line.sub!( regex_de2, '[DATE.DE2]' )
130
+
131
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
132
+ elsif line =~ regex_de
133
+
134
+ year = calculate_year( $1.to_i, $2.to_i, opts[:start_at] )
135
+
136
+ value = '%d-%02d-%02d %02d:%02d' % [year, $2.to_i, $1.to_i, $3.to_i, $4.to_i]
137
+ logger.debug " date: >#{value}<"
138
+
139
+ ## todo: lets you configure year
140
+ ## and time zone (e.g. cet, eet, utc, etc.)
141
+
142
+ line.sub!( regex_de, '[DATE.DE]' )
143
+
144
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
145
+ elsif line =~ regex_de3
146
+ value = '%d-%02d-%02d 12:00' % [$3.to_i, $2.to_i, $1.to_i]
147
+ logger.debug " date: >#{value}<"
148
+
149
+ ## todo: lets you configure year
150
+ ## and time zone (e.g. cet, eet, utc, etc.)
151
+
152
+ line.sub!( regex_de3, '[DATE.DE3]' )
153
+
154
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
155
+ elsif line =~ regex_de4
156
+
157
+ year = calculate_year( $1.to_i, $2.to_i, opts[:start_at] )
158
+
159
+ value = '%d-%02d-%02d 12:00' % [year, $2.to_i, $1.to_i]
160
+ logger.debug " date: >#{value}<"
161
+
162
+ ## todo: lets you configure year
163
+ ## and time zone (e.g. cet, eet, utc, etc.)
164
+
165
+ ### NOTE: needs a trailing space
166
+ # not if regex ends w/ whitespace e.g. /s+
167
+ # make sure sub! adds a space at the end
168
+ # e.g. '[DATE.DE4]' becomes '[DATE.DE4] '
169
+
170
+ line.sub!( regex_de4, '[DATE.DE4] ' )
171
+
172
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
173
+ elsif line =~ regex_en
174
+ value = '%d-%s-%02d %02d:%02d' % [$3.to_i, $2, $1.to_i, $4.to_i, $5.to_i]
175
+ logger.debug " date: >#{value}<"
176
+
177
+ line.sub!( regex_en, '[DATE.EN]' )
178
+
179
+ return DateTime.strptime( value, '%Y-%b-%d %H:%M' ) ## %b - abbreviated month name (e.g. Jan,Feb, etc.)
180
+ elsif line =~ regex_en2
181
+ day = $2.to_i
182
+ month = month_abbrev_en_to_i[ $1 ]
183
+ year = calculate_year( day, month, opts[:start_at] )
184
+ hours = $3.to_i
185
+ minutes = $4.to_i
186
+
187
+ value = '%d-%02d-%02d %02d:%02d' % [year, month, day, hours, minutes]
188
+ logger.debug " date: >#{value}<"
189
+
190
+ line.sub!( regex_en2, '[DATE.EN2] ' )
191
+
192
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
193
+ elsif line =~ regex_en21
194
+ day = $2.to_i
195
+ month = month_abbrev_en_to_i[ $1 ]
196
+ year = calculate_year( day, month, opts[:start_at] )
197
+
198
+ value = '%d-%02d-%02d 12:00' % [year, month, day]
199
+ logger.debug " date: >#{value}<"
200
+
201
+ line.sub!( regex_en21, '[DATE.EN21] ' )
202
+
203
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
204
+ elsif line =~ regex_es21
205
+ day = $1.to_i
206
+ month = month_abbrev_es_to_i[ $2 ]
207
+ year = calculate_year( day, month, opts[:start_at] )
208
+
209
+ value = '%d-%02d-%02d 12:00' % [year, month, day]
210
+ logger.debug " date: >#{value}<"
211
+
212
+ line.sub!( regex_es21, '[DATE.ES21] ' )
213
+
214
+ return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
215
+ else
216
+ return nil
217
+ end
218
+ end
219
+
220
+ end # module FixtureHelpers
221
+ end # module SportDb
222
+
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module FixtureHelpers
5
+
6
+ def is_group?( line )
7
+ # NB: check after is_round? (round may contain group reference!)
8
+ line =~ SportDb.lang.regex_group
9
+ end
10
+
11
+ def find_group_title_and_pos!( line )
12
+ ## group pos - for now support single digit e.g 1,2,3 or letter e.g. A,B,C or HEX
13
+ ## nb: (?:) = is for non-capturing group(ing)
14
+ regex = /(?:Group|Gruppe|Grupo)\s+((?:\d{1}|[A-Z]{1,3}))\b/
15
+
16
+ match = regex.match( line )
17
+
18
+ return [nil,nil] if match.nil?
19
+
20
+ pos = case match[1]
21
+ when 'A' then 1
22
+ when 'B' then 2
23
+ when 'C' then 3
24
+ when 'D' then 4
25
+ when 'E' then 5
26
+ when 'F' then 6
27
+ when 'G' then 7
28
+ when 'H' then 8
29
+ when 'I' then 9
30
+ when 'J' then 10
31
+ when 'K' then 11
32
+ when 'L' then 12
33
+ when 'HEX' then 666 # HEX for Hexagonal - todo/check: map to something else ??
34
+ else match[1].to_i
35
+ end
36
+
37
+ title = match[0]
38
+
39
+ logger.debug " title: >#{title}<"
40
+ logger.debug " pos: >#{pos}<"
41
+
42
+ line.sub!( regex, '[GROUP|TITLE+POS]' )
43
+
44
+ return [title,pos]
45
+ end
46
+
47
+
48
+
49
+ end # module FixtureHelpers
50
+ end # module SportDb
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module FixtureHelpers
5
+
6
+
7
+ def find_ground!( line )
8
+ TextUtils.find_key_for!( 'ground', line )
9
+ end
10
+
11
+ ## todo/fix: pass in known_grounds as a parameter? why? why not?
12
+ def map_ground!( line )
13
+ TextUtils.map_titles_for!( 'ground', line, @known_grounds )
14
+ end
15
+
16
+
17
+ def find_track!( line )
18
+ TextUtils.find_key_for!( 'track', line )
19
+ end
20
+
21
+ ## todo/fix: pass in known_tracks as a parameter? why? why not?
22
+ def map_track!( line )
23
+ TextUtils.map_titles_for!( 'track', line, @known_tracks )
24
+ end
25
+
26
+ def find_person!( line )
27
+ TextUtils.find_key_for!( 'person', line )
28
+ end
29
+
30
+ def map_person!( line )
31
+ TextUtils.map_titles_for!( 'person', line, @known_persons)
32
+ end
33
+
34
+
35
+ end # module FixtureHelpers
36
+ end # module SportDb
37
+
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module FixtureHelpers
5
+
6
+
7
+ def find_record_comment!( line )
8
+ # assume everything left after the last record marker,that is, ] is a record comment
9
+
10
+ regex = /]([^\]]+?)$/ # NB: use non-greedy +?
11
+
12
+ if line =~ regex
13
+ value = $1.strip
14
+ return nil if value.blank? # skip whitespaces only
15
+
16
+ logger.debug " comment: >#{value}<"
17
+
18
+ line.sub!( value, '[REC.COMMENT] ' )
19
+ return value
20
+ else
21
+ return nil
22
+ end
23
+ end
24
+
25
+
26
+ def find_record_timeline!( line )
27
+
28
+ # +1 lap or +n laps
29
+ regex_laps = /\s+\+\d{1,2}\s(lap|laps)\b/
30
+
31
+ # 2:17:15.123
32
+ regex_time = /\b\d{1,2}:\d{2}:\d{2}\.\d{1,3}\b/
33
+
34
+ # +40.1 secs
35
+ regex_secs = /\s+\+\d{1,3}\.\d{1,3}\s(secs)\b/ # NB: before \+ - boundry (\b) will not work
36
+
37
+ # NB: $& contains the complete matched text
38
+
39
+ if line =~ regex_laps
40
+ value = $&.strip
41
+ logger.debug " timeline.laps: >#{value}<"
42
+
43
+ line.sub!( value, '[REC.TIMELINE.LAPS] ' ) # NB: add trailing space
44
+ return value
45
+ elsif line =~ regex_time
46
+ value = $&.strip
47
+ logger.debug " timeline.time: >#{value}<"
48
+
49
+ line.sub!( value, '[REC.TIMELINE.TIME] ' ) # NB: add trailing space
50
+ return value
51
+ elsif line =~ regex_secs
52
+ value = $&.strip
53
+ logger.debug " timeline.secs: >#{value}<"
54
+
55
+ line.sub!( value, '[REC.TIMELINE.SECS] ' ) # NB: add trailing space
56
+ return value
57
+ else
58
+ return nil
59
+ end
60
+ end
61
+
62
+ def find_record_laps!( line )
63
+ # e.g. first free-standing number w/ one or two digits e.g. 7 or 28 etc.
64
+ regex = /\b(\d{1,2})\b/
65
+ if line =~ regex
66
+ logger.debug " laps: >#{$1}<"
67
+
68
+ line.sub!( regex, '[REC.LAPS] ' ) # NB: add trailing space
69
+ return $1.to_i
70
+ else
71
+ return nil
72
+ end
73
+ end
74
+
75
+ def find_record_leading_state!( line )
76
+ # e.g. 1|2|3|etc or Ret - must start line
77
+ regex = /^[ \t]*(\d{1,3}|Ret)[ \t]+/
78
+ if line =~ regex
79
+ value = $1.dup
80
+ logger.debug " state: >#{value}<"
81
+
82
+ line.sub!( regex, '[REC.STATE] ' ) # NB: add trailing space
83
+ return value
84
+ else
85
+ return nil
86
+ end
87
+ end
88
+
89
+
90
+ end # module FixtureHelpers
91
+ end # module SportDb
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module FixtureHelpers
5
+
6
+ def is_round?( line )
7
+ line =~ SportDb.lang.regex_round
8
+ end
9
+
10
+ def is_knockout_round?( line )
11
+
12
+ ## todo: check for adding ignore case for regex (e.g. 1st leg/1st Leg)
13
+
14
+ if line =~ SportDb.lang.regex_leg1
15
+ logger.debug " two leg knockout; skip knockout flag on first leg"
16
+ false
17
+ elsif line =~ SportDb.lang.regex_knockout_round
18
+ logger.debug " setting knockout flag to true"
19
+ true
20
+ elsif line =~ /K\.O\.|K\.o\.|Knockout/
21
+ ## NB: add two language independent markers, that is, K.O. and Knockout
22
+ logger.debug " setting knockout flag to true (lang independent marker)"
23
+ true
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ def find_round_title2!( line )
30
+ # assume everything after // is title2 - strip off leading n trailing whitespaces
31
+ regex = /\/{2,}\s*(.+)\s*$/
32
+ if line =~ regex
33
+ logger.debug " title2: >#{$1}<"
34
+
35
+ line.sub!( regex, '[ROUND|TITLE2]' )
36
+ return $1
37
+ else
38
+ return nil # no round title2 found (title2 is optional)
39
+ end
40
+ end
41
+
42
+
43
+ def find_round_title!( line )
44
+ # assume everything left is the round title
45
+ # extract all other items first (round title2, round pos, group title n pos, etc.)
46
+
47
+ buf = line.dup
48
+ logger.debug " find_round_title! line-before: >>#{buf}<<"
49
+
50
+ buf.gsub!( /\[.+?\]/, '' ) # e.g. remove [ROUND|POS], [ROUND|TITLE2], [GROUP|TITLE+POS] etc.
51
+ buf.sub!( /\s+[\/\-]{1,}\s+$/, '' ) # remove optional trailing / or / chars (left over from group)
52
+ buf.strip! # remove leading and trailing whitespace
53
+
54
+ logger.debug " find_round_title! line-after: >>#{buf}<<"
55
+
56
+ ### bingo - assume what's left is the round title
57
+
58
+ logger.debug " title: >>#{buf}<<"
59
+ line.sub!( buf, '[ROUND|TITLE]' )
60
+
61
+ buf
62
+ end
63
+
64
+
65
+ def find_round_pos!( line )
66
+ ## fix/todo:
67
+ ## if no round found assume last_pos+1 ??? why? why not?
68
+
69
+ # extract optional round pos from line
70
+ # e.g. (1) - must start line
71
+ regex_pos = /^[ \t]*\((\d{1,3})\)[ \t]+/
72
+
73
+ ## find free standing number
74
+ regex_num = /\b(\d{1,3})\b/
75
+
76
+ if line =~ regex_pos
77
+ logger.debug " pos: >#{$1}<"
78
+
79
+ line.sub!( regex_pos, '[ROUND|POS] ' ) ## NB: add back trailing space that got swallowed w/ regex -> [ \t]+
80
+ return $1.to_i
81
+ elsif line =~ regex_num
82
+ ## assume number in title is pos (e.g. Jornada 3, 3 Runde etc.)
83
+ ## NB: do NOT remove pos from string (will get removed by round title)
84
+ logger.debug " pos: >#{$1}<"
85
+ return $1.to_i
86
+ else
87
+ ## fix: add logger.warn no round pos found in line
88
+ return nil
89
+ end
90
+ end # method find_round_pos!
91
+
92
+
93
+ end # module FixtureHelpers
94
+ end # module SportDb