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

Commit 49480f6

Browse files
authored
Fix #338 (#340)
Allow `start_tweets` and `end_tweets` to be optional for `get_all_tweets` and `count_all_tweets` Also, make `count_all_tweets` inherit params from `get_all_tweets` to reduce replications.
1 parent 174c304 commit 49480f6

21 files changed

Lines changed: 66 additions & 69 deletions

R/count_all_tweets.R

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,9 @@
33
#' This function returns aggregate counts of tweets by query string or strings
44
#' between specified date ranges.
55
#'
6-
#' @param query string or character vector, search query or queries
7-
#' @param start_tweets string, starting date
8-
#' @param end_tweets string, ending date
9-
#' @param bearer_token string, bearer token
106
#' @param n integer, upper limit of tweet counts to be fetched (i.e., for 365 days n must be at least 365). Default is 100.
11-
#' @param file string, name of the resulting RDS file
12-
#' @param data_path string, if supplied, fetched data can be saved to the designated path as jsons
13-
#' @param export_query If `TRUE`, queries are exported to data_path
14-
#' @param bind_tweets If `TRUE`, tweets captured are bound into a data.frame for assignment
15-
#' @param granularity string, the granularity for the search counts results. Options are "day"; "hour"; "minute". Default is day.
16-
#' @param verbose If `FALSE`, query progress messages are suppressed
17-
#' @param ... arguments will be passed to `build_query()` function. See `?build_query()` for further information.
18-
#'
7+
#' @param granularity string, the granularity for the search counts results. Options are "day"; "hour"; "minute". Default is day.
8+
#' @inheritParams get_all_tweets
199
#' @return a data.frame
2010
#' @export
2111
#'
@@ -47,23 +37,26 @@ count_all_tweets <-
4737
granularity = "day",
4838
verbose = TRUE,
4939
...) {
50-
if (missing(start_tweets)) {
51-
stop("Start time must be specified.")
52-
}
53-
if (missing(end_tweets)) {
54-
stop("End time must be specified.")
55-
}
5640

5741
# Build query
5842
built_query <- build_query(query, ...)
5943

6044
# Building parameters for get_tweets()
6145
params <- list(
6246
"query" = built_query,
63-
"start_time" = start_tweets,
64-
"end_time" = end_tweets,
6547
"granularity" = granularity
66-
)
48+
)
49+
if (!missing(start_tweets)) {
50+
params$start_time <- start_tweets
51+
}
52+
if (!missing(end_tweets)) {
53+
params$end_time <- end_tweets
54+
}
55+
if ("start_time" %in% names(params) & "end_time" %in% names(params)) {
56+
## For backward compatibility with test cases
57+
params <- params[c("query", "start_time", "end_time", "granularity")]
58+
}
59+
6760
endpoint_url <- "https://api.twitter.com/2/tweets/counts/all"
6861
.vcat(verbose, "query: ", params[["query"]], "\n")
6962

R/get_all_tweets.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#' while user-level data will be returned as a series of JSONs beginning "users_".
1616
#'
1717
#' @param query string or character vector, search query or queries
18-
#' @param start_tweets string, starting date
19-
#' @param end_tweets string, ending date
18+
#' @param start_tweets string, starting date; default to now - 30 days
19+
#' @param end_tweets string, ending date; default to now - 30 seconds
2020
#' @param bearer_token string, bearer token
2121
#' @param n integer, upper limit of tweets to be fetched
2222
#' @param file string, name of the resulting RDS file
@@ -68,12 +68,6 @@ get_all_tweets <-
6868
context_annotations = FALSE,
6969
verbose = TRUE,
7070
...) {
71-
if (missing(start_tweets)) {
72-
stop("Start time must be specified.")
73-
}
74-
if (missing(end_tweets)) {
75-
stop("End time must be specified.")
76-
}
7771

7872
# Check file storage conditions
7973
check_data_path(data_path = data_path, file = file, bind_tweets = bind_tweets, verbose = verbose)
@@ -85,13 +79,21 @@ get_all_tweets <-
8579
params <- list(
8680
"query" = built_query,
8781
"max_results" = page_n,
88-
"start_time" = start_tweets,
89-
"end_time" = end_tweets,
9082
"tweet.fields" = "attachments,author_id,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,source,text,withheld",
9183
"user.fields" = "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld",
9284
"expansions" = "author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id",
9385
"place.fields" = "contained_within,country,country_code,full_name,geo,id,name,place_type"
9486
)
87+
if (!missing(start_tweets)) {
88+
params$start_time <- start_tweets
89+
}
90+
if (!missing(end_tweets)) {
91+
params$end_time <- end_tweets
92+
}
93+
if ("start_time" %in% names(params) & "end_time" %in% names(params)) {
94+
## For backward compatibility with test cases
95+
params <- params[c("query", "max_results", "start_time", "end_time", "tweet.fields", "user.fields", "expansions", "place.fields")]
96+
}
9597
endpoint_url <- "https://api.twitter.com/2/tweets/search/all"
9698

9799
if (context_annotations){

man/count_all_tweets.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_all_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_bbox_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_country_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_geo_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_image_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_lang_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_media_tweets.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)