I have a twitter developer account with access to the v2 API and am trying to count the number of tweets posted by certain organisations (replaced it with POTUS in the example code) over the past years. However, I can only count the tweets from the last month. If I want to see the months prior to that I need to manually add the next_token to the script. This would be very time-consuming. Instead, I would like to have a script with automatic pagination.
My knowledge is very rudimentary and I don't understand how others have fixed this issue. I know I should write some kind of loop, but this is above my head.
library(httr)
bearer_token = ""
headers = c(
`Authorization` = sprintf('Bearer %s', bearer_token)
)
params = list(
`query` = 'from:POTUS',
`start_time` = '2017-01-01T00:00:00Z',
`end_time` = '2022-01-01T00:00:00Z',
`granularity` = day
)
response <- httr::GET(url = 'https://api.twitter.com/2/tweets/counts/all', httr::add_headers(.headers=headers), query = params)
body <-
content(
response,
as = 'parsed',
type = 'application/json',
simplifyDataFrame = TRUE
)
View(body$data)
sum(body$data$tweet_count)