-
Notifications
You must be signed in to change notification settings - Fork 61
/
fotmob_matches.R
313 lines (287 loc) · 9.3 KB
/
fotmob_matches.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
301
302
303
304
305
306
307
308
309
310
311
312
313
.extract_fotmob_match_general <- function(url) {
resp <- safely_from_json(url)$result
general <- resp$general
scalars <- data.frame(
stringsAsFactors = FALSE,
match_id = general$matchId, ## don't technically need this since `.fotmob_get_single_match_details` is wrapped with `.wrap_fotmob_match_id_f`
match_round = ifelse(is.null(general$matchRound), "", general$matchRound),
league_id = general$leagueId,
league_name = general$leagueName,
league_round_name = general$leagueRoundName,
parent_league_id = general$parentLeagueId,
parent_league_season = general$parentLeagueSeason,
match_time_utc = general$matchTimeUTC
)
teams <- data.frame(
stringsAsFactors = FALSE,
home_team_id = unlist(general$homeTeam$id),
home_team = unlist(general$homeTeam$name),
home_team_color = unlist(general$teamColors$home),
away_team_id = unlist(general$awayTeam$id),
away_team = unlist(general$awayTeam$name),
away_team_color = unlist(general$teamColors$away)
)
list(
resp = resp,
scalars = scalars,
teams = teams
)
}
#' Get fotmob match results by date
#'
#' Returns match results for all matches played on the selected date from fotmob.com
#'
#' @param dates a vector of string-formatted dates in "Ymd" format, e.g. "20210926". An attempt is
#' made to coerce the input to the necessary format if a date is passed in.
#'
#' @return returns a dataframe of match results
#'
#' @importFrom purrr map_dfr
#'
#' @export
#'
#' @examples
#' \donttest{
#' try({
#' library(dplyr)
#' library(tidyr)
#'
#' results <- fotmob_get_matches_by_date(date = c("20210925", "20210926"))
#' results %>%
#' dplyr::select(primary_id, ccode, league_name = name, match_id)
#' })
#' }
#'
fotmob_get_matches_by_date <- function(dates) {
purrr::map_dfr(dates, .fotmob_get_matches_by_single_date)
}
#' @importFrom lubridate is.Date ymd
#' @importFrom stringr str_remove_all
#' @importFrom glue glue
#' @importFrom janitor clean_names
#' @importFrom purrr possibly
#' @importFrom tibble as_tibble tibble
#' @importFrom tidyr unnest
#' @importFrom dplyr rename select
#' @importFrom tidyselect vars_select_helpers
#' @importFrom magrittr %>%
.fotmob_get_matches_by_single_date <- function(date) {
# CRAN feedback was to remove this from the existing functions so I have for now
# print(glue::glue('Scraping match results data from fotmob for "{date}".'))
main_url <- "https://www.fotmob.com/api/"
is_date <- lubridate::is.Date(date)
if(is_date) {
date <- lubridate::ymd(date)
}
date <- stringr::str_remove_all(as.character(date), "-")
url <- paste0(main_url, "matches?date=", date)
f <- function(url) {
resp <- safely_from_json(url)$result
res <- resp$leagues %>%
janitor::clean_names() %>%
tibble::as_tibble()
if(nrow(res) == 0) {
return(res)
}
res %>%
dplyr::rename(match = .data[["matches"]]) %>%
tidyr::unnest(.data[["match"]], names_sep = "_") %>%
dplyr::rename(home = .data[["match_home"]], away = .data[["match_away"]]) %>%
tidyr::unnest(c(.data[["home"]], .data[["away"]], .data[["match_status"]]), names_sep = "_") %>%
dplyr::select(-tidyselect::vars_select_helpers$where(is.list)) %>%
janitor::clean_names()
}
fp <- purrr::possibly(f, otherwise = tibble::tibble())
fp(url)
}
#' Get fotmob match details by match id
#'
#' Returns match details from fotmob.com
#'
#' @param match_ids a vector of strings or numbers representing matches
#'
#' @return returns a dataframe of match shots
#'
#' @examples
#' \donttest{
#' try({
#' library(dplyr)
#' library(tidyr)
#' results <- fotmob_get_matches_by_date(date = "20210926")
#' match_ids <- results %>%
#' dplyr::select(primary_id, ccode, league_name = name, match_id) %>%
#' dplyr::filter(league_name == "Premier League", ccode == "ENG") %>%
#' dplyr::pull(match_id)
#' match_ids # 3609987 3609979
#' details <- fotmob_get_match_details(match_ids)
#' })
#' }
#' @export
fotmob_get_match_details <- function(match_ids) {
.wrap_fotmob_match_f(match_ids, .fotmob_get_single_match_details)
}
#' @importFrom glue glue
#' @importFrom purrr possibly
#' @importFrom dplyr bind_cols mutate case_when
#' @importFrom janitor clean_names
#' @importFrom rlang .data
#' @importFrom tibble as_tibble tibble
#' @importFrom tidyr unnest unnest_wider
.fotmob_get_single_match_details <- function(match_id) {
# CRAN feedback was to remove this from the existing functions so I have for now
# print(glue::glue("Scraping match data from fotmob for match {match_id}."))
main_url <- "https://www.fotmob.com/api/"
url <- paste0(main_url, "matchDetails?matchId=", match_id)
f <- function(url) {
general <- .extract_fotmob_match_general(url)
df <- dplyr::bind_cols(
general$scalars,
general$teams
)
shots <- general$resp$content$shotmap$shots %>% janitor::clean_names()
has_shots <- length(shots) > 0
df <- tibble::as_tibble(df)
if(isTRUE(has_shots)) {
df$shots <- list(shots)
df <- df %>%
tidyr::unnest(.data[["shots"]]) %>%
tidyr::unnest_wider(.data[["on_goal_shot"]], names_sep = "_") %>%
janitor::clean_names()
} else {
df$shots <- NULL
}
df
}
fp <- purrr::possibly(f, otherwise = tibble::tibble())
fp(url)
}
#' Get fotmob match top team stats by match id
#'
#' Returns match top team stats from fotmob.com
#'
#' @param match_ids a vector of strings or numbers representing matches
#'
#' @return returns a dataframe of match top team stats
#'
#' @examples
#' \donttest{
#' try({
#' library(dplyr)
#' library(tidyr)
#' results <- fotmob_get_matches_by_date(date = "20210926")
#' match_ids <- results %>%
#' dplyr::select(primary_id, ccode, league_name = name, match_id) %>%
#' dplyr::filter(league_name == "Premier League", ccode == "ENG") %>%
#' dplyr::pull(match_id)
#' match_ids # 3609987 3609979
#' details <- fotmob_get_match_team_stats(match_ids)
#' })
#' }
#' @export
fotmob_get_match_team_stats <- function(match_ids) {
.wrap_fotmob_match_f(match_ids, .fotmob_get_single_match_team_stats)
}
#' @importFrom purrr possibly
#' @importFrom dplyr bind_cols mutate across rename
#' @importFrom janitor clean_names
#' @importFrom rlang .data
#' @importFrom tibble as_tibble
#' @importFrom tidyr unnest unnest_wider hoist
.fotmob_get_single_match_team_stats <- function(match_id) {
main_url <- "https://www.fotmob.com/api/"
url <- paste0(main_url, "matchDetails?matchId=", match_id)
f <- function(url) {
general <- .extract_fotmob_match_general(url)
df <- dplyr::bind_cols(
general$scalars,
general$teams
)
stats <- general$resp$content$stats$stats %>% janitor::clean_names()
has_stats <- length(stats) > 0
df <- tibble::as_tibble(df)
if(isTRUE(has_stats)) {
wide_stats <- stats %>%
tidyr::unnest_wider(stats, names_sep = '_') %>%
tidyr::unnest(vars_select_helpers$where(is.list))
clean_stats <- wide_stats %>%
tidyr::hoist(
.data[["stats_stats"]],
"home_value" = 1
) %>%
dplyr::rename("away_value" = .data[["stats_stats"]]) %>%
dplyr::mutate(
dplyr::across(c(.data[["home_value"]], .data[["away_value"]]), as.character)
) %>%
tidyr::unnest(c(.data[["home_value"]], .data[["away_value"]]))
df <- dplyr::bind_cols(df, clean_stats)
}
df
}
fp <- purrr::possibly(f, otherwise = tibble::tibble())
fp(url)
}
#' Get fotmob match info by match id
#'
#' Returns match info from fotmob.com
#'
#' @param match_ids a vector of strings or numbers representing matches
#'
#' @return returns a dataframe of match info
#'
#' @examples
#' \donttest{
#' try({
#' library(dplyr)
#' library(tidyr)
#' results <- fotmob_get_matches_by_date(date = "20210926")
#' match_ids <- results %>%
#' dplyr::select(primary_id, ccode, league_name = name, match_id) %>%
#' dplyr::filter(league_name == "Premier League", ccode == "ENG") %>%
#' dplyr::pull(match_id)
#' match_ids # 3609987 3609979
#' details <- fotmob_get_match_info(match_ids)
#' })
#' }
#' @export
fotmob_get_match_info <- function(match_ids) {
.wrap_fotmob_match_f(match_ids, .fotmob_get_single_match_info)
}
#' @importFrom purrr possibly
#' @importFrom dplyr bind_cols
#' @importFrom janitor clean_names
#' @importFrom rlang .data
#' @importFrom tibble tibble enframe
#' @importFrom tidyr pivot_wider unnest unnest_wider
.fotmob_get_single_match_info <- function(match_id) {
main_url <- "https://www.fotmob.com/api/"
url <- paste0(main_url, "matchDetails?matchId=", match_id)
f <- function(url) {
general <- .extract_fotmob_match_general(url)
df <- dplyr::bind_cols(
general$scalars,
general$teams
)
info <- general$resp$content$matchFacts$infoBox
unnested_info <- info %>%
tibble::enframe() %>%
tidyr::pivot_wider(
names_from = .data[["name"]],
values_from = .data[["value"]]
) %>%
janitor::clean_names() %>%
tidyr::unnest_wider(
c(tidyselect::vars_select_helpers$where(is.list), -tidyselect::vars_select_helpers$any_of("attendance")),
names_sep = "_"
) %>%
tidyr::unnest(
tidyselect::vars_select_helpers$any_of("attendance")
) %>%
janitor::clean_names()
if (nrow(df) != 1) {
return(tibble::tibble())
}
dplyr::bind_cols(df, unnested_info)
}
fp <- purrr::possibly(f, otherwise = tibble::tibble())
fp(url)
}