-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathget_match_results.R
300 lines (240 loc) · 11.2 KB
/
get_match_results.R
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#' Get single match results
#'
#' Internal function used in get_match_results
#'
#' @param fixture_url URL of the league season's fixtures and results page
#' @param time_pause the wait time (in seconds) between page loads
#'
#' @return returns a raw dataframe with the results of the league season
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom utils read.csv
#'
#' @export
#' @noRd
#'
#' @examples
#' \dontrun{
#' try({
#' df <- .get_each_season_results(fixture_url =
#' "https://fbref.com/en/comps/9/schedule/Premier-League-Scores-and-Fixtures"
#' )
#'
#' })
#' }
.get_each_season_results <- function(fixture_url, time_pause=3) {
main_url <- "https://fbref.com"
# pb$tick()
# put sleep in as per new user agreement on FBref
Sys.sleep(time_pause)
fixtures_page <- .load_page(fixture_url)
season_name <- fixtures_page %>% rvest::html_nodes("h2 span") %>% rvest::html_text() %>% .[1]
tab_holder <- fixtures_page %>%
rvest::html_node(".stats_table tbody") %>% rvest::html_nodes("tr")
spacer_idx <- !grepl("spacer partial", xml2::xml_attrs(tab_holder))
tab_holder <- tab_holder[spacer_idx]
idx_rm <- grep("thead", xml2::xml_attrs(tab_holder))
if(length(idx_rm) != 0) {
tab_holder <- tab_holder[-idx_rm]
}
season_summary <- tryCatch(fixtures_page %>%
rvest::html_table() %>% .[1] %>% data.frame(), error = function(e) data.frame())
# error handling - The first available Iranian season has something weird in the HTML, meaning the above code wont work
# (https://fbref.com/en/comps/64/741/schedule/2014-2015-Persian-Gulf-Pro-League-Scores-and-Fixtures)
if(nrow(season_summary) == 0) {
season_summary <- fixtures_page %>% rvest::html_node(".stats_table") %>%
rvest::html_table() %>% data.frame()
}
season_summary <- season_summary[spacer_idx,]
if(length(idx_rm) != 0) {
season_summary <- season_summary[-idx_rm,]
}
get_url <- function(tab_element) {
a <- tab_element %>% rvest::html_node(xpath='.//*[@data-stat="match_report"]//a') %>% rvest::html_attr("href")
if(is.na(a) || length(a) == 0) {
a <- NA_character_
} else {
a <- paste0(main_url, a)
}
return(a)
}
match_urls <- purrr::map_chr(tab_holder, get_url)
# match_urls <- match_urls[!duplicated(match_urls, incomparables = NA)]
suppressWarnings(
season_summary <- season_summary %>%
dplyr::filter(is.na(.data[["Time"]]) | .data[["Time"]] != "Time") %>%
dplyr::mutate(Score = gsub("\u2013", " ", .data[["Score"]]) %>% stringr::str_squish()) %>%
tidyr::separate(.data[["Score"]], into = c("HomeGoals", "AwayGoals"), sep = " ") %>%
dplyr::mutate(HomeGoals = as.numeric(.data[["HomeGoals"]]),
AwayGoals = as.numeric(.data[["AwayGoals"]]),
Attendance = as.numeric(gsub(",", "", .data[["Attendance"]])))
)
season_summary <- season_summary %>%
dplyr::mutate(HomeGoals = ifelse(is.na(.data[["HomeGoals"]]) & !is.na(.data[["AwayGoals"]]), .data[["AwayGoals"]], .data[["HomeGoals"]]),
AwayGoals = ifelse(is.na(.data[["AwayGoals"]]) & !is.na(.data[["HomeGoals"]]), .data[["HomeGoals"]], .data[["AwayGoals"]]))
season_summary <- cbind(fixture_url, season_summary)
if(!any(stringr::str_detect(names(season_summary), "Round"))) {
Round <- rep(NA, nrow(season_summary))
season_summary <- cbind(Round, season_summary)
}
if(!any(stringr::str_detect(names(season_summary), "Wk"))) {
Wk <- rep(NA, nrow(season_summary))
season_summary <- cbind(Wk, season_summary)
}
if(any(stringr::str_detect(names(season_summary), "xG"))) {
season_summary <- season_summary %>%
dplyr::select(.data[["fixture_url"]], Round, .data[["Wk"]], .data[["Day"]], .data[["Date"]], .data[["Time"]], .data[["Home"]], .data[["HomeGoals"]], Home_xG=.data[["xG"]], .data[["Away"]], .data[["AwayGoals"]], Away_xG=.data[["xG.1"]], .data[["Attendance"]], .data[["Venue"]], .data[["Referee"]], .data[["Notes"]]) %>%
dplyr::mutate(Home_xG = as.numeric(.data[["Home_xG"]]),
Away_xG = as.numeric(.data[["Away_xG"]]))
} else {
season_summary <- season_summary %>%
dplyr::select(.data[["fixture_url"]], Round, .data[["Wk"]], .data[["Day"]], .data[["Date"]], .data[["Time"]], .data[["Home"]], .data[["HomeGoals"]], .data[["Away"]], .data[["AwayGoals"]], .data[["Attendance"]], .data[["Venue"]], .data[["Referee"]], .data[["Notes"]])
}
season_summary <- season_summary %>%
dplyr::mutate(Wk = as.character(.data[["Wk"]])) %>%
dplyr::mutate(MatchURL = match_urls)
return(season_summary)
}
#' Get FBref match results
#'
#' Returns the game results for a given league season(s)
#' Replaces the deprecated function get_match_results
#'
#' @param country the three character country code
#' @param gender gender of competition, either "M" or "F"
#' @param season_end_year the year(s) the season concludes
#' @param tier the tier of the league, ie '1st' for the EPL or '2nd' for the Championship and so on
#' @param non_dom_league_url the URL for Cups and Competitions found at https://fbref.com/en/comps/
#'
#' @return returns a dataframe with the results of the competition, season and gender
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom utils read.csv
#'
#' @export
#'
#' @examples
#' \dontrun{
#' try({
#' df <- fb_match_results(country = c("ITA"), gender = "M", season_end_year = 2021)
#' # for results from English Championship:
#' df <- fb_match_results(country = "ENG", gender = "M", season_end_year = 2021, tier = "2nd")
#' # for international friendlies:
#'
#' })
#' }
fb_match_results <- function(country, gender, season_end_year, tier = "1st", non_dom_league_url = NA) {
main_url <- "https://fbref.com"
# .pkg_message("Scraping match results")
country_abbr <- country
gender_M_F <- gender
season_end_year_num <- season_end_year
comp_tier <- tier
cups_url <- non_dom_league_url
seasons <- read.csv("https://raw.githubusercontent.com/JaseZiv/worldfootballR_data/master/raw-data/all_leages_and_cups/all_competitions.csv", stringsAsFactors = F)
if(is.na(cups_url)) {
fixtures_urls <- seasons %>%
dplyr::filter(stringr::str_detect(.data[["competition_type"]], "Leagues")) %>%
dplyr::filter(country %in% country_abbr,
gender %in% gender_M_F,
season_end_year %in% season_end_year_num,
tier %in% comp_tier,
!is.na(.data[["fixtures_url"]])) %>%
dplyr::arrange(season_end_year) %>%
dplyr::pull(.data[["fixtures_url"]]) %>% unique()
} else {
fixtures_urls <- seasons %>%
dplyr::filter(.data[["comp_url"]] %in% cups_url,
gender %in% gender_M_F,
season_end_year %in% season_end_year_num,
!is.na(.data[["fixtures_url"]])) %>%
dplyr::arrange(season_end_year) %>%
dplyr::pull(.data[["fixtures_url"]]) %>% unique()
}
stopifnot("Data not available for the season(s) selected" = length(fixtures_urls) > 0)
# create the progress bar with a progress function.
# pb <- progress::progress_bar$new(total = length(fixtures_urls))
all_results <- fixtures_urls %>%
purrr::map_df(.get_each_season_results)
all_results <- seasons %>%
dplyr::select(Competition_Name=.data[["competition_name"]], Gender=.data[["gender"]], Country=.data[["country"]], Season_End_Year=.data[["season_end_year"]], .data[["seasons_urls"]], .data[["fixtures_url"]]) %>%
dplyr::right_join(all_results, by = c("fixtures_url" = "fixture_url")) %>%
dplyr::select(-.data[["seasons_urls"]], -.data[["fixtures_url"]]) %>%
dplyr::mutate(Date = lubridate::ymd(.data[["Date"]])) %>%
dplyr::arrange(.data[["Country"]], .data[["Competition_Name"]], .data[["Gender"]], .data[["Season_End_Year"]], .data[["Wk"]], .data[["Date"]], .data[["Time"]]) %>% dplyr::distinct(.keep_all = T)
# .pkg_message("Match results finished scraping")
return(all_results)
}
#' Get match results
#'
#' Returns the game results for a given league season(s)
#'
#' @param country the three character country code
#' @param gender gender of competition, either "M" or "F"
#' @param season_end_year the year(s) the season concludes
#' @param tier the tier of the league, ie '1st' for the EPL or '2nd' for the Championship and so on
#' @param non_dom_league_url the URL for Cups and Competitions found at https://fbref.com/en/comps/
#'
#' @return returns a dataframe with the results of the competition, season and gender
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom utils read.csv
#'
#' @export
#'
#' @examples
#' \dontrun{
#' try({
#' df <- get_match_results(country = c("ITA"), gender = "M", season_end_year = 2021)
#' # for results from English Championship:
#' df <- get_match_results(country = "ENG", gender = "M", season_end_year = 2021, tier = "2nd")
#' # for international friendlies:
#'
#' })
#' }
get_match_results <- function(country, gender, season_end_year, tier = "1st", non_dom_league_url = NA) {
.Deprecated("fb_match_results")
main_url <- "https://fbref.com"
# .pkg_message("Scraping match results")
country_abbr <- country
gender_M_F <- gender
season_end_year_num <- season_end_year
comp_tier <- tier
cups_url <- non_dom_league_url
seasons <- read.csv("https://raw.githubusercontent.com/JaseZiv/worldfootballR_data/master/raw-data/all_leages_and_cups/all_competitions.csv", stringsAsFactors = F)
if(is.na(cups_url)) {
fixtures_urls <- seasons %>%
dplyr::filter(stringr::str_detect(.data[["competition_type"]], "Leagues")) %>%
dplyr::filter(country %in% country_abbr,
gender %in% gender_M_F,
season_end_year %in% season_end_year_num,
tier %in% comp_tier,
!is.na(.data[["fixtures_url"]])) %>%
dplyr::arrange(season_end_year) %>%
dplyr::pull(.data[["fixtures_url"]]) %>% unique()
} else {
fixtures_urls <- seasons %>%
dplyr::filter(.data[["comp_url"]] %in% cups_url,
gender %in% gender_M_F,
season_end_year %in% season_end_year_num,
!is.na(.data[["fixtures_url"]])) %>%
dplyr::arrange(season_end_year) %>%
dplyr::pull(.data[["fixtures_url"]]) %>% unique()
}
stopifnot("Data not available for the season(s) selected" = length(fixtures_urls) > 0)
# create the progress bar with a progress function.
# pb <- progress::progress_bar$new(total = length(fixtures_urls))
all_results <- fixtures_urls %>%
purrr::map_df(.get_each_season_results)
all_results <- seasons %>%
dplyr::select(Competition_Name=.data[["competition_name"]], Gender=.data[["gender"]], Country=.data[["country"]], Season_End_Year=.data[["season_end_year"]], .data[["seasons_urls"]], .data[["fixtures_url"]]) %>%
dplyr::right_join(all_results, by = c("fixtures_url" = "fixture_url")) %>%
dplyr::select(-.data[["seasons_urls"]], -.data[["fixtures_url"]]) %>%
dplyr::mutate(Date = lubridate::ymd(.data[["Date"]])) %>%
dplyr::arrange(.data[["Country"]], .data[["Competition_Name"]], .data[["Gender"]], .data[["Season_End_Year"]], .data[["Wk"]], .data[["Date"]], .data[["Time"]]) %>% dplyr::distinct(.keep_all = T)
# .pkg_message("Match results finished scraping")
return(all_results)
}