File: user_by_username.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 (52 lines) | stat: -rw-r--r-- 2,185 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
#' Search users by username
#'
#' Looks up users by their username.
#' @inheritParams filtered_stream
#' @param ... Other arguments passed to the API.
#' @param verbose A logical value to provide more information about paginated queries.
#' @param username A user name string or up to 100.
#' @export
#' @references <https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username>
#' <https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by>
#' @seealso [user_search()]
#' @examples
#' if (FALSE) {
#'   user_by_username("rOpenSci")
#'   user_by_username(c("Bioconductor", "R_Contributors"))
#' }
user_by_username <- function(username, expansions = NULL, fields = NULL, ...,
                        token = NULL, parse = TRUE, verbose = FALSE) {

  expansions <- check_expansions(arg_def(expansions, "pinned_tweet_id"),
                                 "pinned_tweet_id")
  fields <- check_fields(arg_def(fields, set_fields(media = NULL,
                                                    poll = NULL,
                                                    place = NULL)),
                         metrics = NULL, place = NULL, poll = NULL, media = NULL)
  expansions_for_fields(expansions, fields)
  if (!is_logical(verbose)) {
    abort("`verbose` must be either `TRUE` or `FALSE`.")
  }
  parsing(parse, expansions, fields)
  if (length(username) > 100 || !is_user_id(username) && length(username) == 0) {
    abort("Please introduce less than 100 valid user ids.")
  }
  if (length(username) > 1) {
    data <- c(list(usernames = username, expansions = expansions), fields, ...)
    url <- "users/by"
  } else {
    data <- c(list(expansions = expansions), fields, ...)
    url <- paste0("users/by/username/", username)
  }
  data <- unlist(prepare_params(data), recursive = FALSE)
  data <- data[data != ""]

  # Rates from the website app and user limits
  req_archive <- endpoint_v2(url, 900/(60*15), c("tweet.read", "users.read"))
  req_final <- httr2::req_url_query(req_archive, !!!data)
  p <- pagination(req_final, 1, 1, verbose)
  if (!parse) {
    return(p)
  }
  parse(p, expansions, fields)
}