| Package: gathertweet | Package: gathertweet | ||||
| Title: Gather Tweets from the Command Line with rtweet | Title: Gather Tweets from the Command Line with rtweet | ||||
| Version: 0.0.0.9000 | |||||
| Version: 0.0.1.9000 | |||||
| Authors@R: | Authors@R: | ||||
| person(given = "Garrick", | person(given = "Garrick", | ||||
| family = "Aden-Buie", | family = "Aden-Buie", |
| export("%>%") | export("%>%") | ||||
| export(backup_tweets) | export(backup_tweets) | ||||
| export(gathertweet_favorites) | |||||
| export(gathertweet_search) | export(gathertweet_search) | ||||
| export(gathertweet_simplify) | export(gathertweet_simplify) | ||||
| export(gathertweet_timeline) | export(gathertweet_timeline) |
| ) { | ) { | ||||
| log_info("Searching for \"{paste0(terms, collapse = '\", \"')}\"") | log_info("Searching for \"{paste0(terms, collapse = '\", \"')}\"") | ||||
| since_id <- if (is.null(max_id)) { | |||||
| if (since_id == "last") { | |||||
| last_seen_tweet(file = file) | |||||
| } else if (since_id == "none") { | |||||
| NULL | |||||
| } else since_id | |||||
| } | |||||
| if (!is.null(since_id)) log_info("Tweets from {since_id}") | |||||
| if (!is.null(max_id)) log_info("Tweets up to {max_id}") | |||||
| since_id <- set_since_id(since_id, max_id, file) | |||||
| tweets <- lapply( | tweets <- lapply( | ||||
| terms, | terms, | ||||
| ) | ) | ||||
| ) | ) | ||||
| if (isTRUE(`no-parse`)) { | if (isTRUE(`no-parse`)) { | ||||
| log_info("Saving un-parsed tweets in {file}") | log_info("Saving un-parsed tweets in {file}") | ||||
| saveRDS(tweets, file) | saveRDS(tweets, file) | ||||
| include_rts = isTRUE(include_rts) | include_rts = isTRUE(include_rts) | ||||
| ) | ) | ||||
| if (!nrow(tweets)) { | |||||
| log_fatal("No new tweets.") | |||||
| } | |||||
| tweets <- tweets[!duplicated(tweets$status_id), ] | |||||
| tweets <- tweets[order(tweets$status_id), ] | |||||
| log_info("Gathered {nrow(tweets)} tweets from {length(users)} users") | |||||
| tweets <- save_tweets(tweets, file) | |||||
| log_info("Total of {nrow(tweets)} tweets in {file}") | |||||
| tweets | |||||
| } | |||||
| #' @export | |||||
| gathertweet_favorites <- function( | |||||
| users, | |||||
| file = "tweets.rds", | |||||
| n = 3000, | |||||
| max_id = NULL, | |||||
| since_id = NULL, | |||||
| `no-parse` = FALSE, | |||||
| token = NULL, | |||||
| ... | |||||
| ) { | |||||
| log_info("Gathering tweets favorited by {collapse(users)}") | |||||
| since_id <- set_since_id(since_id, max_id, file) | |||||
| n <- as.integer(n) | |||||
| if (n > 3000) { | |||||
| log_warn("Twitter API for favorites/list returns a maximum of 3000 tweets per user") | |||||
| n <- 3000 | |||||
| } | |||||
| tweets <- rtweet::get_favorites( | |||||
| user = users, | |||||
| n = n, | |||||
| max_id = max_id, | |||||
| since_id = since_id, | |||||
| parse = isFALSE(`no-parse`), | |||||
| token = token | |||||
| ) | |||||
| if (!nrow(tweets)) { | |||||
| log_fatal("No new tweets.") | |||||
| } | |||||
| tweets <- tweets[!duplicated(tweets$status_id), ] | tweets <- tweets[!duplicated(tweets$status_id), ] | ||||
| tweets <- tweets[order(tweets$status_id), ] | tweets <- tweets[order(tweets$status_id), ] | ||||
| } | } | ||||
| isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x | isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x | ||||
| set_since_id <- function(since_id = NULL, max_id = NULL, file = NULL) { | |||||
| since_id <- if (is.null(max_id)) { | |||||
| if (since_id == "last") { | |||||
| if (is.null(file)) { | |||||
| log_fatal("`file` must be provided for since_id = \"last\"") | |||||
| } | |||||
| last_seen_tweet(file = file) | |||||
| } else if (since_id == "none") { | |||||
| NULL | |||||
| } else since_id | |||||
| } | |||||
| if (!is.null(since_id)) log_info("Tweets from {since_id}") | |||||
| if (!is.null(max_id)) log_info("Tweets up to {max_id}") | |||||
| since_id | |||||
| } |
| Usage: | Usage: | ||||
| gathertweet search [--file=<file>] [options] [--] <terms>... | gathertweet search [--file=<file>] [options] [--] <terms>... | ||||
| gathertweet timeline [options] [--] <users>... | |||||
| gathertweet update [--file=<file> --and-simplify --polite --debug-args --token=<token> --backup --backup-dir=<dir>] | gathertweet update [--file=<file> --and-simplify --polite --debug-args --token=<token> --backup --backup-dir=<dir>] | ||||
| gathertweet timeline [options] [--] <users>... | |||||
| gathertweet favorites [options] [--] <users>... | |||||
| gathertweet simplify [--file=<file> --output=<output> --debug-args --polite] [<fields>...] | gathertweet simplify [--file=<file> --output=<output> --debug-args --polite] [<fields>...] | ||||
| Options: | Options: | ||||
| (default is to exclude RTs) | (default is to exclude RTs) | ||||
| --max_id <max_id> Return tweets with an ID less (older) than or equal to | --max_id <max_id> Return tweets with an ID less (older) than or equal to | ||||
| timeline: | |||||
| timeline and favorites: | |||||
| <users> A list of users as user names, IDs, or a mixture of both, | <users> A list of users as user names, IDs, or a mixture of both, | ||||
| separated by spaces. | separated by spaces. | ||||
| timeline: | |||||
| --home If included, returns home-timeline instead of user-timeline. | --home If included, returns home-timeline instead of user-timeline. | ||||
| simplify: | simplify: | ||||
| collapse <- function(..., sep = ", ") paste(..., collapse = sep) | collapse <- function(..., sep = ", ") paste(..., collapse = sep) | ||||
| # Which action was called? | # Which action was called? | ||||
| valid_actions <- c("search", "update", "simplify", "timeline") | |||||
| valid_actions <- c("search", "update", "simplify", "timeline", "favorites") | |||||
| action <- names(Filter(isTRUE, args[valid_actions])) | action <- names(Filter(isTRUE, args[valid_actions])) | ||||
| if (!length(action)) { | if (!length(action)) { | ||||
| log_fatal("Please specify a valid action: {collapse(valid_actions)}") | log_fatal("Please specify a valid action: {collapse(valid_actions)}") | ||||
| } | } | ||||
| do.call("gathertweet_timeline", args) | do.call("gathertweet_timeline", args) | ||||
| # Favorites ---- | |||||
| } else if (isTRUE(args$favorites)) { | |||||
| if (!length(args$users)) { | |||||
| stop("Please provide a list of users as user names, user IDs, ", | |||||
| "or a mixture of both.") | |||||
| } | |||||
| do.call("gathertweet_favorites", args) | |||||
| } | } | ||||