Sfoglia il codice sorgente

Add get_user_info() and related functions

master
Garrick Aden-Buie 7 anni fa
parent
commit
a48546bc7f
3 ha cambiato i file con 64 aggiunte e 3 eliminazioni
  1. +1
    -0
      NAMESPACE
  2. +47
    -3
      R/tweet_io.R
  3. +16
    -0
      man/get_user_info.Rd

+ 1
- 0
NAMESPACE Vedi File

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

export("%>%")
export(backup_tweets)
export(get_user_info)
export(install_gathertweet)
export(last_seen_tweet)
export(log_debug)

+ 47
- 3
R/tweet_io.R Vedi File

@@ -4,7 +4,8 @@ save_tweets <- function(
file = getOption("gathertweet.file", "tweets.rds"),
save_fun = saveRDS,
read_fun = read_tweets,
lck = NULL
lck = NULL,
key_var = "status_id"
) {
if (nrow(tweets) < 1) return(tweets)
fs::dir_create(fs::path_dir(file))
@@ -18,11 +19,11 @@ save_tweets <- function(
# Don't drop or lose old tweets
tweets_prev <- read_fun(file, lck = lck)
if (!is.null(tweets_prev)) {
tweets_not_new <- anti_join(tweets_prev, tweets, by = "status_id")
tweets_not_new <- anti_join(tweets_prev, tweets, by = key_var)
if (nrow(tweets_not_new)) {
tweets <- bind_rows(tweets, tweets_not_new)
}
if (length(setdiff(tweets_prev$status_id, tweets$status_id)) != 0) {
if (length(setdiff(tweets_prev[[key_var]], tweets[[key_var]])) != 0) {
log_fatal("An error occurred that would have lost stored tweets")
}
}
@@ -158,3 +159,46 @@ shared_lock <- function(file, timeout = 1 * 60 * 1000) {
exclusive_lock <- function(file, timeout = 1 * 60 * 1000) {
lock(path_lock(file), exclusive = TRUE, timeout = timeout)
}

#' @title Get user info
#' @param file The file where tweets are located. The text `_users` is
#' automatically appended to this file name.
#' @export
get_user_info <- function(
tweets = NULL,
file = getOption("gathertweet.file", "tweets.rds"),
dir_profile_images = NULL
) {
if (is.null(tweets)) read_tweets(file)
user_file <- path_add(file, append = "_users")
users <- tweets %>%
rtweet::users_data() %>%
dplyr::distinct()

users <- save_tweets(users, user_file, key_var = "user_id")

if (!is.null(dir_profile_images)) {
rs <- lapply(users$profile_image_url, download_profile_images, output_dir = dir_profile_images)
}

return(users)
}

download_profile_images <- function(profile_image_url, ..., output_dir = "data") {
output_file <- sub("^.+?profile", "profile", profile_image_url)
output_file <- fs::path(output_dir, output_file)
fs::dir_create(fs::path_dir(output_file), recursive = TRUE)
download_file(profile_image_url, output_file)
}

download_file <- function(url, dest) {
if (fs::file_exists(dest)) return(dest)
x <- list(result = NULL, error = NULL)
x$result <- tryCatch({
download.file(url, dest)
dest
}, error = function(e) x$error <<- e$message)

if (!is.null(x$error)) {
log_warn("Error downloading {dest}: {x$error}")
} else x$result

+ 16
- 0
man/get_user_info.Rd Vedi File

@@ -0,0 +1,16 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tweet_io.R
\name{get_user_info}
\alias{get_user_info}
\title{Get user info}
\usage{
get_user_info(tweets = NULL, file = getOption("gathertweet.file",
"tweets.rds"), dir_profile_images = NULL)
}
\arguments{
\item{file}{The file where tweets are located. The text \code{_users} is
automatically appended to this file name.}
}
\description{
Get user info
}

Loading…
Annulla
Salva