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
|
#' Manage Twitter lists
#'
#' Create, add users, and destroy Twitter lists. `r lifecycle::badge("deprecated")`
#'
#' @inheritParams lookup_users
#' @param users Character vectors of users to be added to list.
#' @param name Name of new list to create.
#' @param description Optional, description of list (single character string).
#' @param private Logical indicating whether created list should be private.
#' Defaults to false, meaning the list would be public. Not applicable if list
#' already exists.
#' @param destroy Logical indicating whether to delete a list. Either `list_id` or
#' `slug` must be provided if `destroy = TRUE`.
#' @param list_id Optional, numeric ID of list.
#' @param slug Optional, list slug.
#' @return Response object from HTTP request.
#' @seealso [`rtweet-deprecated`]
#' @references
#' Create: <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-create>
#' Destroy: <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-destroy>
#' Add users: <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-members-create>,
#' <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-members-create_all>
#' Remove users: <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-members-destroy>,
#' <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/create-manage-lists/api-reference/post-lists-members-destroy_all>
post_list <- function(users = NULL,
name = NULL,
description = NULL,
private = FALSE,
destroy = FALSE,
list_id = NULL,
slug = NULL,
token = NULL) {
## destroy list
if (destroy && is.null(users)) {
return(post_list_destroy(list_id, slug, token))
}
## drop users from list
if (destroy) {
return(post_list_destroy_all(users, list_id, slug, token))
}
## create list
if (is.null(list_id) && is.null(slug)) {
r <- post_list_create(name, description, private, token)
tl <- from_js(r)
list_id <- tl$id_str
}
## add users to list one at a time
#post_list_create_all(users, list_id, slug, token)
r <- list()
for (i in seq_along(users)) {
r[[length(r) + 1]] <- post_list_add_one(users[i], list_id = list_id, slug = slug, token = token)
}
invisible(r)
}
post_list_create <- function(name,
description = NULL,
private = FALSE,
token = NULL) {
stopifnot(is.atomic(name) && !is.null(name), length(name) == 1,
is_logical(private))
if (private) {
mode <- "private"
} else {
mode <- "public"
}
params <- list(
name = name,
mode = mode,
description = description
)
TWIT_post(token, "/1.1/lists/create", params)
}
post_list_add_one <- function(user,
list_id = NULL,
slug = NULL,
token = NULL) {
## check and reformat users
stopifnot(is.character(user))
if (length(user) > 1) {
warning("Can only add 1 users at a time via this method. Adding user[]...",
call. = FALSE)
user <- user[1]
}
params <- my_list_params(token,
users = user,
list_id = list_id,
slug = slug
)
TWIT_post(token, "/1.1/lists/members/create", params)
}
post_list_destroy <- function(list_id = NULL,
slug = NULL,
token = NULL) {
params <- my_list_params(token,
list_id = list_id,
slug = slug
)
TWIT_post(token, "/1.1/lists/destroy", params)
}
post_list_create_all <- function(users,
list_id = NULL,
slug = NULL,
token = NULL) {
stopifnot(is.character(users))
if (length(users) > 100) {
warning("Can only add 100 users at a time. Adding users[1:100]...",
call. = FALSE)
users <- users[1:100]
}
params <- my_list_params(token,
users = users,
list_id = list_id,
slug = slug
)
TWIT_post(token, "/1.1/lists/members/create_all", params)
}
post_list_destroy_all <- function(users,
list_id = NULL,
slug = NULL,
token = NULL) {
stopifnot(is.character(users))
if (length(users) > 100) {
warning("Can only drop 100 users at a time. Dropping users[1:100]...",
call. = FALSE)
users <- users[1:100]
}
params <- my_list_params(token,
users = users,
list_id = list_id,
slug = slug
)
TWIT_post(token, "/1.1/lists/members/destroy_all", params = params)
}
my_list_params <- function(token, slug = NULL, list_id = NULL, ..., users = NULL) {
params <- list(...)
if (!is.null(list_id) && !is.null(slug)) {
stopifnot(is.atomic(list_id), length(list_id) == 1)
params$list_id <- list_id
} else if (is.null(slug) && !is.null(list_id)) {
stopifnot(is.atomic(slug), length(slug) == 1)
params$slug <- slug
params$owner_screen_name = api_screen_name(token)
} else {
abort("Must supply exactly one of `list_id` or `slug` to identify a list")
}
if (!is.null(users)) {
params[[user_type(users, "users")]] <- paste0(users, collapse = ",")
}
params
}
|