library(academictwitteR)

get_bearer()   # See academictwitteR documentation for registration and bearer key creation

query <- build_query(
  query = c("#peakdistrict", "#PeakDistrictNationalPark", "#peakdistrictwalks", "Peak District", "Peak District National Park"),
  is_retweet = FALSE,
  remove_promoted = TRUE,
  lang = "en",
  exclude = c("recruitment", "recruiting", "@WhaleyChronicle", "@weatherwhaley", "#WeAreWorkingForYou", "@etsyUK", "@etsy","price", "#ukgetaway", "@etsy")
)

startdates <-  c("2019-05-25", "2020-05-23", "2021-05-29")
enddates <- c("2019-05-27", "2020-05-25", "2021-05-31")

tweetdir <- c()
datadir <- c()

for (i in 1:length(startdates)) {

tweetdir[i] <- paste("tweets", startdates[i], sep = "_")
  
datadir[i] <- paste("data", startdates[i], sep = "_")
dir.create(datadir[i])

get_all_tweets(
    query,
    start_tweets = paste(startdates[i], "T00:00:01Z", sep=""),
    end_tweets = paste(enddates[i], "T23:59:59Z", sep=""),
    data_path = tweetdir[i],
    bind_tweets = FALSE,
    n = 2000
  )


assign(tweetdir[i], bind_tweets(data_path = tweetdir[i], output_format = "tidy"))

# Save dataframes as objects and as xlsx-files
library(writexl)

setwd(datadir[i])

saveRDS(get(tweetdir[i]), paste(tweetdir[i], ".rds", sep=""))
write_xlsx(get(tweetdir[i]), paste(tweetdir[i],".xlsx", sep=""))

setwd(current_path)
}
