Skip to content

Commit

Permalink
Merge pull request #78 from schochastics/paginate_results
Browse files Browse the repository at this point in the history
Paginate results (fix #70 and fix #43)
  • Loading branch information
schochastics committed Nov 16, 2022
2 parents ffcf1ab + 6678df4 commit c184a4c
Show file tree
Hide file tree
Showing 19 changed files with 1,468 additions and 75 deletions.
94 changes: 70 additions & 24 deletions R/accounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,24 @@ get_account_statuses <- function(id,instance = NULL, token = NULL, anonymous = F
#' @param max_id character, Return results older than this id
#' @param since_id character, Return results newer than this id
#' @param limit integer, maximum number of results to return. Defaults to 40.
#' @details this functions needs a user level auth token
#' @param retryonratelimit If TRUE, and a rate limit is exhausted, will wait until it refreshes. Most Mastodon rate limits refresh every 5 minutes. If FALSE, and the rate limit is exceeded, the function will terminate early with a warning; you'll still get back all results received up to that point.
#' @inheritParams auth_setup
#' @details this functions needs a user level auth token. If limit>40, automatic pagination is used. You may get more results than requested.
#' @return tibble or list of followers
#' @examples
#' \dontrun{
#' get_account_followers("109302436954721982")
#' }
#' @export
get_account_followers <- function(id,max_id,since_id,limit = 40, token = NULL, parse = TRUE){
get_account_followers <- function(id,max_id,since_id,
limit = 40L,
token = NULL,
parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/accounts/",id,"/followers")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -83,21 +91,29 @@ get_account_followers <- function(id,max_id,since_id,limit = 40, token = NULL, p
}
process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_account))
parse = parse, FUN = v(parse_account),
n = n, page_size = 40L,
retryonratelimit = retryonratelimit,
verbose = verbose)
}
#' Get accounts a user follows
#' @inheritParams get_account_statuses
#' @inheritParams get_account_followers
#' @details this functions needs a user level auth token
#' @inheritParams auth_setup
#' @inherit get_account_followers details
#' @return tibble or list of accounts a user follows
#' @examples
#' \dontrun{
#' get_account_following("109302436954721982")
#' }
#' @export
get_account_following <- function(id,max_id,since_id,limit = 40, token = NULL, parse = TRUE){
get_account_following <- function(id,max_id,since_id,limit = 40L,
token = NULL, parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/accounts/",id,"/following")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -107,7 +123,9 @@ get_account_following <- function(id,max_id,since_id,limit = 40, token = NULL, p

process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_account))
parse = parse, FUN = v(parse_account),
n = n,page_size = 40L, retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get featured tags of a user
Expand Down Expand Up @@ -172,17 +190,22 @@ get_account_relationships <- function(ids,token = NULL, parse = TRUE){
#' Get bookmarks of user
#' @inheritParams get_account_statuses
#' @inheritParams get_account_followers
#' @inheritParams auth_setup
#' @param min_id character, Return results younger than this id
#' @details this functions needs a user level auth token
#' @inherit get_account_followers details
#' @return bookmarked statuses
#' @examples
#' \dontrun{
#' get_account_followers("109302436954721982")
#' }
#' @export
get_account_bookmarks <- function(max_id,since_id,min_id,limit = 40, token = NULL, parse = TRUE){
get_account_bookmarks <- function(max_id,since_id,min_id,limit = 40L,
token = NULL, parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/bookmarks")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -195,24 +218,31 @@ get_account_bookmarks <- function(max_id,since_id,min_id,limit = 40, token = NUL

process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_status))
parse = parse, FUN = v(parse_status),n = n,
page_size = 40L,retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get favourites of user
#' @inheritParams get_account_statuses
#' @inheritParams get_account_followers
#' @inheritParams auth_setup
#' @param min_id character, Return results younger than this id
#' @details this functions needs a user level auth token
#' @inherit get_account_followers details
#' @return tibble or list of favourited statuses
#' @examples
#' \dontrun{
#' # needs user level token
#' get_account_favourites()
#' }
#' @export
get_account_favourites <- function(max_id,min_id,limit = 40, token = NULL, parse = TRUE){
get_account_favourites <- function(max_id,min_id,limit = 40L,
token = NULL, parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/favourites")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -222,23 +252,30 @@ get_account_favourites <- function(max_id,min_id,limit = 40, token = NULL, parse

process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_status))
parse = parse, FUN = v(parse_status),n = n,
page_size = 40L,retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get blocks of user
#' @inheritParams get_account_statuses
#' @inheritParams get_account_followers
#' @details this functions needs a user level auth token
#' @inheritParams auth_setup
#' @inherit get_account_followers details
#' @return tibble or list of blocked users
#' @examples
#' \dontrun{
#' # needs user level token
#' get_account_blocks()
#' }
#' @export
get_account_blocks <- function(max_id,since_id,limit = 40, token = NULL, parse = TRUE){
get_account_blocks <- function(max_id,since_id,limit = 40L,
token = NULL, parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/blocks")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -248,23 +285,30 @@ get_account_blocks <- function(max_id,since_id,limit = 40, token = NULL, parse =

process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_account))
parse = parse, FUN = v(parse_account), n = n,
page_size = 40L,retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get mutes of user
#' @inheritParams get_account_statuses
#' @inheritParams get_account_followers
#' @details this functions needs a user level auth token
#' @inheritParams auth_setup
#' @inherit get_account_followers details
#' @return tibble or list of muted users
#' @examples
#' \dontrun{
#' # needs user level token
#' get_account_mutes()
#' }
#' @export
get_account_mutes <- function(max_id,since_id,limit = 40, token = NULL, parse = TRUE){
get_account_mutes <- function(max_id,since_id,limit = 40L,
token = NULL, parse = TRUE,
retryonratelimit = TRUE,
verbose = TRUE){
path <- paste0("api/v1/mutes")
params <- list(limit = limit)
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -274,5 +318,7 @@ get_account_mutes <- function(max_id,since_id,limit = 40, token = NULL, parse =

process_request(token = token,path = path,
params = params,
parse = parse, FUN = v(parse_account))
parse = parse, FUN = v(parse_account),
n = n, page_size = 40L, retryonratelimit = retryonratelimit,
verbose = verbose)
}
48 changes: 36 additions & 12 deletions R/timelines_statuses.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ get_poll <- function(id, instance = NULL, token = NULL, anonymous = FALSE, parse
#' @param since_id character, Return results newer than this id
#' @param min_id character, Return results immediately newer than this id
#' @param limit integer, Maximum number of results to return
#' @param retryonratelimit If TRUE, and a rate limit is exhausted, will wait until it refreshes. Most Mastodon rate limits refresh every 5 minutes. If FALSE, and the rate limit is exceeded, the function will terminate early with a warning; you'll still get back all results received up to that point.
#' @param verbose logical whether to display messages
#' @inheritParams post_toot
#' @inheritParams get_status
#' @return statuses
Expand All @@ -107,8 +109,12 @@ get_poll <- function(id, instance = NULL, token = NULL, anonymous = FALSE, parse
#' @references
#' https://docs.joinmastodon.org/methods/timelines/
get_timeline_public <- function(local = FALSE, remote = FALSE, only_media = FALSE,
max_id, since_id, min_id, limit = 20L, instance = NULL, token = NULL, anonymous = FALSE, parse = TRUE) {
params <- list(local = local, remote = remote, only_media = only_media, limit = limit)
max_id, since_id, min_id, limit = 20L,
instance = NULL, token = NULL, anonymous = FALSE, parse = TRUE,
retryonratelimit = TRUE,verbose = TRUE) {
params <- list(local = local, remote = remote,
only_media = only_media, limit = min(limit,40))
n <- limit
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -121,7 +127,10 @@ get_timeline_public <- function(local = FALSE, remote = FALSE, only_media = FALS
path = "/api/v1/timelines/public"

process_request(token = token,path = path,instance = instance,params = params,
anonymous = anonymous,parse = parse,FUN = v(parse_status))
anonymous = anonymous,parse = parse,FUN = v(parse_status),
n = n, page_size = 40L,
retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get hashtag timeline
Expand All @@ -138,8 +147,10 @@ get_timeline_public <- function(local = FALSE, remote = FALSE, only_media = FALS
#' }
get_timeline_hashtag <- function(hashtag = "rstats", local = FALSE, only_media = FALSE,
max_id, since_id, min_id, limit = 20L, instance = NULL,
token = NULL, anonymous = FALSE, parse = TRUE) {
params <- list(local = local, only_media = only_media, limit = limit)
token = NULL, anonymous = FALSE, parse = TRUE,
retryonratelimit = TRUE,verbose = TRUE) {
n <- limit
params <- list(local = local, only_media = only_media, limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -152,7 +163,9 @@ get_timeline_hashtag <- function(hashtag = "rstats", local = FALSE, only_media =
path <- paste0("/api/v1/timelines/tag/", gsub("^#+", "", hashtag))

process_request(token = token,path = path,instance = instance,params = params,
anonymous = anonymous,parse = parse,FUN = v(parse_status))
anonymous = anonymous,parse = parse,FUN = v(parse_status),
n = n, page_size = 40L, retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' Get home and list timelines
Expand All @@ -165,8 +178,10 @@ get_timeline_hashtag <- function(hashtag = "rstats", local = FALSE, only_media =
#' \dontrun{
#' get_timeline_home()
#' }
get_timeline_home <- function(local = FALSE, max_id, since_id, min_id, limit = 20L, token = NULL, parse = TRUE) {
params <- list(local = local, limit = limit)
get_timeline_home <- function(local = FALSE, max_id, since_id, min_id, limit = 20L,
token = NULL, parse = TRUE,retryonratelimit = TRUE,verbose = TRUE) {
n <- limit
params <- list(local = local, limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -179,7 +194,10 @@ get_timeline_home <- function(local = FALSE, max_id, since_id, min_id, limit = 2
path = "/api/v1/timelines/home"

process_request(token = token,path = path,params = params,
parse = parse,FUN = v(parse_status))
parse = parse,FUN = v(parse_status),
n = n, page_size = 40L,
retryonratelimit = retryonratelimit,
verbose = verbose)
}

#' @rdname get_timeline_home
Expand All @@ -188,8 +206,11 @@ get_timeline_home <- function(local = FALSE, max_id, since_id, min_id, limit = 2
#' \dontrun{
#' get_timeline_list("<listid>")
#' }
get_timeline_list <- function(list_id, max_id, since_id, min_id, limit = 20L, token = NULL, parse = TRUE) {
params <- list(limit = limit)
get_timeline_list <- function(list_id, max_id, since_id, min_id,
limit = 20L, token = NULL, parse = TRUE,
retryonratelimit = TRUE,verbose = TRUE) {
n <- limit
params <- list(limit = min(limit,40L))
if (!missing(max_id)) {
params$max_id <- max_id
}
Expand All @@ -202,5 +223,8 @@ get_timeline_list <- function(list_id, max_id, since_id, min_id, limit = 20L, to
path <- paste0("/api/v1/timelines/list/", list_id)

process_request(token = token,path = path,params = params,
parse = parse,FUN = v(parse_status))
parse = parse,FUN = v(parse_status),
n = n, page_size = 40L,
retryonratelimit = retryonratelimit,
verbose = verbose)
}

0 comments on commit c184a4c

Please sign in to comment.