sportdb-readers 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 571eb048edc2298d0c059f6af8bd572b9926631c
4
- data.tar.gz: 7dc563726f8c82a2fa03327e7b51ba99bff29f20
3
+ metadata.gz: fec930bd9a27bc8f0e0b12ac9dd6a1e28e63c85d
4
+ data.tar.gz: 55eaf685681e934e2b694f6ef4597847dbaed8cf
5
5
  SHA512:
6
- metadata.gz: d1319dcf78797f4a04f766c190cfbea4f2a6777a2925638f63092ab21a073304659e449c7d0443f01addfbf496df61e4cd21112d3ca14b333abbc9a1aba23557
7
- data.tar.gz: 8817b8cdf55bb7df0afafd3a8abadc62ac01e05fd82b18e3e6a6cd0bb9228da6662ec49e114b74225a3f41f30c65b23d361c8cdde8ed3c7b7e33faad63745651
6
+ metadata.gz: 5ca012b994817963992c4b965cf89de4c39ba70b4865ec714a7ee486580aafd42137c047cbfe831e17b9a6db9e3cf940c8cf787746a7ccef88afcc8945739a30
7
+ data.tar.gz: 9887277231dd7de20ddeaa47ccc87c6df89767032658fc72bee6ed2f317d9fd3fc79ca5cf552bf9a7afcd0a08582566e9b03266cad228c380b346285a21f9c49
data/Manifest.txt CHANGED
@@ -11,4 +11,5 @@ lib/sportdb/readers/sync.rb
11
11
  lib/sportdb/readers/version.rb
12
12
  test/helper.rb
13
13
  test/test_match_parser.rb
14
+ test/test_read.rb
14
15
  test/test_reader.rb
@@ -32,7 +32,7 @@ module SportDb
32
32
  datafiles.each { |datafile| MatchReaderV2.read( datafile ) }
33
33
  else
34
34
  ## check if datafile matches configuration naming (e.g. .conf.txt)
35
- if Datafile::CONF_REGEX.match( path ) ## fix: use Datafile.match_conf
35
+ if Datafile.match_conf( path )
36
36
  EventReaderV2.read( path )
37
37
  else ## assume "regular" match datafile
38
38
  MatchReaderV2.read( path )
@@ -59,14 +59,25 @@ class EventReaderV2 ## todo/check: rename to EventsReaderV2 (use plural?) why
59
59
  league = Sync::League.find_or_create( rec[:league] )
60
60
  season = Sync::Season.find_or_create( rec[:season] )
61
61
 
62
+
62
63
  event = Sync::Event.find_or_create( league: league, season: season )
64
+ if rec[:stage]
65
+ stage = Sync::Stage.find_or_create( rec[:stage], event: event )
66
+ else
67
+ stage = nil
68
+ end
69
+
63
70
 
64
71
  rec[:clubs].each do |club_rec|
65
72
  club = Sync::Club.find_or_create( club_rec )
66
73
  ## add teams to event
67
74
  ## todo/fix: check if team is alreay included?
68
75
  ## or clear/destroy_all first!!!
69
- event.teams << club
76
+ if stage
77
+ stage.teams << club
78
+ else
79
+ event.teams << club
80
+ end
70
81
  end
71
82
  end
72
83
 
@@ -18,17 +18,32 @@ class MatchReaderV2 ## todo/check: rename to MatchReaderV2 (use plural?) why?
18
18
  season = Sync::Season.find!( rec[:season] )
19
19
 
20
20
  event = Sync::Event.find!( league: league, season: season )
21
+ if rec[:stage]
22
+ stage = Sync::Stage.find!( rec[:stage], event: event )
23
+ teams = stage.teams
24
+ else
25
+ teams = event.teams
26
+ end
27
+
28
+ ## hack for now: switch lang
29
+ if ['at', 'de'].include?( league.country.key )
30
+ SportDb.lang.lang = 'de'
31
+ else
32
+ SportDb.lang.lang = 'en'
33
+ end
21
34
 
22
35
  ## todo/fix: set lang for now depending on league country!!!
23
36
  parser = MatchParserSimpleV2.new( rec[:lines],
24
- event.teams,
25
- event.start_at )
37
+ teams, ## note: use event OR stage teams (if stage is present)
38
+ event.start_at ) ## note: keep season start_at date for now (no need for more specific stage date need for now)
26
39
  round_recs, match_recs = parser.parse
40
+ pp round_recs
27
41
 
28
42
  round_recs.each do |round_rec|
29
43
  round = Sync::Round.find_or_create( round_rec, event: event ) ## check: use/rename to EventRound why? why not?
30
44
  end
31
45
  match_recs.each do |match_rec|
46
+ ## todo/fix: pass along stage (if present): stage - optional!!!!
32
47
  match = Sync::Match.create_or_update( match_rec, event: event )
33
48
  end
34
49
  end
@@ -25,13 +25,15 @@ class LeagueOutlineReader
25
25
  recs=[]
26
26
  OutlineReader.parse( txt ).each do |node|
27
27
  if node[0] == :h1
28
- ## check for league and season
28
+ ## check for league (and stage) and season
29
29
  heading = node[1]
30
- if m=heading.match( LEAGUE_SEASON_HEADING_REGEX )
30
+ values = split_league( heading )
31
+ if m=values[0].match( LEAGUE_SEASON_HEADING_REGEX )
31
32
  puts "league >#{m[:league]}<, season >#{m[:season]}<"
32
33
 
33
34
  recs << { league: m[:league],
34
35
  season: m[:season],
36
+ stage: values[1], ## note: defaults to nil if not present
35
37
  lines: []
36
38
  }
37
39
  else
@@ -58,12 +60,39 @@ class LeagueOutlineReader
58
60
  recs.each do |rec|
59
61
  league = find_league( rec[:league] )
60
62
  rec[:league] = league
63
+
64
+ check_stage( rec[:stage] ) if rec[:stage] ## note: only check for now (no remapping etc.)
61
65
  end
62
66
 
63
67
  recs
64
68
  end # method parse
65
69
 
66
70
 
71
+ def self.split_league( str ) ## todo/check: rename to parse_league(s) - why? why not?
72
+ ## split into league / stage / ... e.g.
73
+ ## => Österr. Bundesliga 2018/19, Regular Season
74
+ ## => Österr. Bundesliga 2018/19, Championship Round
75
+ ## etc.
76
+ values = str.split( /[,<>‹›]/ ) ## note: allow , > < or › ‹ for now
77
+ values = values.map { |value| value.strip } ## remove all whitespaces
78
+ values
79
+ end
80
+
81
+ def self.check_stage( name )
82
+ known_stages = ['regular season',
83
+ 'championship round',
84
+ 'relegation round',
85
+ ]
86
+
87
+ if known_stages.include?( name.downcase )
88
+ ## everything ok
89
+ else
90
+ puts "** !!! ERROR !!! no (league) stage match found for >#{name}<, add to (builtin) stages table; sorry"
91
+ exit 1
92
+ end
93
+ end
94
+
95
+
67
96
  def self.find_league( name )
68
97
  league = nil
69
98
  m = config.leagues.match( name )
@@ -54,6 +54,7 @@ module Sync
54
54
  end
55
55
  end # class League
56
56
 
57
+
57
58
  class Season
58
59
  def self.normalize_key( key ) ## helper for season key (rename to norm_key ???)
59
60
  ## note: "normalize" season key
@@ -177,6 +178,38 @@ module Sync
177
178
  end
178
179
  end # class Round
179
180
 
181
+
182
+ class Stage
183
+ def self.find( name, event: )
184
+ SportDb::Model::Stage.find_by( title: name, event_id: event.id )
185
+ end
186
+ def self.find!( name, event: )
187
+ rec = find( name, event: event )
188
+ if rec.nil?
189
+ puts "** !!!ERROR!!! db sync - no stage match found for:"
190
+ pp name
191
+ pp event
192
+ exit 1
193
+ end
194
+ rec
195
+ end
196
+
197
+ def self.find_or_create( name, event: )
198
+ rec = find( name, event: event )
199
+ if rec.nil?
200
+ ## use title and not name - why? why not?
201
+ ## quick fix: change name to title
202
+ attribs = { event_id: event.id,
203
+ title: name,
204
+ }
205
+ rec = SportDb::Model::Stage.create!( attribs )
206
+ end
207
+ rec
208
+ end
209
+ end # class Stage
210
+
211
+
212
+
180
213
  class Match ## todo/check: add alias for Game class - why? why not?
181
214
  def self.create_or_update( match, event: )
182
215
  ## note: MUST find round, thus, use bang (!)
@@ -6,7 +6,7 @@ module Readers
6
6
 
7
7
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
8
8
  MINOR = 1
9
- PATCH = 1
9
+ PATCH = 2
10
10
  VERSION = [MAJOR,MINOR,PATCH].join('.')
11
11
 
12
12
  def self.version
@@ -16,9 +16,15 @@ class TestMatchParser < MiniTest::Test
16
16
  def Club.read( txt )
17
17
  recs = []
18
18
  txt.each_line do |line|
19
- values = line.split( ',' )
20
- values = values.map {|value| value.strip }
21
- recs << Club.new( values[0], values[1], values.size > 2 ? values[2..-1].join('|') : nil )
19
+ values = line.split( '|' )
20
+ values = values.map { |value| value.strip }
21
+
22
+ title = values[0]
23
+ ## note: quick hack - auto-generate key, that is, remove all non-ascii chars and downcase
24
+ key = title.downcase.gsub( /[^a-z]/, '' )
25
+ synonyms = values.size > 1 ? values[1..-1].join( '|' ) : nil
26
+
27
+ recs << Club.new( key, title, synonyms )
22
28
  end
23
29
  recs
24
30
  end
@@ -61,26 +67,26 @@ Matchday 2
61
67
  TXT
62
68
 
63
69
  clubs_txt = <<TXT
64
- arsenalfc, Arsenal FC, Arsenal, FC Arsenal
65
- leicestercityfc, Leicester City FC, Leicester, Leicester City
66
- watfordfc, Watford FC, Watford, FC Watford
67
- liverpoolfc, Liverpool FC, Liverpool, FC Liverpool
68
- chelseafc, Chelsea FC, Chelsea, FC Chelsea
69
- burnleyfc, Burnley FC, Burnley, FC Burnley
70
- crystalpalacefc, Crystal Palace FC, Crystal Palace, C Palace, Palace, Crystal P
71
- huddersfieldtownafc, Huddersfield Town AFC, Huddersfield, Huddersfield Town
72
- evertonfc, Everton FC, Everton, FC Everton
73
- stokecityfc, Stoke City FC, Stoke, Stoke City
74
- southamptonfc, Southampton FC, Southampton, FC Southampton
75
- swanseacityfc, Swansea City FC, Swansea, Swansea City, Swansea City AFC
76
- westbromwichalbionfc, West Bromwich Albion FC, West Brom, West Bromwich Albion, West Bromwich, Albion
77
- afcbournemouth, AFC Bournemouth, Bournemouth, A.F.C. Bournemouth, Bournemouth FC
78
- brightonhovealbionfc, Brighton & Hove Albion FC, Brighton, Brighton & Hove, Brighton & Hove Albion
79
- manchestercityfc, Manchester City FC, Man City, Manchester City, Man. City, Manchester C
80
- newcastleunitedfc, Newcastle United FC, Newcastle, Newcastle Utd, Newcastle United
81
- tottenhamhotspurfc, Tottenham Hotspur FC, Tottenham, Tottenham Hotspur, Spurs
82
- manchesterunitedfc, Manchester United FC, Man Utd, Man. United, Manchester U., Manchester Utd, Manchester United
83
- westhamunitedfc, West Ham United FC, West Ham, West Ham United
70
+ Arsenal FC | Arsenal | FC Arsenal
71
+ Leicester City FC | Leicester | Leicester City
72
+ Watford FC | Watford | FC Watford
73
+ Liverpool FC | Liverpool | FC Liverpool
74
+ Chelsea FC | Chelsea | FC Chelsea
75
+ Burnley FC | Burnley | FC Burnley
76
+ Crystal Palace FC | Crystal Palace | C Palace | Palace | Crystal P
77
+ Huddersfield Town AFC | Huddersfield | Huddersfield Town
78
+ Everton FC | Everton | FC Everton
79
+ Stoke City FC | Stoke | Stoke City
80
+ Southampton FC | Southampton | FC Southampton
81
+ Swansea City FC | Swansea | Swansea City | Swansea City AFC
82
+ West Bromwich Albion FC | West Brom | West Bromwich Albion | West Bromwich | Albion
83
+ AFC Bournemouth | Bournemouth | A.F.C. Bournemouth | Bournemouth FC
84
+ Brighton & Hove Albion FC | Brighton | Brighton & Hove | Brighton & Hove Albion
85
+ Manchester City FC | Man City | Manchester City | Man. City | Manchester C
86
+ Newcastle United FC | Newcastle | Newcastle Utd | Newcastle United
87
+ Tottenham Hotspur FC | Tottenham | Tottenham Hotspur | Spurs
88
+ Manchester United FC | Man Utd | Man. United | Manchester U. | Manchester Utd | Manchester United
89
+ West Ham United FC | West Ham | West Ham United
84
90
  TXT
85
91
 
86
92
 
data/test/test_read.rb ADDED
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_read.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestRead < MiniTest::Test
12
+
13
+ def test_read
14
+
15
+ SportDb.connect( adapter: 'sqlite3', database: ':memory:' )
16
+ SportDb.create_all ## build schema
17
+
18
+ ## turn on logging to console
19
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
20
+
21
+
22
+ path = "../../../openfootball/england/2015-16/.conf.txt"
23
+ # path = "../../../openfootball/england/2017-18/.conf.txt"
24
+ # path = "../../../openfootball/england/2018-19/.conf.txt"
25
+ # path = "../../../openfootball/england/2019-20/.conf.txt"
26
+ SportDb.read( path )
27
+ path = "../../../openfootball/england/2015-16/1-premierleague-i.txt"
28
+ # path = "../../../openfootball/england/2017-18/1-premierleague-i.txt"
29
+ # path = "../../../openfootball/england/2018-19/1-premierleague.txt"
30
+ # path = "../../../openfootball/england/2019-20/1-premierleague.txt"
31
+ SportDb.read( path )
32
+ # path = "../../../openfootball/england/2017-18/1-premierleague-ii.txt"
33
+ # SportDb.read( path )
34
+ end # method test_read
35
+ end # class TestReader
data/test/test_reader.rb CHANGED
@@ -19,11 +19,13 @@ class TestReader < MiniTest::Test
19
19
  ActiveRecord::Base.logger = Logger.new(STDOUT)
20
20
 
21
21
 
22
+ # path = "../../../openfootball/austria/2018-19/.conf.txt"
22
23
  path = "../../../openfootball/england/2015-16/.conf.txt"
23
24
  # path = "../../../openfootball/england/2017-18/.conf.txt"
24
25
  # path = "../../../openfootball/england/2018-19/.conf.txt"
25
26
  # path = "../../../openfootball/england/2019-20/.conf.txt"
26
27
  recs = SportDb::EventReaderV2.read( path )
28
+ # path = "../../../openfootball/austria/2018-19/1-bundesliga.txt"
27
29
  path = "../../../openfootball/england/2015-16/1-premierleague-i.txt"
28
30
  # path = "../../../openfootball/england/2017-18/1-premierleague-i.txt"
29
31
  # path = "../../../openfootball/england/2018-19/1-premierleague.txt"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb-readers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-30 00:00:00.000000000 Z
11
+ date: 2019-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sportdb-config
@@ -89,6 +89,7 @@ files:
89
89
  - lib/sportdb/readers/version.rb
90
90
  - test/helper.rb
91
91
  - test/test_match_parser.rb
92
+ - test/test_read.rb
92
93
  - test/test_reader.rb
93
94
  homepage: https://github.com/sportdb/sport.db
94
95
  licenses: