R/tweet_gettr.R

Defines functions tweet_gettr

Documented in tweet_gettr

#' Tweet GettR
#'
#' This function lets you scrape the timeline of any twitter user.
#'
#' @param handle The handle of the twitter user's timeline you wish to scrape.
#' @param output The location of an output text file that you can re-use if you want a re-usable file of tweets. Defaults to a blank string, no output saved.
#' @param token Token. The token generated by setup_twitteR.
#' @param n Integer. Defaults to `3200`. The number of tweets to pull.
#' @param includeRts Boolean. Defaults to `FALSE`. Whether or not to include re-tweets.
#' @param includeReplies Boolean. Defaults to `FALSE`. Whether or not to include replies.
#' @param sentiments Dataframe. Dataframe containing words and their sentiments. Columns MUST be "word" and "sentiment"
#' @keywords Twitter, Scraping
#' @return Returns a properly-formatted list that you can run make_sentence on
#' @export
#' @examples
#' tweet_gettr("@realDonaldTrump", "./trump.txt")

tweet_gettr <- function(handle, token = NULL, output = "", n = 3200,
                        includeRts = FALSE, includeReplies=FALSE, sentiments = NULL) {
  # Make sure that this is an @
  if (substr(handle,1,1) == '@') {
    handle <- substr(handle, 2, stringr::str_length(handle))
  }

  # Scrape handle's timeline.
  #  Exits if the twitter api is not setup
  tweets_raw <- NULL
  if (is.null(token)) {
    message("Warning: Twitter api not set up, setting it up automatically")
    token <- setup_twitteR()
  }

  tweets_raw <- rtweet::get_timeline(handle, token = token, n = 3200)
  if (nrow(tweets_raw) == 0) {
    return("No Tweets Found")
  }
  tweets_raw <- as.data.frame(tweets_raw)
  tweets <- tweets_raw$text
  rt_filter <- TRUE
  reply_filter <- TRUE
  if (!includeRts) {
    rt_filter <- !(tweets_raw$is_retweet)
  }

  if (!includeReplies) {
    reply_filter <- is.na(tweets_raw$reply_to_screen_name)
  }
  tweets <- tweets[rt_filter & reply_filter]

  tweets <- stringr::str_replace_all(tweets, "\n", " newline ")

  if (length(tweets) > n) {
    tweets <- tweets[1:n]
  } else {
    message("Less tweets available than the desired amount")
  }

  # export the tweets here
  if (nchar(output) > 0) {
    fileConn <- file(output)
    writeLines(tweets, fileConn, sep = "\n")
    close(fileConn)
  }

  # if(is.null(sentiments)) {
  #   print("Using standard sentiments...")
  #   sentiments <- tidytext::get_sentiments("bing")
  # }

  # get pfp
  profile_pic_url <- tweets_raw[1, "profile_image_url"]
  username <- tweets_raw[1, "name"]

  return(generate_clean_data(tweets,list("pfp" = profile_pic_url,
                                         "username" = username,
                                         "handle"=paste0("@",handle))))
}
serrat839/package documentation built on May 29, 2020, 10:54 a.m.