sportdb-writers 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,195 @@
1
+
2
+ module SportDb
3
+
4
+ ###
5
+ ## todo/fix:
6
+ ## add -i/--interactive flag
7
+ ## will prompt yes/no before git operations (with consequences)!!!
8
+
9
+ class GitHubSync
10
+
11
+ ## map leagues to repo+path
12
+ ## e.g. fr.1 => europe/france
13
+ ## eng..1 => england
14
+ REPOS = {
15
+ 'at.1' => 'austria',
16
+ 'at.2' => 'austria',
17
+ 'at.3.o' => 'austria',
18
+ 'at.cup' => 'austria',
19
+
20
+ 'de.1' => 'deutschland',
21
+ 'de.2' => 'deutschland',
22
+ 'de.3' => 'deutschland',
23
+ 'de.cup' => 'deutschland',
24
+
25
+ 'eng.1' => 'england',
26
+ 'eng.2' => 'england',
27
+ 'eng.3' => 'england',
28
+ 'eng.4' => 'england',
29
+ 'eng.5' => 'england',
30
+ 'eng.cup' => 'england', # English FA Cup
31
+
32
+ 'es.1' => 'espana',
33
+ 'es.2' => 'espana',
34
+
35
+ 'fr.1' => 'europe/france',
36
+ 'fr.2' => 'europe/france',
37
+
38
+ 'hu.1' => 'europe/hungary',
39
+ 'gr.1' => 'europe/greece',
40
+ 'pt.1' => 'europe/portugal',
41
+ 'pt.2' => 'europe/portugal',
42
+
43
+ 'ch.1' => 'europe/switzerland',
44
+ 'ch.2' => 'europe/switzerland',
45
+
46
+ 'tr.1' => 'europe/turkey',
47
+ 'tr.2' => 'europe/turkey',
48
+
49
+ 'is.1' => 'europe/iceland',
50
+ 'sco.1' => 'europe/scotland',
51
+ 'ie.1' => 'europe/ireland',
52
+
53
+ 'fi.1' => 'europe/finland',
54
+ 'se.1' => 'europe/sweden',
55
+ 'se.2' => 'europe/sweden',
56
+ 'no.1' => 'europe/norway',
57
+ 'dk.1' => 'europe/denmark',
58
+
59
+ 'lu.1' => 'europe/luxembourg',
60
+ 'be.1' => 'europe/belgium',
61
+ 'nl.1' => 'europe/netherlands',
62
+ 'cz.1' => 'europe/czech-republic',
63
+
64
+ 'sk.1' => 'europe/slovakia',
65
+ 'hr.1' => 'europe/croatia',
66
+ 'pl.1' => 'europe/poland',
67
+
68
+ 'ro.1' => 'europe/romania',
69
+
70
+ 'ua.1' => 'europe/ukraine',
71
+
72
+ 'ru.1' => 'europe/russia',
73
+ 'ru.2' => 'europe/russia',
74
+
75
+ 'it.1' => 'italy',
76
+ 'it.2' => 'italy',
77
+
78
+ 'mx.1' => 'mexico',
79
+
80
+ 'ar.1' => 'south-america/argentina',
81
+ 'br.1' => 'south-america/brazil',
82
+
83
+ 'cn.1' => 'world/asia/china',
84
+ 'jp.1' => 'world/asia/japan',
85
+ }
86
+
87
+
88
+
89
+ ########
90
+ ## (auto)default to Writer.config.out_dir - why? why not?
91
+ ##
92
+ ## note - is root for org (NOT monotree for now - why?`why not?)
93
+ def self.root() @root || "/sports/openfootball"; end
94
+ def self.root=( dir ) @root = dir; end
95
+ ## use root_dir (alias) - why? why not?
96
+
97
+
98
+ def initialize( datasets )
99
+ @repos = _find_repos( datasets )
100
+ end
101
+
102
+
103
+ def git_push_if_changes
104
+ _git_push_if_changes( @repos )
105
+ end
106
+
107
+ def git_fast_forward_if_clean
108
+ _git_fast_forward_if_clean( @repos )
109
+ end
110
+
111
+
112
+ ## todo/fix: rename to something like
113
+ ## git_(auto_)commit_and_push_if_changes/if_dirty()
114
+
115
+ def _git_push_if_changes( names ) ## optenfootball repo names e.g. world, england, etc.
116
+ message = "auto-update week #{Date.today.cweek}" ## add /#{Date.today.cday - why? why not?
117
+ puts message
118
+
119
+ names.each do |name|
120
+ path = "#{self.class.root}/#{name}"
121
+
122
+ Gitti::GitProject.open( path ) do |proj|
123
+ puts ''
124
+ puts "###########################################"
125
+ puts "## trying to commit & push repo in path >#{path}<"
126
+ puts "Dir.getwd: #{Dir.getwd}"
127
+ output = proj.changes
128
+ if output.empty?
129
+ puts "no changes found; skipping commit & push"
130
+ else
131
+ proj.add( '.' )
132
+ proj.commit( message )
133
+ proj.push
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+
140
+ def _git_fast_forward_if_clean( names )
141
+ names.each do |name|
142
+ path = "#{self.class.root}/#{name}"
143
+
144
+ Gitti::GitProject.open( path ) do |proj|
145
+ output = proj.changes
146
+ unless output.empty?
147
+ puts "FAIL - cannot git pull (fast-forward) - working tree has changes:"
148
+ puts output
149
+ exit 1
150
+ end
151
+
152
+ proj.fast_forward
153
+ end
154
+ end
155
+ end
156
+
157
+
158
+ ## todo/check: find a better name for helper?
159
+ ## note: datasets of format
160
+ ##
161
+ ## DATASETS = [
162
+ ## ['it.1', %w[2020/21 2019/20]],
163
+ ## ['it.2', %w[2019/20]],
164
+ ## ['es.1', %w[2019/20]],
165
+ ## ['es.2', %w[2019/20]],
166
+ ## ]
167
+
168
+
169
+ def _find_repos( datasets )
170
+ repos = []
171
+ datasets.each do |dataset|
172
+ league_key = dataset[0]
173
+ path = REPOS[ league_key ]
174
+ ## pp path
175
+ if path.nil?
176
+ puts "!! ERROR - no repo path found for league >#{league_key}<; sorry"
177
+ exit 1
178
+ end
179
+
180
+ ## auto-add
181
+ ## openfootball/ org here
182
+ ## and keep root "generic" to monoroot - why? why not?
183
+
184
+ ## use only first part e.g. europe/belgium => europe
185
+ repos << path.split( '/' )[0]
186
+ end
187
+ pp repos
188
+ repos.uniq ## note: remove duplicates (e.g. europe or world or such)
189
+ end
190
+
191
+
192
+
193
+
194
+ end # class GitHub
195
+ end # module SportDb
@@ -0,0 +1,57 @@
1
+ ######
2
+ # goals helper
3
+ # todo/check/fix: move upstream for (re)use - why? why not?
4
+
5
+
6
+ module Writer
7
+
8
+
9
+ def self.merge_goals( matches, goals )
10
+ goals_by_match = goals.group_by { |rec| rec.match_id }
11
+ puts "match goal reports - #{goals_by_match.size} records"
12
+
13
+ ## lets group by date for easier lookup
14
+ matches_by_date = matches.group_by { |rec| rec.date }
15
+
16
+
17
+ ## note: "shadow / reuse" matches and goals vars for now in loop
18
+ ## find better names to avoid confusion!!
19
+ goals_by_match.each_with_index do |(match_id, goals),i|
20
+ ## split match_id
21
+ team_str, more_str = match_id.split( '|' )
22
+ team1_str, team2_str = team_str.split( ' - ' )
23
+
24
+ more_str = more_str.strip
25
+ team1_str = team1_str.strip
26
+ team2_str = team2_str.strip
27
+
28
+ ## for now assume date in more (and not round or something else)
29
+ date_str = more_str # e.g. in 2019-07-26 format
30
+
31
+ puts ">#{team1_str}< - >#{team2_str}< | #{date_str}, #{goals.size} goals"
32
+
33
+ ## try a join - find matching match
34
+ matches = matches_by_date[ date_str ]
35
+ if matches.nil?
36
+ puts "!! ERROR: no match found for date >#{date_str}<"
37
+ exit 1
38
+ end
39
+
40
+ found_matches = matches.select {|match| match.team1 == team1_str &&
41
+ match.team2 == team2_str }
42
+
43
+ if found_matches.size == 1
44
+ match = found_matches[0]
45
+ match.goals = SportDb::Import::Goal.build( goals )
46
+ else
47
+ puts "!!! ERROR: found #{found_matches.size} in #{matches.size} matches for date >#{date_str}<:"
48
+ matches.each do |match|
49
+ puts " >#{match.team1}< - >#{match.team2}<"
50
+ end
51
+ exit 1
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+ end # module Writer