Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

[FR] Function to get tweet metrics #356

Open
beatrizmilz opened this issue Nov 20, 2022 · 1 comment
Open

[FR] Function to get tweet metrics #356

beatrizmilz opened this issue Nov 20, 2022 · 1 comment

Comments

@beatrizmilz
Copy link

beatrizmilz commented Nov 20, 2022

Describe the solution you'd like

Hi!
I did not find the metrics of the tweets I collected using this package (likes, retweets, quotes, etc.).

So I looked up the API documentation:
https://developer.twitter.com/en/docs/twitter-api/metrics

And did a simple function to get that info.

get_metrics <- function(id_tweet) {


  id_tweets <- id_tweet |> paste0(collapse = ",")
  
  content_response <- httr::GET(
    url = glue::glue(
      "https://api.twitter.com/2/tweets?ids={id_tweets}&tweet.fields=public_metrics&expansions=attachments.media_keys&media.fields=public_metrics"
    ),
    httr::add_headers(
      Authorization = glue::glue('Bearer {academictwitteR::get_bearer()}')
    )
  ) |> httr::content(simplifyDataFrame = TRUE)
  
  if (!is.null(content_response$data)) {
    content_response |>
      purrr::pluck("data") |>
      tibble::as_tibble() |>
      tidyr::unnest(cols = c(public_metrics, edit_history_tweet_ids))
  } else {
    content_response |>
      purrr::pluck(1) |>
      tibble::as_tibble()
    
  }
}

With this, I was able to get the metrics. But the functions in this package are more robust and deal with maximum requests, saving partial results and resuming after, pagination, and so on.

I don't know if I missed it in the docs, and the package already has a way to get tweet metrics. If that it's the case, can someone point me to that?
Otherwise, could the package have a function to do that? I can try to look at the code for get_user_profile() and create a similar function for this and create a PR if that can be helpful.

Thanks!

Anything else?

https://developer.twitter.com/en/docs/twitter-api/metrics

@cjbarrie
Copy link
Owner

cjbarrie commented Jan 5, 2023

Hi @beatrizmilz ,not sure I follow why you were unable to get tweet metrics. These still appear when I test. Better to supply reprex if you think there's an issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants