File: lists_memberships.R

package info (click to toggle)
r-cran-rtweet 2.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,068 kB
  • sloc: sh: 13; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,857 bytes parent folder | download
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
#' Get Twitter list memberships (lists containing a given user)
#'
#' Due to deleted or removed lists, the returned number of memberships
#' is often less than the provided n value. This is a reflection of the API and
#' not a unique quirk of rtweet.
#'
#' @inheritParams TWIT_paginate_cursor
#' @inheritParams get_timeline
#' @param filter_to_owned_lists When `TRUE`, will return only lists that
#'   authenticating user owns.
#' @param previous_cursor `r lifecycle::badge("deprecated")` Please use
#'   `cursor` instead.
#' @references <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/get-lists-memberships>
#' @export
#' @seealso [`rtweet-deprecated`]
lists_memberships <- function(user = NULL,
                              n = 200,
                              cursor = "-1",
                              filter_to_owned_lists = FALSE,
                              token = NULL,
                              parse = TRUE,
                              retryonratelimit = NULL,
                              verbose = TRUE,
                              previous_cursor = NULL) {
  params <- list()
  if (!is.null(user)) {
    params[[user_type(user)]] <- user
  }
  if (isTRUE(filter_to_owned_lists)) {
    params$filter_to_owned_lists <- TRUE
  }

  r <- TWIT_paginate_cursor(token, "/1.1/lists/memberships", params,
    n = n,
    cursor = cursor,
    retryonratelimit = retryonratelimit,
    verbose = verbose,
    page_size = if (n >= 1000) 1000 else n,
    get_id = function(x) x$lists$id_str
  )

  if (parse) {
    r <- parse_lists_list(r)
  }

  r
}

parse_lists_list <- function(x) {
  lists <- lapply(x, function(x) x$lists)
  dfs <- lapply(lists, wrangle_into_clean_data, type = "list")
  dfs <- lapply(dfs, tibble::as_tibble)
  df <- do.call(rbind, dfs)

  copy_cursor(df, x)
}