├── LICENSE ├── .gitignore ├── .Rbuildignore ├── NAMESPACE ├── bounceR.Rproj ├── DESCRIPTION ├── man ├── get_completed_matches.Rd ├── get_hawkeye_stats_all.Rd └── get_hawkeye_stats.Rd ├── LICENSE.md ├── README.md ├── README.Rmd └── R ├── hawkeye_all.R ├── hawkeye.R └── matches.R /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2021 2 | COPYRIGHT HOLDER: Richard Little 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .Rdata 4 | .httr-oauth 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^bouncer\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^README\.Rmd$ 5 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(get_completed_matches) 4 | export(get_hawkeye_stats) 5 | export(get_hawkeye_stats_all) 6 | import(curl) 7 | import(dplyr) 8 | import(jsonlite) 9 | import(stringr) 10 | import(tidyr) 11 | importFrom(stats,na.omit) 12 | importFrom(stats,setNames) 13 | importFrom(utils,read.csv) 14 | -------------------------------------------------------------------------------- /bounceR.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | LineEndingConversion: Posix 18 | 19 | BuildType: Package 20 | PackageUseDevtools: Yes 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: bounceR 2 | Title: A Cricket package to make Hawkeye data accessible 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "Richard", 6 | family = "Little", 7 | role = c("aut", "cre"), 8 | email = "alittlefitness@gmail.com") 9 | Description: bounceR provides a set of functions to extract Hawkeye data for Cricket from json files on the ICC and BCCI webpages, 10 | turning them into tidy data for analysis. Current data extracted includes Test matches, ODI and IPL T20 competitions. 11 | License: MIT + file LICENSE 12 | Encoding: UTF-8 13 | LazyData: true 14 | Roxygen: list(markdown = TRUE) 15 | RoxygenNote: 7.1.1 16 | Imports: 17 | curl, 18 | dplyr, 19 | stringr, 20 | tidyr, 21 | jsonlite 22 | -------------------------------------------------------------------------------- /man/get_completed_matches.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/matches.R 3 | \name{get_completed_matches} 4 | \alias{get_completed_matches} 5 | \title{Tidy metadata from completed Test, ODI or IPL T20 matches} 6 | \usage{ 7 | get_completed_matches(type = "TEST", competition = "MEN") 8 | } 9 | \arguments{ 10 | \item{type}{Type of match data to return: must be one of "TEST", "ODI" or "IPL". Default is "TEST".} 11 | 12 | \item{competition}{Men's or Women's competitions: must be one of "MEN" or "WOMEN". Default is "MEN".} 13 | } 14 | \value{ 15 | A tibble containing metadata for Test, ODI or IPL T20 matches. 16 | } 17 | \description{ 18 | Tidy metadata from completed Test, ODI or IPL T20 matches 19 | } 20 | \examples{ 21 | df <- get_completed_matches(type = "TEST", competition = "MEN") 22 | df <- get_completed_matches(type = "ODI", competition = "WOMEN") 23 | df <- get_completed_matches(type = "IPL", competition = "MEN") 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2021 Richard Little 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /man/get_hawkeye_stats_all.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hawkeye_all.R 3 | \name{get_hawkeye_stats_all} 4 | \alias{get_hawkeye_stats_all} 5 | \title{Hawkeye Stats for All Tests, ODI or IPL matches} 6 | \usage{ 7 | get_hawkeye_stats_all(type = "TEST", competition = "MEN") 8 | } 9 | \arguments{ 10 | \item{type}{Type of match data to return: must be one of "TEST", "ODI" or "IPL". Default is "TEST".} 11 | 12 | \item{competition}{Men's or Women's competitions: must be one of "MEN" or "WOMEN". Default is "MEN".} 13 | } 14 | \value{ 15 | A tibble containing the Hawkey stats for all available matches. The Hawkeye stats include the ball speed in metres/second, 16 | the x, y location when the ball impacts the pitch, the x, y location of the ball as it passes the stumps and the x, y location 17 | on the field after the batter has played their shot. x, y locations are presented in metres. Individual delivery Hawkeye stats 18 | may be missing if the technology failed. Ball tracking is not available for all matches and field x, y locations are not 19 | available for all matches. Some matches include field x, y locations for non scoring shots, while others only include 20 | field x, y locations for scoring shots. 21 | } 22 | \description{ 23 | Hawkeye Stats for All Tests, ODI or IPL matches 24 | } 25 | \examples{ 26 | df <- get_hawkeye_stats_all(type = "TEST", competition = "WOMEN") 27 | } 28 | -------------------------------------------------------------------------------- /man/get_hawkeye_stats.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hawkeye.R 3 | \name{get_hawkeye_stats} 4 | \alias{get_hawkeye_stats} 5 | \title{Hawkeye Stats for Tests, ODI or IPL matches} 6 | \usage{ 7 | get_hawkeye_stats(id) 8 | } 9 | \arguments{ 10 | \item{id}{Id for match(es) to collect Hawkeye stats from. Invalid ids or ids with no Hawkeye data will result in an empty tibble 11 | or an error. Ids can be found using the get_completed_matches function or via the icc-cricket.com results page for Test and 12 | ODI matches and via the iplt20.com page for IPL matches.} 13 | } 14 | \value{ 15 | A tibble containing the Hawkey stats for the requested matches. The Hawkeye stats include the ball speed in metres/second, 16 | the x, y location when the ball impacts the pitch, the x, y location of the ball as it passes the stumps and the x, y location 17 | on the field after the batter has played their shot. x, y locations are presented in metres. Individual delivery Hawkeye stats 18 | may be missing if the technology failed. Ball tracking is not available for all matches and field x, y locations are not 19 | available for all matches. Some matches include field x, y locations for non scoring shots, while others only include 20 | field x, y locations for scoring shots. 21 | } 22 | \description{ 23 | Hawkeye Stats for Tests, ODI or IPL matches 24 | } 25 | \examples{ 26 | df <- get_hawkeye_stats(id = 23469) 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bounceR 2 | 3 | 4 | 5 | 6 | The bounceR package provides a set of functions to extract the Hawkeye statistics from the ICC and BCCI webpages. The matches currently included are Men's and Women's Tests, ODIs and IPL T20 matches. The Hawkeye statistics included are Ball Speed (m/s), Pitch x,y location (m), Stumps x,y location (m) and the Field x,y location (m) after the Batter's shot. Some matches may only contain Ball Speed, Pitch and Stump location statistics, while others contain only the Field location statistics. Further, some matches may contain the Field Location for all shots, while others contain only scoring shot statistics. 7 | 8 | ## Installation 9 | 10 | You can install the development version from [GitHub](https://github.com/) with: 11 | 12 | ``` r 13 | # install.packages("devtools") 14 | devtools::install_github("alittlefitness/bounceR") 15 | ``` 16 | ## Usage 17 | 18 | As an example to retrieve all Hawkeye stats for Women's Test matches the following code can be used: 19 | 20 | ```{r example} 21 | get_hawkeye_stats_all(type = "TEST", competition = "WOMEN") 22 | ``` 23 | 24 | Alternatively, to retrieve the Hawkeye stats for a single match, the matchId can be supplied to the get_hawkeye_stats function: 25 | 26 | ```{r example2} 27 | get_hawkeye_stats(id = 23469) 28 | ``` 29 | 30 | The matchId can be found on the ICC or BCCI results webpage (the number in this example url https://www.icc-cricket.com/match/23531#scorecard) or via the get_completed_matches function: 31 | 32 | ```{r example2} 33 | get_completed_matches(type = "TEST", competition = "MEN") 34 | ``` 35 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | #!/bin/sh 9 | knitr::opts_chunk$set( 10 | collapse = TRUE, 11 | comment = "#>", 12 | fig.path = "man/figures/README-", 13 | out.width = "100%" 14 | ) 15 | ``` 16 | 17 | # bounceR 18 | 19 | 20 | 21 | 22 | The bounceR package provides a set of functions to extract the Hawkeye statistics from the ICC and BCCI webpages. The matches currently included are Men's and Women's Tests, ODIs and IPL T20 matches. The Hawkeye statistics included are Ball Speed (m/s), Pitch x,y location (m), Stumps x,y location (m) and the Field x,y location (m) after the Batter's shot. Some matches may only contain Ball Speed, Pitch and Stump location statistics, while others contain only the Field location statistics. Further, some matches may contain the Field Location for all shots, while others contain only scoring shot statistics. 23 | 24 | ## Installation 25 | 26 | You can install the development version from [GitHub](https://github.com/) with: 27 | 28 | ``` r 29 | # install.packages("devtools") 30 | devtools::install_github("alittlefitness/bounceR") 31 | ``` 32 | ## Usage 33 | 34 | As an example to retrieve all Hawkeye stats for Women's Test matches the following code can be used: 35 | 36 | ```{r example} 37 | get_hawkeye_stats_all(type = "TEST", competition = "WOMEN") 38 | ``` 39 | 40 | Alternatively, to retrieve the Hawkeye stats for a single match, the matchId can be supplied to the get_hawkeye_stats function: 41 | 42 | ```{r example2} 43 | get_hawkeye_stats(id = 23469) 44 | ``` 45 | 46 | The matchId can be found on the ICC or BCCI results webpage (the number in this example url https://www.icc-cricket.com/match/23531#scorecard) or via the get_completed_matches function: 47 | 48 | ```{r example2} 49 | get_completed_matches(type = "TEST", competition = "MEN") 50 | -------------------------------------------------------------------------------- /R/hawkeye_all.R: -------------------------------------------------------------------------------- 1 | #' Hawkeye Stats for All Tests, ODI or IPL matches 2 | #' 3 | #' @param type Type of match data to return: must be one of "TEST", "ODI" or "IPL". Default is "TEST". 4 | #' @param competition Men's or Women's competitions: must be one of "MEN" or "WOMEN". Default is "MEN". 5 | #' @return A tibble containing the Hawkey stats for all available matches. The Hawkeye stats include the ball speed in metres/second, 6 | #' the x, y location when the ball impacts the pitch, the x, y location of the ball as it passes the stumps and the x, y location 7 | #' on the field after the batter has played their shot. x, y locations are presented in metres. Individual delivery Hawkeye stats 8 | #' may be missing if the technology failed. Ball tracking is not available for all matches and field x, y locations are not 9 | #' available for all matches. Some matches include field x, y locations for non scoring shots, while others only include 10 | #' field x, y locations for scoring shots. 11 | #' @import curl 12 | #' @importFrom utils read.csv 13 | #' @export 14 | #' @examples 15 | #' df <- get_hawkeye_stats_all(type = "TEST", competition = "WOMEN") 16 | 17 | get_hawkeye_stats_all <- function(type = "TEST", competition = "MEN") { 18 | 19 | if(type == "TEST" & competition == "MEN") { 20 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/mensTestHawkeyeStats.csv"), stringsAsFactors = F) 21 | } else if(type == "ODI" & competition == "MEN") { 22 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/mensODIHawkeyeStats.csv"), stringsAsFactors = F) 23 | } else if(type == "IPL" & competition == "MEN") { 24 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/mensIPLHawkeyeStats.csv"), stringsAsFactors = F) 25 | }else if(type == "TEST" & competition == "WOMEN") { 26 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/womensTestHawkeyeStats.csv"), stringsAsFactors = F) 27 | }else if(type == "ODI" & competition == "WOMEN") { 28 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/womensODIHawkeyeStats.csv"), stringsAsFactors = F) 29 | } else if(type == "IPL" & competition == "WOMEN") { 30 | dat <- read.csv(curl("https://raw.githubusercontent.com/alittlefitness/HawkeyeStats/main/womensIPLHawkeyeStats.csv"), stringsAsFactors = F) 31 | } 32 | 33 | return(dat) 34 | } 35 | -------------------------------------------------------------------------------- /R/hawkeye.R: -------------------------------------------------------------------------------- 1 | #' Hawkeye Stats for Tests, ODI or IPL matches 2 | #' 3 | #' @param id Id for match(es) to collect Hawkeye stats from. Invalid ids or ids with no Hawkeye data will result in an empty tibble 4 | #' or an error. Ids can be found using the get_completed_matches function or via the icc-cricket.com results page for Test and 5 | #' ODI matches and via the iplt20.com page for IPL matches. 6 | #' @return A tibble containing the Hawkey stats for the requested matches. The Hawkeye stats include the ball speed in metres/second, 7 | #' the x, y location when the ball impacts the pitch, the x, y location of the ball as it passes the stumps and the x, y location 8 | #' on the field after the batter has played their shot. x, y locations are presented in metres. Individual delivery Hawkeye stats 9 | #' may be missing if the technology failed. Ball tracking is not available for all matches and field x, y locations are not 10 | #' available for all matches. Some matches include field x, y locations for non scoring shots, while others only include 11 | #' field x, y locations for scoring shots. 12 | #' @import dplyr 13 | #' @import stringr 14 | #' @import tidyr 15 | #' @import jsonlite 16 | #' @import curl 17 | #' @importFrom stats na.omit 18 | #' @export 19 | #' @examples 20 | #' df <- get_hawkeye_stats(id = 23469) 21 | 22 | get_hawkeye_stats <- function(id) { 23 | ID <- ID_1 <- ID_10 <- ID_11 <- ID_13 <- ID_14 <- ID_15 <- ID_16 <- ID_17 <- ID_18 <- ID_2 <- ID_3 <- 24 | ID_4 <- ID_5 <- ID_7 <- ID_8 <- ID_9 <- bowlingStyle <- delivery <- fullName <- matchId <- 25 | nonStriker <- rightArmedBowl <- rightHandedBat <- value <- . <- NULL # set the global variables to NULL 26 | 27 | working <- tibble(delivery = "") # create a working tibble 28 | players <- tibble(id = NULL) # create an empty players tibble 29 | 30 | for(i in id) { 31 | url <- paste0("https://cricketapi-icc.pulselive.com/fixtures/", i, "/uds/stats") # url string to collect Hawkeye stats 32 | player_url <- paste0("https://cricketapi-icc.pulselive.com/fixtures/", i, "/uds") # url string to collect player information 33 | 34 | initial <- try(fromJSON(url, simplifyVector = F), silent = T) # if url doesn't exist register the error but move on 35 | 36 | suppressWarnings( 37 | if(str_detect(initial, "error") == F) { 38 | if(length(initial$data) != 0) { 39 | content <- tibble(raw = initial$data) %>% 40 | unnest_longer(col = raw, values_to = "value", indices_to = "delivery") %>% 41 | mutate(matchId = i) 42 | 43 | working <- bind_rows(working, content) 44 | 45 | players_list <- fromJSON(player_url) 46 | team1 <- tibble(players_list$teams$players[[1]]) 47 | team2 <- tibble(players_list$teams$players[[2]]) 48 | 49 | players <- bind_rows(players, team1, team2) 50 | } 51 | } 52 | ) 53 | } 54 | 55 | try({ 56 | # turn the data into a tidy tibble 57 | working_hawkeye <- working %>% 58 | mutate(value = str_split(value, ",")) %>% 59 | unnest(value) %>% 60 | group_by(matchId, delivery) %>% 61 | mutate(ID = paste0("ID_", row_number())) %>% 62 | pivot_wider(names_from = ID, values_from = value) 63 | 64 | # determine whether the match has hawkeye stats available 65 | has_hawkeye <- working_hawkeye %>% 66 | filter(ID_5 > 0 | ID_17 > 0) %>% 67 | ungroup() %>% 68 | distinct(matchId) 69 | 70 | # filter the working tibble to only include matches with hawkeye stats available 71 | hawkeye <- working_hawkeye %>% 72 | filter(matchId %in% has_hawkeye$matchId) %>% 73 | mutate_at(vars(ID_2, ID_3, ID_4), ~ as.integer(.)) 74 | 75 | batter <- distinct(players) %>% 76 | select(ID_2 = id, batter = fullName, rightHandedBat) 77 | 78 | non_striker <- distinct(players) %>% 79 | select(ID_3 = id, nonStriker = fullName) 80 | 81 | bowler <- distinct(players) %>% 82 | select(ID_4 = id, bowler = fullName, rightArmedBowl, bowlingStyle) 83 | 84 | suppressMessages( 85 | # join the filtered tibble with batter, non-striker and bowler information and create a tidy tibble 86 | hawkeye_stats <- left_join(hawkeye, batter) %>% 87 | left_join(., non_striker) %>% 88 | left_join(., bowler) %>% 89 | select(matchId, delivery, ball = ID_1, batter, batterId = ID_2, rightHandedBat, nonStriker, nonStrikerId = ID_3, 90 | bowler, bowlerId = ID_4, rightArmedBowl, bowlingStyle, ballSpeed = ID_5, dismissalDetails = ID_7, runs = ID_8, 91 | batterRuns = ID_9, bowlerRuns = ID_10, extras = ID_11, pitchX = ID_14, pitchY = ID_13, stumpsX = ID_15, 92 | stumpsY = ID_16, fieldX = ID_17, fieldY = ID_18) 93 | ) 94 | }, silent = T) 95 | return(hawkeye_stats) 96 | } 97 | 98 | 99 | -------------------------------------------------------------------------------- /R/matches.R: -------------------------------------------------------------------------------- 1 | #' Tidy metadata from completed Test, ODI or IPL T20 matches 2 | #' 3 | #' @param type Type of match data to return: must be one of "TEST", "ODI" or "IPL". Default is "TEST". 4 | #' @param competition Men's or Women's competitions: must be one of "MEN" or "WOMEN". Default is "MEN". 5 | #' @return A tibble containing metadata for Test, ODI or IPL T20 matches. 6 | #' @import dplyr 7 | #' @import jsonlite 8 | #' @import curl 9 | #' @importFrom stats na.omit 10 | #' @importFrom stats setNames 11 | #' @export 12 | #' @examples 13 | #' df <- get_completed_matches(type = "TEST", competition = "MEN") 14 | #' df <- get_completed_matches(type = "ODI", competition = "WOMEN") 15 | #' df <- get_completed_matches(type = "IPL", competition = "MEN") 16 | 17 | get_completed_matches <- function(type = "TEST", competition = "MEN") { 18 | value <- NULL 19 | comp <- if_else(competition == "MEN", "b%2Cm", "w") # used to set the url for Tests and ODI 20 | 21 | if(type %in% c("TEST", "ODI") & competition %in% c("MEN", "WOMEN")) { # Test and ODI matches use the ICC page to access the json format data 22 | initial_url <- paste0("https://cricketapi-icc.pulselive.com/fixtures?matchTypes=", 23 | type, "&tournamentTypes=I%2CWI&teamTypes=", comp, "&matchStates=C&page=0&pageSize=300&sort=desc") 24 | 25 | initial <- fromJSON(initial_url) 26 | # find how many pages of completed matches exist 27 | page_info <- tibble(value = initial$pageInfo$numPages) %>% 28 | distinct() %>% 29 | mutate(value = value - 1) 30 | 31 | # create the tibble from the 1st page 32 | suppressMessages( 33 | match_info <- setNames(tibble(initial$content[c("timestamp", "label", "tournamentLabel")], 34 | initial$content$scheduleEntry["matchType"], 35 | initial$content$tournamentId[c("id", "name")], 36 | initial$content$scheduleEntry$matchId["id"], 37 | initial$content$scheduleEntry$venue[c("fullName", "city", "country")], 38 | initial$content$scheduleEntry$matchStatus["text"], 39 | initial$content$scheduleEntry$team1$team[c("fullName")], 40 | initial$content$scheduleEntry$team2$team[c("fullName")], .name_repair = "universal"), 41 | c("timestamp", "label", "tournamentLabel", "matchType", 42 | "tournamentId", "tournamentName", "matchId", 43 | "venueFullName", "venueCity", "venueCountry", 44 | "resultText", "team1FullName", "team2FullName")) 45 | ) 46 | 47 | # loop through remaining pages and bind the tibbles together 48 | suppressMessages( 49 | try( 50 | for(i in 1:page_info$value) { 51 | page_info_url <- paste0("https://cricketapi-icc.pulselive.com/fixtures?matchTypes=", 52 | type, "&tournamentTypes=I%2CWI&teamTypes=", comp, "&matchStates=C&page=", 53 | i, "&pageSize=300&sort=desc") 54 | dat <- fromJSON(page_info_url) 55 | content <- setNames(tibble(dat$content[c("timestamp", "label", "tournamentLabel")], 56 | dat$content$scheduleEntry["matchType"], 57 | dat$content$tournamentId[c("id", "name")], 58 | dat$content$scheduleEntry$matchId["id"], 59 | dat$content$scheduleEntry$venue[c("fullName", "city", "country")], 60 | dat$content$scheduleEntry$matchStatus["text"], 61 | dat$content$scheduleEntry$team1$team[c("fullName")], 62 | dat$content$scheduleEntry$team2$team[c("fullName")], .name_repair = "universal"), 63 | c("timestamp", "label", "tournamentLabel", "matchType", 64 | "tournamentId", "tournamentName", "matchId", 65 | "venueFullName", "venueCity", "venueCountry", 66 | "resultText", "team1FullName", "team2FullName")) 67 | 68 | match_info <- bind_rows(match_info, content) 69 | }, silent = T) 70 | ) 71 | return(match_info) 72 | } else if(type == "IPL" & competition == "MEN") { # IPL matches use the BCCI page to acces the json format data 73 | 74 | ipl_season_ids <- c(22399, 18790, 10192, 7749, 5815, 3957, 2785, 2374, 605, 1) # Season ids for IPL data pages 75 | working <- tibble(timestamp = "") # create working tibble to bind to in the loop 76 | 77 | # loop through the season ids and bind data into a single tibble 78 | for(i in ipl_season_ids){ 79 | 80 | initial_url <- paste0("https://cricketapi.platform.iplt20.com//fixtures?tournamentIds=", 81 | i, "&pageSize=100") 82 | dat <- fromJSON(initial_url) 83 | suppressMessages( 84 | content <- setNames(tibble(dat$content[c("timestamp", "label", "tournamentLabel")], 85 | dat$content$scheduleEntry["matchType"], 86 | dat$content$tournamentId[c("id", "name")], 87 | dat$content$scheduleEntry$matchId["id"], 88 | dat$content$scheduleEntry$venue[c("fullName", "city", "country")], 89 | dat$content$scheduleEntry$matchStatus["text"], 90 | dat$content$scheduleEntry$team1$team[c("fullName")], 91 | dat$content$scheduleEntry$team2$team[c("fullName")], .name_repair = "universal"), 92 | c("timestamp", "label", "tournamentLabel", "matchType", 93 | "tournamentId", "tournamentName", "matchId", 94 | "venueFullName", "venueCity", "venueCountry", 95 | "resultText", "team1FullName", "team2FullName")) 96 | ) 97 | working <- bind_rows(working, content) 98 | } 99 | 100 | match_info <- working %>% 101 | na.omit() 102 | return(match_info) 103 | 104 | } else if(type == "IPL" & competition == "WOMEN") { 105 | 106 | ipl_season_ids <- c(21511, 11365, 8337) # Season ids for Women's IPL data pages 107 | working <- tibble(timestamp = "") # create working tibble to bind to in the loop 108 | 109 | # loop through the season ids and bind data into a single tibble 110 | for(i in ipl_season_ids){ 111 | 112 | initial_url <- paste0("https://cricketapi.platform.iplt20.com//fixtures?tournamentIds=", 113 | i, "&pageSize=100") 114 | dat <- fromJSON(initial_url) 115 | suppressMessages( 116 | content <- setNames(tibble(dat$content[c("timestamp", "label", "tournamentLabel")], 117 | dat$content$scheduleEntry["matchType"], 118 | dat$content$tournamentId[c("id", "name")], 119 | dat$content$scheduleEntry$matchId["id"], 120 | dat$content$scheduleEntry$venue[c("fullName", "city", "country")], 121 | dat$content$scheduleEntry$matchStatus["text"], 122 | dat$content$scheduleEntry$team1$team[c("fullName")], 123 | dat$content$scheduleEntry$team2$team[c("fullName")], .name_repair = "universal"), 124 | c("timestamp", "label", "tournamentLabel", "matchType", 125 | "tournamentId", "tournamentName", "matchId", 126 | "venueFullName", "venueCity", "venueCountry", 127 | "resultText", "team1FullName", "team2FullName")) 128 | ) 129 | working <- bind_rows(working, content) 130 | } 131 | 132 | match_info <- working %>% 133 | na.omit() 134 | return(match_info) 135 | } 136 | } 137 | 138 | --------------------------------------------------------------------------------