Skip to content

Commit

Permalink
Merge pull request #16 from py-b/since-until
Browse files Browse the repository at this point in the history
add since and until (lichess)
  • Loading branch information
JaseZiv committed Aug 20, 2023
2 parents 7d6633c + aa40853 commit 04879a3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: chessR
Type: Package
Title: Functions to Extract, Clean and Analyse Online Chess Game Data
Version: 1.5.2
Version: 1.5.3
Authors@R: c(person("Jason Zivkovic", email = "jase.ziv83@gmail.com",
role = c("aut", "cre")),
person("Jonathan", "Carroll",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# chessR 1.5.3

Feature: `get_raw_lichess()` can now be filtered by date with the parameters
`since` and `until` (@py-b #16)

***

# chessR 1.5.2

CRAN suggestions for functions to fail gracefully.
Expand Down
35 changes: 30 additions & 5 deletions R/get_raw_lichess.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#' Lichess data as a data frame
#'
#' @param player_names A vector of a valid username or usernames from Lichess
#' @param since Games played since this date. An object of class `Date` or a
#' string of the form `"yyyy-mm-dd"`. If `NULL`, defaults to account creation
#' date.
#' @param until Games played until this date. An object of class `Date` or a
#' string of the form `"yyyy-mm-dd"`. If `NULL`, defaults to now.
#'
#' @return a dataframe of lichess data
#'
Expand All @@ -15,16 +20,34 @@
#' @examples
#' \dontrun{
#' georges_data <- get_raw_lichess(player_names = "Georges")
#' the_knife_data <- get_raw_lichess("the_knife", since = "2023-08-13", until = "2023-08-14")
#' }
get_raw_lichess <- function(player_names) {
get_raw_lichess <- function(player_names, since = NULL, until = NULL) {

get_file <- function(player_name) {

# cat("Extracting ", player_name, " games. Please wait\n")

since_query <- ""
if (!is.null(since)) {
# API parameter since awaits number of milliseconds since 1970-01-01
since_integer <- as.integer(as.Date(since)) * 86400 * 1000
since_query <- paste0("&since=", since_integer)
}

until_query <- ""
if (!is.null(until)) {
# API parameter until awaits number of milliseconds since 1970-01-01
until_integer <- ((as.integer(as.Date(until)) + 1) * 86400 * 1000) - 1
until_query <- paste0("&until=", until_integer)
}

# download the tmp file
tmp <- tempfile()
curl::curl_download(paste0("https://lichess.org/api/games/user/", player_name, "?evals=true&clocks=true&opening=true"), tmp)
curl::curl_download(
paste0("https://lichess.org/api/games/user/", player_name, "?evals=true&clocks=true&opening=true", since_query, until_query),
tmp
)
# read in the file
read_in_file <- readLines(tmp)
# cleaning steps of the file
Expand Down Expand Up @@ -78,10 +101,12 @@ get_raw_lichess <- function(player_names) {
purrr::map_df(create_games_df)
# apply the user's names
output$Username <- each_player
# filter out games where the variant is 'From Position'
output <- output %>% dplyr::filter(.data$Variant != "From Position")
if (nrow(output)) {
# filter out games where the variant is 'From Position'
output <- output %>% dplyr::filter(.data$Variant != "From Position")
final_output <- dplyr::bind_rows(final_output, output)
}

final_output <- dplyr::bind_rows(final_output, output)
}
return(final_output)
}
10 changes: 9 additions & 1 deletion man/get_raw_lichess.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-chessR.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ test_that("get_raw_lichess() works", {
lichess_game_data <- get_raw_lichess("JaseZiv")
expect_type(lichess_game_data, "list")
expect_true(nrow(lichess_game_data) != 0)
# tests for date parameters
lichess_game_data <- get_raw_lichess("JaseZiv", since = "2020-11-01", until = "2020-11-03")
expect_type(lichess_game_data, "list")
expect_true(nrow(lichess_game_data) == 13)
expect_true(all(lichess_game_data$Date %in% paste0("2020.11.0", 1:3)))
# no games for the chosen dates
lichess_game_data <- get_raw_lichess("JaseZiv", until = "2019-01-01")
expect_type(lichess_game_data, "list")
expect_true(nrow(lichess_game_data) == 0)
})


Expand Down

0 comments on commit 04879a3

Please sign in to comment.