File: search_users.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-- 1,518 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 for users
#'
#' Search for Twitter users. The Twitter API limits the results to at most
#' 1,000 users.  `r lifecycle::badge("deprecated")`
#'
#' @inheritParams TWIT_paginate_max_id
#' @inheritParams stream
#' @param q As string providing the search query. Try searching by interest,
#'   full name, company name, or location. Exact match searches are not
#'   supported.
#' @return Data frame with one row for each matching user.
#' @family users
#' @seealso [user_search()], [`rtweet-deprecated`]
#' @export
#' @references <https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-users-search>
search_users <- function(q, n = 100,
                         parse = TRUE,
                         token = NULL,
                         verbose = TRUE) {

  stopifnot(is_n(n), is.atomic(q) && !is.null(q))
  if (n > 1000) {
    abort("`n` must be <= 1,000 (the maximum allowed by Twitter)")
  }

  pages <- ceiling(n / 20)
  results <- vector("list", pages)

  if (verbose) {
    pb <- progress::progress_bar$new(
      format = "Searching for users :bar",
      total = pages
    )
    withr::defer(pb$terminate())
  }

  params <- list(q = q, count = 20)
  for (i in seq_len(pages)) {
    if (verbose) {
      pb$tick()
    }
    params$page <- i
    results[[i]] <- TWIT_get(token, "/1.1/users/search", params)
  }

  if (parse) {
    results <- users_with_tweets(results)
    results$created_at <- format_date(results$created_at)
  }

  results
}