diff --git a/NAMESPACE b/NAMESPACE index 49a5fd1..5db6618 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,6 @@ export(bind_tweet_jsons) export(bind_user_jsons) export(build_query) -export(build_user_query) export(get_all_tweets) export(get_bbox_tweets) export(get_country_tweets) diff --git a/R/build_queryv2.R b/R/build_queryv2.R index af0c9ea..ee01f78 100644 --- a/R/build_queryv2.R +++ b/R/build_queryv2.R @@ -82,7 +82,7 @@ build_query <- function(query, query <- paste(query, "is:retweet") } - if(isFALSE(is_retweet)) { + if(!isTRUE(is_retweet)) { query <- paste(query, "-is:retweet") } diff --git a/R/build_user_queryv2.R b/R/build_user_queryv2.R deleted file mode 100644 index 9474b6b..0000000 --- a/R/build_user_queryv2.R +++ /dev/null @@ -1,200 +0,0 @@ -#' Build tweet query -#' -#' Build tweet query according to targeted parameters, can then be input to main \code{\link{get_user_tweets}} function as query parameter. -#' -#' @param users string or character vector, user handles from which to collect data -#' @param is_retweet If `TRUE`, only retweets will be returned; if `FALSE` retweets will not be returned -#' @param is_reply If `TRUE`, only reply tweets will be returned -#' @param is_quote If `TRUE`, only quote tweets will be returned -#' @param place string, name of place e.g. "London" -#' @param country string, name of country as ISO alpha-2 code e.g. "GB" -#' @param point_radius numeric, a vector of two point coordinates latitude, longitude, and point radius distance (in miles) -#' @param bbox numeric, a vector of four bounding box coordinates from west longitude to north latitude -#' @param geo_query If `TRUE` user will be prompted to enter relevant information for bounding box or point radius geo buffers -#' @param remove_promoted If `TRUE`, tweets created for promotion only on ads.twitter.com are removed -#' @param has_hashtags If `TRUE`, only tweets containing hashtags will be returned -#' @param has_cashtags If `TRUE`, only tweets containing cashtags will be returned -#' @param has_links If `TRUE`, only tweets containing links and media will be returned -#' @param has_mentions If `TRUE`, only tweets containing mentions will be returned -#' @param has_media If `TRUE`, only tweets containing a recognized media object, such as a photo, GIF, or video, as determined by Twitter will be returned -#' @param has_images If `TRUE`, only tweets containing a recognized URL to an image will be returned -#' @param has_videos If `TRUE`, only tweets containing contain native Twitter videos, uploaded directly to Twitter will be returned -#' @param has_geo If `TRUE`, only tweets containing Tweet-specific geolocation data provided by the Twitter user will be returned -#' @param lang string, a single BCP 47 language identifier e.g. "fr" -#' -#' @return a query string -#' @export -#' -#' @examples -#' \dontrun{ -#' users <- c("cbarrie", "justin_ct_ho") -#' query <- build_user_query(users, is_retweet = F, has_media = T, lang = "en") -#' } -#' -#' @importFrom utils menu -#' - -build_user_query <- function(users, - is_retweet = NULL, - is_reply = FALSE, - is_quote = FALSE, - place = NULL, - country = NULL, - point_radius = NULL, - bbox = NULL, - geo_query = FALSE, - remove_promoted = FALSE, - has_hashtags = FALSE, - has_cashtags = FALSE, - has_links = FALSE, - has_mentions = FALSE, - has_media = FALSE, - has_images = FALSE, - has_videos = FALSE, - has_geo = FALSE, - lang= NULL) { - - for(i in seq_along(users)) { - query <- users[[i]] - - if (isTRUE(is_retweet) & isTRUE(is_reply)) { - stop("A tweet cannot be both a retweet and a reply") - } - - if (isTRUE(is_quote) & isTRUE(is_reply)) { - stop("A tweet cannot be both a quote tweet and a reply") - } - - if (isTRUE(point_radius) & isTRUE(bbox)) { - stop("Select either point radius or bounding box") - } - - if(isTRUE(is_retweet)) { - query <- paste(query, "is:retweet") - } - - if(isFALSE(is_retweet)) { - query <- paste(query, "-is:retweet") - } - - if(isTRUE(is_reply)) { - query <- paste(query, "is:reply") - } - - if(isTRUE(is_quote)) { - query <- paste(query, "is:quote") - } - - if(!is.null(place)) { - query <- paste(query, paste0("place:", place)) - } - - if(!is.null(country)) { - query <- paste(query, paste0("place_country:", country)) - } - - if(isTRUE(geo_query)) { - if(response <- menu(c("Point radius", "Bounding box"), title="Which geo buffer type type do you want?") ==1) { - x <- readline("What is longitude? ") - y <- readline("What is latitude? ") - z <- readline("What is radius? ") - - zn<- as.integer(z) - while(zn>25) { - cat("Radius must be less than 25 miles") - z <- readline("What is radius? ") - zn<- as.integer(z) - } - - z <- paste0(z, "mi") - - r <- paste(x,y,z) - query <- paste(query, paste0("point_radius:","[", r,"]")) - } - else if(response <- menu(c("Point radius", "Bounding box"), title="Which geo buffer type type do you want?") ==2) { - w <- readline("What is west longitude? ") - x <- readline("What is south latitude? ") - y <- readline("What is east longitude? ") - z <- readline("What is north latitude? ") - - z <- paste(w,x,y,z) - - query <- paste(query, paste0("bounding_box:","[", z,"]")) - } - - } - - if(!is.null(point_radius)) { - x <- point_radius[1] - y <- point_radius[2] - z <- point_radius[3] - - zn<- as.numeric(z) - while(zn>25) { - cat("Radius must be less than 25 miles") - z <- readline("Input new radius: ") - zn<- as.numeric(z) - } - - z <- paste0(z, "mi") - - r <- paste(x,y,z) - query <- paste(query, paste0("point_radius:","[", r,"]")) - } - - if(!is.null(bbox)) { - w <- bbox[1] - x <- bbox[2] - y <- bbox[3] - z <- bbox[4] - - z <- paste(w,x,y,z) - - query <- paste(query, paste0("bounding_box:","[", z,"]")) - } - - if(isTRUE(remove_promoted)) { - query <- paste(query, "-is:nullcast") - } - - if(isTRUE(has_hashtags)) { - query <- paste(query, "has:hashtags") - } - - if(isTRUE(has_cashtags)) { - query <- paste(query, "has:cashtags") - } - - if(isTRUE(has_links)) { - query <- paste(query, "has:links") - } - - if(isTRUE(has_mentions)) { - query <- paste(query, "has:mentions") - } - - if(isTRUE(has_media)) { - query <- paste(query, "has:media") - } - - if(isTRUE(has_images)) { - query <- paste(query, "has:images") - } - - if(isTRUE(has_videos)) { - query <- paste(query, "has:videos") - } - - if(isTRUE(has_geo)) { - query <- paste(query, "has:geo") - } - - if(!is.null(lang)) { - query <- paste(query, paste0("lang:", lang)) - - } - - users[[i]] <- paste(query) - } - return(users) -} \ No newline at end of file diff --git a/man/build_user_query.Rd b/man/build_user_query.Rd deleted file mode 100644 index 8eaf014..0000000 --- a/man/build_user_query.Rd +++ /dev/null @@ -1,80 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/build_user_queryv2.R -\name{build_user_query} -\alias{build_user_query} -\title{Build tweet query} -\usage{ -build_user_query( - users, - is_retweet = NULL, - is_reply = FALSE, - is_quote = FALSE, - place = NULL, - country = NULL, - point_radius = NULL, - bbox = NULL, - geo_query = FALSE, - remove_promoted = FALSE, - has_hashtags = FALSE, - has_cashtags = FALSE, - has_links = FALSE, - has_mentions = FALSE, - has_media = FALSE, - has_images = FALSE, - has_videos = FALSE, - has_geo = FALSE, - lang = NULL -) -} -\arguments{ -\item{users}{string or character vector, user handles from which to collect data} - -\item{is_retweet}{If \code{TRUE}, only retweets will be returned; if \code{FALSE} retweets will not be returned} - -\item{is_reply}{If \code{TRUE}, only reply tweets will be returned} - -\item{is_quote}{If \code{TRUE}, only quote tweets will be returned} - -\item{place}{string, name of place e.g. "London"} - -\item{country}{string, name of country as ISO alpha-2 code e.g. "GB"} - -\item{point_radius}{numeric, a vector of two point coordinates latitude, longitude, and point radius distance (in miles)} - -\item{bbox}{numeric, a vector of four bounding box coordinates from west longitude to north latitude} - -\item{geo_query}{If \code{TRUE} user will be prompted to enter relevant information for bounding box or point radius geo buffers} - -\item{remove_promoted}{If \code{TRUE}, tweets created for promotion only on ads.twitter.com are removed} - -\item{has_hashtags}{If \code{TRUE}, only tweets containing hashtags will be returned} - -\item{has_cashtags}{If \code{TRUE}, only tweets containing cashtags will be returned} - -\item{has_links}{If \code{TRUE}, only tweets containing links and media will be returned} - -\item{has_mentions}{If \code{TRUE}, only tweets containing mentions will be returned} - -\item{has_media}{If \code{TRUE}, only tweets containing a recognized media object, such as a photo, GIF, or video, as determined by Twitter will be returned} - -\item{has_images}{If \code{TRUE}, only tweets containing a recognized URL to an image will be returned} - -\item{has_videos}{If \code{TRUE}, only tweets containing contain native Twitter videos, uploaded directly to Twitter will be returned} - -\item{has_geo}{If \code{TRUE}, only tweets containing Tweet-specific geolocation data provided by the Twitter user will be returned} - -\item{lang}{string, a single BCP 47 language identifier e.g. "fr"} -} -\value{ -a query string -} -\description{ -Build tweet query according to targeted parameters, can then be input to main \code{\link{get_user_tweets}} function as query parameter. -} -\examples{ -\dontrun{ -users <- c("cbarrie", "justin_ct_ho") -query <- build_user_query(users, is_retweet = F, has_media = T, lang = "en") -} - -} diff --git a/vignettes/academictwitteR-build.Rmd b/vignettes/academictwitteR-build.Rmd index 5f2227f..95bd5d2 100644 --- a/vignettes/academictwitteR-build.Rmd +++ b/vignettes/academictwitteR-build.Rmd @@ -302,33 +302,20 @@ query ``` -## Building user queries with `build_user_query()` +## Building user queries with `get_user_tweets()` What if we had a number of users in which we were interested but we weren't interested in all of their tweets? Say we wanted only to collect tweets that weren't retweets but contained images, and that were in the English language? -We can achieve this by building a query with `build_user_query` and passing the resulting character vector of users and parameters to the `get_user_tweets()` function as follows: - -```{r} - -users <- c("cbarrie", "justin_ct_ho") - -users_params <- - build_user_query(users, - is_retweet = F, - has_media = T, - lang = "en") - -users_params - -``` - - -We would then enter our new vector of users with added parameters to the `get_user_tweets()` function as follows: +We can achieve this by building a query with `get_user_tweets()`, the fuction will turn a character vector of users into a query and pass to the `get_all_tweets()` function along with other parameters: ```{r, eval=FALSE} +users <- c("cbarrie", "justin_ct_ho") tweets <- - get_user_tweets(users_params, + get_user_tweets(users, "2018-01-01T00:00:00Z", "2020-04-05T00:00:00Z", - bearer_token) + bearer_token, + is_retweet = FALSE, + has_media = TRUE, + lang = "en") ```