-
Notifications
You must be signed in to change notification settings - Fork 0
/
football_data.rb
178 lines (141 loc) · 4.46 KB
/
football_data.rb
1
2
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
class FootballData
include HTTParty
base_uri 'http://www.football-data.org'
attr_accessor :excluded_countries
attr_accessor :excluded_league_names
attr_accessor :cache_filename
attr_reader :fixtures
attr_reader :newest_to_update
def initialize
@cache_filename = 'api_data_cache.json'
@excluded_countries = []
@excluded_league_names = []
end
def set_url url
self.base_uri url
end
def load_all_fixtures
puts "Searching for #{@cache_filename}..."
if File.exist? @cache_filename and (File.mtime('api_data_cache.json') + 1.day >= Time.now or @cache_filename != 'api_data_cache.json')
puts '...found!'
read_from_cache
else
puts '...Not detected or outdated, re-downloading matches'
download_all_fixtures
end
end
def download_all_fixtures
ids = league_ids
@fixtures = ids.map do |id|
fixtures_by_league id
end if not ids.nil?
remove_excluded_countries!
remove_excluded_league_names!
#add_outdated_leagues_to_excluded!
find_newest_to_update
puts "Next to be updated: #{@newest_to_update}"
fixtures_to_file @cache_filename
end
def next_update_time
find_newest_to_update
@newest_to_update[:time]
end
def read_from_cache
@fixtures = JSON.parse(File.read @cache_filename).map do |season|
season.deep_symbolize_keys!
end
remove_excluded_countries!
remove_excluded_league_names!
#add_outdated_leagues_to_excluded!
end
private
def remove_excluded_countries!
@fixtures.select! { |fixture| @excluded_countries.index( fixture[:country] ).nil? }
end
def remove_excluded_league_names!
@fixtures.select! { |fixture| @excluded_league_names.index( fixture[:league_name] ).nil? }
end
def add_outdated_leagues_to_excluded!
find_newest_to_update
if @newest_to_update[:time] < DateTime.now
puts "Removing outdated: #{@newest_to_update[:league_name]} because #{@newest_to_update} is a past game and has no result"
@excluded_league_names << @newest_to_update[:league_name]
remove_excluded_league_names!
add_outdated_leagues_to_excluded!
end
end
def find_newest_to_update
@newest_to_update = nil
newest_league_name = nil
@fixtures.each do |fixture|
fixture[:matches].select { |match| match[:time] >= DateTime.now }.each do |match|
if match[:team1_goals] == -1
if @newest_to_update.nil?
@newest_to_update = match.dup
newest_league_name = fixture[:league_name]
else
if @newest_to_update[:time] > match[:time]
@newest_to_update = match.dup
newest_league_name = fixture[:league_name]
end
end
end
end
end
return {} if @newest_to_update.nil?
@newest_to_update[:league_name] = newest_league_name
@newest_to_update
end
def country_of_league league
if league.include? 'Bundesliga'
'Germany'
elsif league.include? 'Serie'
'Italy'
elsif league.include? 'Division'
'Spain'
elsif league.include? 'Ligue'
'France'
elsif league.include? 'Premier'
'United Kingdom'
else
'Netherlands'
end
end
def league_ids
response = self.class.get('/soccerseasons')
return nil if response.code != 200
JSON.parse(response.body).select do |season|
current_season = DateTime.now.year
current_season -= 1 if DateTime.now.month < 8
season['year'].to_i == current_season
end.map { |season| {id: season['id'], league_name: season['caption'], year: season['year']} }
end
def fixtures_by_league id
response = self.class.get("/soccerseasons/#{id[:id]}/fixtures")
return nil if response.code != 200
puts "Parsing league #{id[:league_name]}"
{
league_name: id[:league_name],
year: id[:year],
country: country_of_league(id[:league_name]),
league_api_id: id[:id],
matches: JSON.parse(response.body).map do |fixture|
{
team1_name: fixture['homeTeam'],
team2_name: fixture['awayTeam'],
team1_goals: fixture['goalsHomeTeam'].to_i,
team2_goals: fixture['goalsAwayTeam'].to_i,
time: fixture['date'],
matchday: fixture['matchday']
}
end
}
end
def fixtures_to_file filename
return if @fixtures.nil?
puts "Saving file #{filename}..."
File.open(filename, 'w') do |f|
f.write(JSON.pretty_generate @fixtures)
end
end
end