Переглянути джерело

Add gathertweet favorites

wrapper around rtweet::get_favorites()
master
Garrick Aden-Buie 7 роки тому
джерело
коміт
9af703b08f
4 змінених файлів з 81 додано та 15 видалено
  1. +1
    -1
      DESCRIPTION
  2. +1
    -0
      NAMESPACE
  3. +64
    -11
      R/gathertweet_actions.R
  4. +15
    -3
      inst/gathertweet.R

+ 1
- 1
DESCRIPTION Переглянути файл

@@ -1,6 +1,6 @@
Package: gathertweet
Title: Gather Tweets from the Command Line with rtweet
Version: 0.0.0.9000
Version: 0.0.1.9000
Authors@R:
person(given = "Garrick",
family = "Aden-Buie",

+ 1
- 0
NAMESPACE Переглянути файл

@@ -2,6 +2,7 @@

export("%>%")
export(backup_tweets)
export(gathertweet_favorites)
export(gathertweet_search)
export(gathertweet_simplify)
export(gathertweet_timeline)

+ 64
- 11
R/gathertweet_actions.R Переглянути файл

@@ -17,15 +17,7 @@ gathertweet_search <- function(
) {
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(
terms,
@@ -44,8 +36,6 @@ gathertweet_search <- function(
)
)



if (isTRUE(`no-parse`)) {
log_info("Saving un-parsed tweets in {file}")
saveRDS(tweets, file)
@@ -117,6 +107,53 @@ gathertweet_timeline <- function(
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[order(tweets$status_id), ]

@@ -152,3 +189,19 @@ gathertweet_simplify <- function(
}

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
}

+ 15
- 3
inst/gathertweet.R Переглянути файл

@@ -5,8 +5,9 @@

Usage:
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 timeline [options] [--] <users>...
gathertweet favorites [options] [--] <users>...
gathertweet simplify [--file=<file> --output=<output> --debug-args --polite] [<fields>...]

Options:
@@ -48,9 +49,11 @@ search and timeline:
(default is to exclude RTs)
--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,
separated by spaces.

timeline:
--home If included, returns home-timeline instead of user-timeline.

simplify:
@@ -78,7 +81,7 @@ do_gathertweet <- function() {
collapse <- function(..., sep = ", ") paste(..., collapse = sep)

# 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]))
if (!length(action)) {
log_fatal("Please specify a valid action: {collapse(valid_actions)}")
@@ -129,6 +132,15 @@ do_gathertweet <- function() {
}

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)
}



Завантаження…
Відмінити
Зберегти